1. Implement a class Complex which represents the Complex Number data type.
Implement the following
1. Constructor (including a default constructor which creates the complex number
0+0i).
2. Overload operator+ to add two complex numbers.
3. Overload operator* to multiply two complex numbers.
4. Overload operators << and >> to print and read Complex Numbers.
#include<iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
// Default constructor
Complex() {
real = 0;
imag = 0;
// Parametrized constructor
Complex(double r, double i) {
real = r;
imag = i;
// Overloading + operator
Complex operator +(const Complex& obj) {
// Return a new Complex object with sum of real and imaginary parts
return Complex(real + [Link], imag + [Link]);
// Overloading * operator
Complex operator *(const Complex& obj) {
double r = real * [Link] - imag * [Link]; // Real part
double i = real * [Link] + imag * [Link]; // Imaginary part
return Complex(r, i);
// Overloading << operator
friend ostream& operator <<(ostream& out, const Complex& c) {
if ([Link] >= 0) {
out << [Link] << "+" << [Link] << "i"; // Only "out" here, not cout
} else {
out << [Link] << "-" << -[Link] << "i";
return out; // Return the stream for chaining
// Overloading >> operator
friend istream& operator >>(istream& in, Complex& c) {
in >> [Link] >> [Link]; // Read real and imaginary parts
return in; // Return the stream for chaining
};
int main() {
Complex c1, c2, sum, product;
// Read two complex numbers
cout << "Enter the first complex number (real and imaginary): ";
cin >> c1;
cout << "Enter the second complex number (real and imaginary): ";
cin >> c2;
// Add two complex numbers
sum = c1 + c2;
cout << "Sum = " << sum << endl;
// Multiply two complex numbers
product = c1 * c2;
cout << "Product = " << product << endl;
return 0;