Sunday, 26 April 2015

U6P5

Define a class complex with real and imaginary as two data member with default & parameterized constructors ,function to initialize display data of class .It should overload the '+' operator to add two complex objects. Write a program to demonstrate use of complex class.

Code : 
 #include<iostream.h>  
 #include<conio.h>  
 class complex  
 {  
      public:  
      float x,y;  
      complex()  
      {  
           x=0;  
           y=0;  
      }  
      complex(float real,float image)  
      {  
           x=real;  
           y=image;  
      }  
      void display(complex c)  
      {  
           cout<<"Real: "<<c.x<<" + "<<"Img: "<<c.y<<endl;  
      }  
 };  
 complex sum(complex c1,complex c2)  
 {  
      complex c3;  
      c3.x=c1.x+c2.x;  
      c3.y=c1.y+c2.y;  
      return c3;  
 }  
 void main()  
 {  
      clrscr();  
      complex A(1.1,2.1),B(1.2,2.2),C;  
      C=sum(A,B);  
      cout<<"For A:\n";  
      A.display(A);  
      cout<<"\nFor B:\n";  
      B.display(B);  
      cout<<"\nFor A+B:\n";  
      C.display(C);  
      getch();  
 }  

Output : 

No comments:

Post a Comment