C++ program to add two complex numbers using friend function: This simple CPP tutorial is used to add the 2 real numbers & imaginary numbers. While compiling the source code, 2 real numbers are added and 2 imaginary parts also added separately. C++ Friend function is used to add those two complex numbers. An Example program is also given below.
Addition of Complex numbers:
[Useful: C Program Tutorials] & [IT Companies Details]
//Header files
#include<iostream.h>
#include<conio.h>
class complexz
{
int RL,IMGN;
public :
void get()
{
cout<<"\n\t Enter your Real Number Here: ";
cin>>RL; //Input method
cout<<"\n \t Enter your Imaginary Number Here:"; // Display method
cin>>IMGN;
}
friend void sum(complexz,complexz); // Friend function
};
void sum(complexz com1,complexz com2)
{
cout<<"\n Final Output result is : ";
cout<<"\n {"<<com1.RL<<" + i "<<com1.IMGN;
cout<<" } + { "<<com2.RL<<" + i "<<com2.IMGN;
cout<<" } = "<<com1.RL+com2.RL<<" + i "<<com1.IMGN+com2.IMGN;
}
void main()
{
complexz a1,a2;
clrscr();
cout<<"\n \t Sum of 2 complex Numbers Program\n\n";
// © http://students3k.com - Karthikh Venkat
cout<<"\n\t Given input value\n \t Operand Value 1: ";
a1.get();
cout<<"\n \t Operand Value 2";
a2.get();
sum(a1,a2);
getch();
}
SEE: All Examination Results & News