#include #define T printf("%s real=%lg image=%lg\n", __PRETTY_FUNCTION__,real, image) class complex { private: double real; double image; public: complex() { // Конструктор по умолчанию real = 0; image = 0; T; } complex(double re, double im) { // Конструктор real = re; image = im; T; } complex(const complex &oth) { real = oth.real; image = oth.image; T; } ~complex() { // Деструктор T; } void print() { T; printf("(%lg,%lg)\n", real, image); } // Геттеры и сеттеры // Имеется понятие Текущий элемент. self в python, this в c++. double re() const { return real; } // Это - синоним this->real; double im() const { return image;} void setRe(double val) { real = val; } void setIm(double val) { image = val; } complex operator-(complex const &b) const { // Есть и текущий объект! complex c(real - b.real, image - b.image); // real = 10; image = 50; без const разрешено! return c; } }; // Внешняя реализация операции + complex operator+(complex const &a, complex const &b) { complex c(a.re()+b.re(), a.im()+b.im()); return c; } // Осторожно! Имеется побочный эффект! complex foo_non_const(complex &a, complex &b) { a.setRe(a.re() + b.re()); a.setIm(a.im() + b.im()); return a; } complex foo(complex const &a, complex const &b) { complex c; c.setRe(a.re() + b.re()); c.setIm(a.im() + b.im()); return c; } int main() { complex a(10,20); complex b(30,40); //complex c = foo(a,b); complex c = a + b - complex(39,58); a.print(); b.print(); c.print(); }