C++ (Qt)class Number{public: Number() : _x(0) {} Number(int x) : _x(x) {} Number &operator+(const Number &x) { _x += x._x; return *this; } friend std::ostream& operator<<(std::ostream&, const Number&); //friend const Number operator+(const Number&, const Number&);private: int _x;}; std::ostream& operator<<(std::ostream& out, const Number& x) { out << x._x; return out;} /*const Number operator+(const Number&x1, const Number &x2) { return Number(x1._x + x2._x);}*/ Number n1 = 1;Number n2 = 2; Number n3 = n1+(1+n2); cout << n3 << endl;
C++ (Qt)class TPNumber{ /* .......... */ TPNumber operator +(const TPNumber &p1); /* .......... */} TPNumber TPNumber::operator +(const TPNumber &p1){ return TPNumber(this->number + p1.number, this->sistemaSchisleniya, this->tochnost);}
C++ (Qt)Number x = 1;NumberF y = 3.14; NumberF sum = x + y; // Внимание сюда!
C++ (Qt)Number *n1 = new NumberF(3.14);Number *n2 = new NumberF(2.71); *n2 = *n2 + *n1;
C++ (Qt)TPNumber TPNumber::operator +(const TPNumber &p1){ return TPNumber(this->number + p1.number, this->sistemaSchisleniya, this->tochnost);}
C++ (Qt)TPNumber TPNumber::operator +(const TPNumber &p1) const
C++ (Qt)#include <fstream> class PNumber { ...};
C++ (Qt)class Number{ /* ... ... ... */public: virtual Number& operator =(Number &n) = 0; virtual Number& operator +(Number &n) = 0; /* ... ... ... */}; class PNumber : public Number{ /* ... ... ... */ Number& operator =(Number &p1); Number& operator +(Number &p1); /* ... ... ... */}; Number& PNumber::operator +(PNumber &p1){ PNumber *pn; Number *n = &p1; pn = dynamic_cast<PNumber *>(n); pn->number += this->number; return *pn;}
C++ (Qt)Number& PNumber::operator +(PNumber &p1){ PNumber *pn; Number *n = &p1; pn = dynamic_cast<PNumber *>(n); pn->number += this->number; return *pn;}
C++ (Qt)enum NumberType {TNumberInt, TNumberF}; class Sum; class Number{public: virtual ~Number() {} virtual NumberType type() const = 0; virtual Number& operator=(const Number&) = 0; virtual Number& operator=(const Sum&) = 0;}; class Sum{public: Sum(const Number& x1, const Number& x2) : _x1(x1), _x2(x2) {} const Number& x1() const { return _x1; } const Number& x2() const { return _x2; } NumberType type() const { return _x1.type(); }private: const Number& _x1; const Number& _x2;}; class NumberInt : public Number{public: NumberInt() : _x(0) {} NumberInt(int x) : _x(x) {} virtual NumberType type() const { return TNumberInt; } virtual Number& operator=(const Number&x) { if (x.type() != TNumberInt) throw "error!"; const NumberInt& num = static_cast<const NumberInt&>(x); _x = num._x; return *this; } virtual Number& operator=(const Sum& s) { if (s.type() != TNumberInt) throw "error!"; const NumberInt& i1 = static_cast<const NumberInt&>(s.x1()); const NumberInt& i2 = static_cast<const NumberInt&>(s.x2()); _x = i1._x + i2._x; return *this; } friend const NumberInt operator+(const NumberInt&, const NumberInt&); friend std::ostream& operator<<(std::ostream&, const NumberInt&);private: int _x;}; std::ostream& operator<<(std::ostream& out, const NumberInt& x) { out << x._x; return out;} const Sum operator+(const Number& x1, const Number& x2) { return Sum(x1, x2);} const NumberInt operator+(const NumberInt& x1, const NumberInt& x2) { return NumberInt(x1._x + x2._x);} int main(){ Number *n1 = new NumberInt(1); Number *n2 = new NumberInt(2); *n1 = *n2 + *n2; cout << static_cast<const NumberInt&>(*n1); return 0;}
C++ (Qt)struct Number { virtual const Number* add(const Number* other) const = 0;}; // Собственно операции с "числами"Number* add(const Number* x, const Number* y) { return x->add(y);}...// Реализация двойной деспеччиризацииstruct Int;struct Rational;struct Real; struct Int : Number { virtual const Number* add(const Number* other) const { return other->addInt(this); } const Int* addInt(const Int* other) const { return new Int(val + other->val); } const Rational* addRational(const Rational* other) const { return other->addInt(this); } const Real* addReal(const Real* other) const { return other->addInt(this); } int get_val() const { return val; } private: int val;}; struct Rational : Number { virtual const Number* add(const Number* other) const { return other->addRational(this); } const Rational* addInt(const Int* o) const { return new Rational(nom + denom * o->get_val(), denom); } const Rational* addRational(const Rational* o) const { return Rational(nom * o->denom + o->nom + denom, denom * o->denom); } const Real* addReal(const Real* other) const { return other->addRational(this); } private: int nom, denom;};...