Operator Overloading.cpp
Operator Overloading.cpp
Div-SE-1
Roll Number-38
Assignment number - 6
Implement a class Complex which represents the Complex Number data type. Implement the
following operations:
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers
#include<iostream>
class Complex
float i;
float j;
public:
Complex()
i=0;
j=0;
input>>k.i;
input>>k.j;
output<<k.i<<"+"<<k.j<<"i\n";
};
Complex Complex::operator+(Complex c)
Complex temp1;
temp1.i=i+c.i;
temp1.j=j+c.j;
return(temp1);
Complex Complex::operator*(Complex c)
Complex temp2;
temp2.i=(i*c.i)-(j*c.j);
temp2.j=(j*c.i)+(i*c.j);
return(temp2);
}
int main()
Complex c1,c2,c3,c4;
cout<<c1;
cin>>c1;
cin>>c2;
c3=c1+c2;
c4=c1*c2;
cout<<c1;
cout<<c2;
cout<<c3;
cout<<c4;
return 0;