#include <iostream>
using namespace std;
class Cmplx {
public:
double re;
double im;
// Конструктор по умолчанию и с параметрами
Cmplx(double r = 0.0, double i = 0.0) : re(r), im(i) {}
// Конструктор копирования
Cmplx(const Cmplx& other) : re(other.re), im(other.im) {}
// Оператор присваивания
Cmplx& operator=(const Cmplx& other) {
if (this != &other) {
re = other.re;
im = other.im;
}
return *this;
}
// Вычитание комплексных чисел
Cmplx operator-(const Cmplx& other) const {
return Cmplx(re - other.re, im - other.im);
}
// Умножение комплексных чисел: (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
Cmplx operator*(const Cmplx& other) const {
return Cmplx(re * other.re - im * other.im, re * other.im + im * other.re);
}
};
// Внешний оператор сложения (как в вашем примере для MyInt)
Cmplx operator+(const Cmplx& me, const Cmplx& other) {
return Cmplx(me.re + other.re, me.im + other.im);
}
// Оператор вывода в поток (красиво форматирует вывод знака мнимой части)
ostream& operator<< (ostream& strm, const Cmplx& c) {
strm << c.re;
if (c.im >= 0) {
strm << "+" << c.im << "i";
} else {
strm << c.im << "i"; // Минус выведется автоматически вместе с числом
}
return strm;
}
int main() {
Cmplx a(2.5, 4.0); // 2.5 + 4i
Cmplx b(1.5, -2.0); // 1.5 - 2i
Cmplx sum = a + b; // Сложение
Cmplx diff = a - b; // Вычитание
Cmplx mult = a * b; // Умножение
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a + b = " << sum << endl; // Выведет: 4+2i
cout << "a - b = " << diff << endl; // Выведет: 1+6i
cout << "a * b = " << mult << endl; // Выведет: 11.75+1i
return 0;
}