1. Program to design a class representing complex numbers and perform addition and multiplication using operator overloading.
#include <iostream.h>
#include<conio.h>
class Complex {
public:
double real, imag;
// Constructor
Complex(double r = 0, double i = 0) {
real = r;
imag = i;
}
// Overloading + operator
Complex operator+(const Complex& obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
// Overloading * operator
Complex operator*(const Complex& obj) {
Complex res;
res.real = real * obj.real - imag * obj.imag;
res.imag = real * obj.imag + imag * obj.real;
return res;
}
void print() {
cout << real << " + i" << imag << endl;
}
};
int main() {
int c11 , c12 ,c21,c22= 0;
cout << "Enter 1st Real number & Imagnory number: ";
cin >> c11 >> c12;
cout << "Enter 2nd Real number & Imagnory number: ";
cin >> c21 >> c22;
Complex c1(c11, c12), c2(c21, c22);
Complex c3 = c1 + c2;
Complex c4 = c1 * c2;
cout << "Sum: ";
c3.print();
cout << "Product: ";
c4.print();
getch();
return 0;
}