C++ program : to Find sum of two complex numbers




#include<iostream.h>
#include<conio.h>
class complex
{
   int r;
   int i;
   public:
   void get()
   {
      cout<<"Enter the real part  :";
      cin>>r;
      cout<<"Enter the Image part :";
      cin>>i;
   }
   void put();
   complex operator+(complex);
};
complex complex::operator+(complex t)
{
   complex ans;
   ans.r=r+t.r;
   ans.i=i+t.i;
   return ans;
}
void complex::put()
{
   cout<<r;
   if(i<0)
      cout<<i<<"i";
   else
      cout<<"+"<<i<<"i";
}



int main()
{
   clrscr();
   complex a,b,c;
   cout<<"\tComplex Number 1\n";
   a.get();
   cout<<"\tComplex Number 2\n";
   b.get();
   c=a+b;
   cout<<"The sum is : ";
   c.put();
   getch();
   return 0;
}

Class diagram 

(click image to view large)

 

 



Related Posts:

0 comments:

Post a Comment