C++ (Qt)class Number{public: // ... virtual Number& operator +(Number &n) = 0; virtual Number& operator -(Number &n) = 0; virtual Number& operator /(Number &n) = 0; virtual Number& operator *(Number &n) = 0;//... };
C++ (Qt)Number *n1 = new TANumber;Number *n2 = new TANumber; *n1 = *n1 + *n2;*n1 = *n1 - *n2;*n1 = (*n1) * (*n2);// ...// и даже так:TANumber n1;TANumber n2; n1 = n1 + n2;n1 = n1 - n2;// ...
C++ (Qt)struct CFractNumber : public CBaseNumber { CFractNumber & operator + (const CBaseNumber & other) const { assert(dynamoc_cast <const CFactNumber *> &other) != 0); CFractNumber & result = mBuf[mBufCount]; result = *this; mBufCount = (mBufCount + 1) % BUF_SIZE; return result += other; } private: static int mBufCount = 0; static CFractNumber mBuf[BUF_SIZE]; enum { BUF_SIZE = 256, };};
C++ (Qt)CFractNumber fun1(const CFractNumber& init, int cnt) { CFractNumber& res = init + init; for (int i = 0; i < cnt - 1; ++i) res = res + init; return res;}// или чуть подругому:CFractNumber fun2(const CFractNumber& init, int cnt) { const CFractNumber& first = init + init; CFractNumber res = first; for (int i = 0; i < cnt - 1; ++i) res = res + init + first; return res;}//И где-то в коде:CFractNumber b = fun1(a, 1000);CFractNumber d = fun2(c, 1000);