Write a program to find sum of complex number using friend function.

#include<iostream.h>
#include<math.h>
#include<conio.h>
class complex
{
    private:
    float real,imag;
    public:
       void getdata()
       {
      cout<<"Enter the real part:";
      cin>>real;
      cout<<"Enter the imaginary part:";
      cin>>imag;
       }
       void show()
       {
      if(imag<0)
      cout<<real<<"-i"<<abs(imag);
      else
      cout<<real<<"+i"<<imag;
       }
       friend complex operator + (complex ,complex);
};

complex operator +(complex c1,complex c2)
{
   complex temp;
   temp.real=c1.real+c2.real;
   temp.imag=c1.imag+c2.imag;
   return (temp);
}
void main()
{
   clrscr();
   complex c1,c2,c3;
   cout<<"First complex number"<<endl;
   c1.getdata();
   cout<<"Second complex number"<<endl;
   c2.getdata();
   c3=c1+c2;
   cout<<endl<<"First complex number";
   c1.show();
   cout<<endl<<"Second complex number";
   c2.show();
   cout<<"\nSum of complex number:";
   c3.show();
   getch();
}

No comments:

Post a Comment