Saturday, 23 March 2019

CPP-EX47

Create a class ComplexNumber containing members as:
     - real
     - imaginary
     Write a C++ program to calculate and display the sum of two complex
     numbers.(Use friend function and return by object) 

 #include<iostream.h>
#include<conio.h>
 class complex
   {
    float x;
    float y;
    public:
    complex() { }
    complex(float a) {x=y=a;}
    complex(float real,float imag)
    {x=real;y=imag;}
    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<<"\n";
   }
 int main()
   {
    clrscr();
    complex A(2.7,3.5);
    complex B(1.6);
    complex C;
    C=sum(A,B);
    cout<<"\nA=";
    show(A);
    cout<<"B=";
    show(B);
    cout<<"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";
    cout<<"P=";
    show(P);
    cout<<"Q=";
    show(Q);
    cout<<"R=";
    show(R);
    getch();
    return 0;
   }