/* 1. Write a C++ program to create a class called as COMPLEX, and implement the
following by overloading the function ADD which returns the COMPLEX number.
(i) ADD(s1,s2)- where s1 is an integer(real part) and s2 is a COMPLEX number.
(ii) ADD(s1,s2)- where s1 and s2 are COMPLEX numbers.
Display the result by overloading the operator <<. */
#include<iostream.h>
#include<conio.h>
#include<math.h>
class COMPLEX
{
int real;
int img;
public:
void READ()
{
cout<<"\nEnter real part of COMPLEX number : ";
cin>>real;
cout<<"\nEnter imaginary part of a COMPLEX number : ";
cin>>img;
}
COMPLEX ADD(int s1,COMPLEX s2);
COMPLEX ADD(COMPLEX s1,COMPLEX s2);
friend ostream& operator <<(ostream& Out,COMPLEX s);
};
ostream& operator <<(ostream& Out,COMPLEX s)
{
if (s.img < 0)
Out<<s.real<<"-i"<<abs(s.img);
else
Out<<s.real<<"+i"<<abs(s.img);
return Out;
}
COMPLEX COMPLEX::ADD(int s1,COMPLEX s2)
{
real = s1 + s2.real;
img = s2.img;
return (*this);
}
COMPLEX COMPLEX::ADD(COMPLEX s1,COMPLEX s2)
{
real = s1.real + s2.real;
img = s1.img + s2.img;
return (*this);
}
void main()
{
COMPLEX a,b,c;
int realno;
clrscr();
cout<<"Enter the values for the 1st COMPLEX number\n";
a.READ();
cout<<"\n\nEnter the values for the 2nd COMPLEX number\n";
b.READ();
c.ADD(a,b); /* Add two COMPLEX numbers */
cout<<"\n\nResult - Addition of 2 complex numbers is : ";
cout<<a<<" + "<<b<<" = "<<c;
cout<<"\n\nEnter integer constant (real part) to be added to a complex no : ";
cin>>realno;
c.ADD(realno,b);
cout<<"\nResult - Addition of integer (real part) & a complex no is :\n";
cout<<b<<" + "<<realno<<"+i0 = "<<c;
getch();
}