Complex Nums

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

PROGRAM:

/* 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. 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>
using namespace std;
class complex {
public:
int real, imag;
complex(){
real = 0;
imag = 0;
}
void set(){
cout<<"\nEnter real number: ";
cin>>real;
cout<<"\nEnter imaginary number: ";
cin>>imag;
}
void display(){
cout<<real<<"+"<<imag<<"i";
}

void operator >> (complex c){


int num1;
int num2;
int num3;
int num4;
num1 >> real;
num2 >> imag;
num3 >> c.real;
num4 >> c.imag;
cout << num1 << "+" << num2 << "i";
cout << num3 << "+" << num4 << "i";
}

complex operator+(complex c){


complex sum;
sum.real = real+c.real;
sum.imag = imag + c.imag;
return(sum);
}
complex operator*(complex c){
complex mul;
mul.real = c.real*real - c.imag*imag;
mul.imag = imag*c.real + c.imag*real;
return(mul);
}
};
int main(){
complex c1,c2,c3,c4,c5,c6;
cout<<"\nDefault constructor value is: ";
c1.display();
c1.set();
c2.set();
c3=c1+c2;
c4=c1*c2;
cout<<"The sum of the two complex numbers is: ";
c3.display();
cout<<"\nThe multiplication of the two complex numbers is: ";
c4.display();
c5 = c1;
cout<<"\nThe first complex number is: ";
c5.display();
c6 = c2;
cout<<"\nThe second complex number is: ";
c6.display();
return 0;
}

***************************************************************************************

OUTPUT:

Default constructor value is: 0+0i


Enter real number: 2
Enter imaginary number: 3
Enter real number: 4
Enter imaginary number: 5
The sum of the two complex numbers is: 6+8i
The multiplication of the two complex numbers is: -7+22i
The first complex number is: 2+3i
The second complex number is: 4+5i

You might also like