Ex No.1b PROGRAM USING FRIEND FUNCTION
4-8-10
AIM:To add two complex numbers using c++.
ALGORTHIM:
Step 1: Start the program.
Step 2: The operation of sum is declared as friend function.
Step 3: Call the functions from the main functions.
Step 4: As the function has arguments, the value are given in main function itself.
Step 5: The sum of two complex no.is calculated.
Step 6: End the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>.
class complex
{
float x; float y;
public:
void input(float real,float image)
{
x=read;y=image;
}
friend complex sum(complex,complex);
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 complex::show(complex c)
{
cout<<c.x<<"+j"<<c.y;
}
int main()
{
clrscr();
complex A,B,C;
A.input(3.1,5.5);
B.input(2.7,1.2);
C=sum(A,B);
cout<<"\n A=";A.show(A);
cout<<"\n B=";B.show(B);
cout<<"\n C=";C.show(C);
getch();
return 0;
}
RESULT:
Thus the c++ program to find the sum of two complex number using friend function us executed and output is verified successfully.