//Sample Program to demonstrate the usage of friend function
#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
friend complex sum (complex,complex);
friend void show (complex) ;
   };
complex sum (complex c1, complex c2)
  {
    complex c3;
    c3.x = c1.x + c2.x;
    c3.y = c1.y + c2.y;
    return(c3);
   }
void show (complex c)
  {
   cout << c.x << "+j" << c.y << endl;
  }
void main()
 {
   clrscr();
   complex A(2.7, 3.5);
   complex B (1.6);
   complex C;
   C= sum (A, B);

   cout << "\n First Complex number is A= ";
   show(A);
   cout << "\n Second Complex number is B= ";
   show(B);
   cout << "\n Sum of two Complex number is C= ";
   show(C);
   complex P, Q, R;
   P= complex (2.5, 3.9);
   Q= complex (1.6, 2.5);
   R= sum (P, Q);
   cout << "\n\n First Complex number is P= ";
   show (P);
   cout << "\n Second Complex number is Q= ";
   show (Q);
   cout << "\n Sum of two Complex number is R= ";
   show (R);
  getch();
}
<< GO BACK

Our aim is to provide information to the knowledge seekers.


comments powered by Disqus


Footer1