#include<iostream.h>
#include<conio.h>
class complex
{
float real;
float imag;
public:
void getnum();
void putnum();
void sum(complex,complex);
void dif(complex,complex);
};
void complex::getnum()
{
cout<<"\nEnter the real part : ";
cin>>real;
cout<<"\nEnter the imaginary part : ";
cin>>imag;
}
void complex::putnum()
{
cout<<real;
if(imag<0)
cout<<imag<<"i\n";
else
cout<<"+"<<imag<<"i\n";
}
void complex::sum(complex a,complex b)
{
real=a.real+b.real;
imag=a.imag+b.imag;
}
void complex::dif(complex a,complex b)
{
real=a.real-b.real;
imag=a.imag-b.imag;
}
int main()
{
complex c1,c2,c3,c4;
cout<<"\nEnter first complex number\n";
c1.getnum();
cout<<"\nEnter second complex number\n";
c2.getnum();
//Sum of two inputted numbers
c3.sum(c1,c2);
cout<<"\nThe Sum is : ";
c3.putnum();
//Difference of two inputted numbers
c4.dif(c1,c2);
cout<<"\nThe Difference is : ";
c4.putnum();
Output:
Enter first complex number
Enter the real part : 4
Enter the imaginary part : 5
Enter second complex number
Enter the real part : 3
Enter the imaginary part : 6
The Sum is : 7+11i
The Difference is : 1-1i
#include<conio.h>
class complex
{
float real;
float imag;
public:
void getnum();
void putnum();
void sum(complex,complex);
void dif(complex,complex);
};
void complex::getnum()
{
cout<<"\nEnter the real part : ";
cin>>real;
cout<<"\nEnter the imaginary part : ";
cin>>imag;
}
void complex::putnum()
{
cout<<real;
if(imag<0)
cout<<imag<<"i\n";
else
cout<<"+"<<imag<<"i\n";
}
void complex::sum(complex a,complex b)
{
real=a.real+b.real;
imag=a.imag+b.imag;
}
void complex::dif(complex a,complex b)
{
real=a.real-b.real;
imag=a.imag-b.imag;
}
int main()
{
complex c1,c2,c3,c4;
cout<<"\nEnter first complex number\n";
c1.getnum();
cout<<"\nEnter second complex number\n";
c2.getnum();
//Sum of two inputted numbers
c3.sum(c1,c2);
cout<<"\nThe Sum is : ";
c3.putnum();
//Difference of two inputted numbers
c4.dif(c1,c2);
cout<<"\nThe Difference is : ";
c4.putnum();
Output:
Enter first complex number
Enter the real part : 4
Enter the imaginary part : 5
Enter second complex number
Enter the real part : 3
Enter the imaginary part : 6
The Sum is : 7+11i
The Difference is : 1-1i