// ConsoleApplication3.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//
#include <iostream>
using namespace std;
class Cmplx {
public:
double re;
double im;
Cmplx(double r, double i) {
re = r;
im = i;
}
Cmplx() {
re = 0;
im = 0;
}
Cmplx(Cmplx& cm) {
re = cm.re;
im = cm.im;
}
Cmplx& operator=(const Cmplx& other) {
re = other.re;
im = other.im;
return *this;
}
Cmplx operator-(const Cmplx& other) {
Cmplx result;
result.re = re - other.re;
result.im = im - other.im;
return result;
}
Cmplx operator*(const Cmplx& other) {
Cmplx result;
result.re = re * other.re - im * other.im;
result.im = re * other.im + im * other.re;
return result;
}
/* Cmplx operator+(const Cmplx& other) {
Cmplx result;
result.re = this->re + other.re;
result.im = this->im + other.im;
return result;
}*/
};
Cmplx operator+(const Cmplx& me, const Cmplx& other) {
Cmplx result;
result.re = me.re + other.re;
result.im = me.im + other.im;
return result;
}
ostream& operator<< (ostream& strm, Cmplx& cm) {
return strm << cm.re << (cm.im >= 0 ? "+" : "") << cm.im << "i";
}
int main() {
Cmplx a; // 0+0i
Cmplx b(12, 5); // 12+5i
Cmplx c(3, 2); // 3+2i
Cmplx d = c; // копирование
d = a + b; // сложение
d.operator=(operator+(a, b));
cout << d << endl;
d = b - c;
cout << d << endl;
d = b * c;
cout << d << endl;
}