OOP PR 3 (Complex)
OOP PR 3 (Complex)
#include<iostream>
using namespace std;
class complex
{
private:
float R1,R2,Ra,I1,I2,Ia,Rs,Is,M1,M2,Dr,Di;
float final;
public:
void read()
{
cout<<"Enter real part of first and second complex number:"<<endl;
cin>>R1>>R2;
cout<<"Enter imaginary part of first and second complex number:"<<endl;
cin>>I1>>I2;
}
void in()
{
cout<<"Your Entered Complex number is"<<endl;
cout<<R1<<" + "<<I1<<"i"<<endl;
cout<<R2<<" + "<<I2<<"i"<<endl<<endl;
}
void sum()
{
Ra=R1+R2;
Ia=I1+I2;
cout<<"The sum of The given complex number is :"<<endl;
cout<<Ra<<" + "<<Ia<<"i"<<endl<<endl;
}
void sub()
{
Rs=R1-R2;
Is=I1-I2;
cout<<"The sub of The given complex number is :"<<endl;
cout<<Rs<<" + "<<Is<<"i";
}
void mult(){
M1=(R1*R2)+(-(I1*I2));
M2=(R1*I2)+(R2*I1);
cout<<"\n\nThe multiplication of The given complex number is :"<<endl;
cout<<M1<<" + "<<M2<<"i"<<endl;
}
void div()
{
float denom = R2*R2 + I2*I2;
Dr = (R1*R2 + I1*I2) / denom;
Di = (I1*R2 - R1*I2) / denom;
cout<<"\n\nThe division of The given complex number is :"<<endl;
cout<<Dr<<" + "<<Di<<"i"<<endl;
}
};
int main(void)
{
complex C1,C2,C3;
C1.read();
C1.in();
C1.sum();
C1.sub();
C1.mult();
C1.div();
}
Output:
Enter real part of first and second complex number:
10
20
Enter imaginary part of first and second complex number:
5
6
Your Entered Complex number is
10 + 5i
20 + 6i