Program for the addition of 3 complex numbers using FRIEND function

/*Program for the addition of 3 complex numbers using FRIEND function*/

#include<iostream.h>

#include<conio.h>

class complex

{

int real,imag;

public:

complex()

{

}

complex(int f,int g)

{

real=f;

imag=g;

}

void display()

{

cout<<real<<“+i”<<imag;

}

friend complex sum (complex,complex,complex);

};

complex sum(complex q,complex w,complex e)

{

complex r;

r.real=q.real+w.real+e.real;

r.imag=q.imag+w.imag+e.imag;

return r;

}

int main()

{

int z,x,c,v,y,u;

cout<<“\n\nenter the real and imaginary part of 1st complex number\n”;

cin>>y>>u;

complex p(y,u);

cout<<“1st complex number       “;

p.display();

cout<<“\n\nenter the real and imaginary part of 2nd complex number\n”;

cin>>z>>x;

complex o(z,x);

cout<<“\n2nd complex number       “;

o.display();

cout<<“\n\nenter the real and imaginary part of 3rd complex number\n”;

cin>>c>>v;

complex i(c,v);

cout<<“\n3rd complex number       “;

i.display();

complex l=sum(p,o,i);

cout<<“\n\naddition of these 3 complex numbers            “;

l.display();

getch();

return 0;

}

­________________________________________________________

OUTPUT       :

Enter the real and imaginary part of 1st complex number

1        2

1st complex number                 1+i2

Enter the real and imaginary part of 2nd complex number

3        4

2nd complex number                3+i4

Enter the real and imaginary part of 3rd complex number

5        6

3rd complex number                 5+i6

Addition of these 3 complex numbers                  9+i12

 







Leave a Reply

Your email address will not be published. Required fields are marked *