oop
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex() : real(0), imag(0) {}
Complex(double r, double i) : real(r), imag(i) {}
Complex operator+(const Complex& other) {
return Complex(real + other.real, imag + other.imag);
}
Complex operator*(const Complex& other) {
double newReal = (real * other.real) - (imag * other.imag);
double newImag = (real * other.imag) + (imag * other.real);
return Complex(newReal, newImag);
}
friend ostream& operator<<(ostream& out, const Complex& c) {
if (c.imag >= 0) {
out << c.real << "+" << c.imag << "i";
} else {
out << c.real << c.imag << "i";
}
return out;
}
friend istream& operator>>(istream& in, Complex& c) {
char ch;
in >> c.real >> c.imag >> ch;
return in;
}
};
int main() {
Complex c1, c2, result;
cout << "Enter first complex number (real and imaginary parts): ";
cin >> c1;
cout << "Enter second complex number (real and imaginary parts): ";
cin >> c2;
result = c1 + c2;
cout << "Sum: " << result << endl;
result = c1 * c2;
cout << "Product: " << result << endl;
return 0;
}