Wednesday, 26 June 2013

complex number arithemetic operation using friend function

  Using friend function write a c++ program to add 2 complex

                                  numbers.

#include<iostream.h>
#include<conio.h>
class Complex{
private:
int real,imaginary;
public:
void getComplexNo()
{
cout<<"Input real and Imaginary part for Complex No";
cin>>real>>imaginary;
}
void printComplexNO()
{
cout<<"Complex NO-->"<<"+i"<<imaginary<<"\n";
}
friend Complex AddComplex(Complex C1,Complex c2);
};
Complex AddComplex(Complex C1,Complex C2)
{
Complex T;
T.real=C1.real+C2.real;
T.imaginary=C1.imaginary+C2.imaginary;
return T;
}
void main()
{
Complex A,B;
cot<<"Get First Complex number\n";
A.getComplexNo();
cout<<"Get Second Complex Number\n";
B.getComplexNo();
cout<<"Finding total of complex No's\n";
Complex Tot=AddComplex(A,B);
getch();
clrscr();
cout<<"First Complex No\n";
cout<<"------------------\n";
A.printComplexNo();
cout<<"Second Complex No\n";
cout<<"----------------\n";
B.printcomplexNo();
cout<<"Printing the Result\n";
cout<<"-------------------\n";
Tot.printComplexNo();
cot<<"-----------------\n";
getch();
}


Output:
First Complex No
-----------------
Complex No=2+i3
Second Complex No
----------------
Complex No=5+i2
Printing the Result
---------------------
Complex No=7 +i5
-------------------
Get First Complex number
Input real and Imaginary part for Complex No12
Input real and Imaginary part for Complex No12
4
Get Second Complex Number
Input real and Imaginary part for Complex No9
4
Finding Total of Complex no's
First Complex No
------------------
Complex no-->12+i4
Second Complex No
-----------------
complex No-->9+i4
Printing the Result
----------------- 
Complex No-->21+i8
------------------



No comments:

Post a Comment