Monday, 23 February 2015

U3P8

Define a class complex, having data member as X and Y, Define a friend function sum () to add two complex numbers and display all numbers using show () friend function.

 #include<iostream.h>  
 #include<conio.h>  
 class complex  
 {  
      double X,Y;  
      public:  
      void asign()  
      {  
           X =38.3;  
           Y =46.4;  
      }  
      friend void sum(complex);  
      friend void show(complex);  
 };  
 void sum(complex obj)  
 {  
      cout<<"SUM of given numbers : "<<obj.X + obj.Y<<endl;  
 }  
 void show(complex obj)  
 {  
      cout<<"Given integer value for X is : "<<obj.X<<endl;  
      cout<<"Given integer value for Y is : "<<obj.Y<<endl;  
 }  
 void main()  
 {  
      clrscr();  
      cout<<"Program will show use of Friend Function."<<endl;  
      complex c;  
      cout<<"Assigning values...\nX = 38.3\nY = 46.4"<<endl;  
      c.asign();  
      cout<<"\nCalling sum() Friend Function..."<<endl;  
      sum(c);  
      cout<<"\nCalling show() Friend Function..."<<endl;  
      show(c);  
      getch();  
 }  

OUTPUT :

No comments:

Post a Comment