FY-2-a Best OOP program-friend function for adding the two complex numbers

Friend function for adding the two complex numbers

Write a friend function for adding the two complex numbers, using a single class

Click here to get other OOP Practical

FYIT practical 2-a : Program to add two complex number using friend function

#include<iostream>
using namespace std;
class complex
{
float a,b;
public:
void get()
{
cout<<endl<<"Enter value for A=";
cin>>a;
cout<<endl<<"Enter value for B=";
cin>>b;
}
void disp()
{
cout<<endl<<a;
cout<<endl<<b;
}
friend complex sum(complex,complex);
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.a=c1.a+c2.a;
c3.b=c1.b+c2.b;
return c3;
}
int main()
{
complex x,y,z;
x.get();
x.disp();
y.get();
y.disp();
z=sum(x,y);
z.disp();
return 0;
}

Output

friend-function

Check out more FYIT OOP Practical Program and other logic building programs in C++

We are aiming to explain all concepts of C++ and OOP in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

1 thought on “FY-2-a Best OOP program-friend function for adding the two complex numbers”

Leave a Comment