OOP Exp3
OOP Exp3
#CODE
#include<iostream>
#include<math.h>
using namespace std;
class complex
{
float real,img;
public:
void read();
void display();
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
complex operator~();
};
void complex::read()
{
cout<<"Enter The real and imaginary part:";
cin>>real>>img;
}
void complex::display()
{
if(img>=0)
cout<<real<<"+"<<img<<"i"<<endl;
else
cout<<real<<img<<"i"<<endl;
}
complex complex::operator~()
{
complex con;
con.real=real;
con.img=-img;
return con;
}
int main()
{
complex c,c1,ans;
c.read();
c1.read();
ans=c+c1;
cout<<"Sum is: ";ans.display();
ans=c-c1;
cout<<"Difference is: ";ans.display();
ans=c*c1;
cout<<"Product is: ";ans.display();
ans=c/c1;
cout<<"Division is: ";ans.display();
ans=~c;
cout<<"Conjugate of 1st no: ";ans.display();
ans=~c1;
cout<<"Conjugate of 2nd no: ";ans.display();
return 0;
}
#OUTPUT
Enter The real and imaginary part:4
7
Enter The real and imaginary part:2
4
Sum is: 6+11i
Difference is: 2+3i
Product is: -20+30i
Division is: 1.8-0.1i
Conjugate of 1st no: 4-7i
Conjugate of 2nd no: 2-4i