//Sample program to add two Complex numbers using class and object
#include < iostream.h >
#include < conio.h >

class complex
  {  float x ,y;
      public:
     complex() { }   //C1
     complex (float a){ x=y=a; } //C2
      complex (float real, float img)
	      { x= real; y= img; } //C3
complex sum (complex);
void show () ;
   };
complex complex::sum (complex c)
  {
    complex temp;
    temp.x = x + c.x;
    temp.y = y + c.y;
    return(temp);
   }
void complex ::show ()
  {
   cout << x << "+j" << y << endl;
  }
void main()
 {
   clrscr();
   complex A(2.7, 3.5);
   complex B (1.6);
   complex C;
   C= A.sum (B);
   cout << "\n First Complex Number A= ";
   A.show();
   cout << "\n Second Complex Number B= ";
   B.show();
   cout << "\n Sum of two Complex Numbers C= ";
   C.show();
   getch();
}
Click here to go back

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus




Footer1