This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
class complx | |
{ | |
private: | |
int real,imag;//attributes | |
public: | |
void SetData(int r,int i) // setting data into private attribute | |
{ | |
real=r;imag=i; | |
} | |
void print() //display on output screen | |
{ | |
cout<<real<<"+"<<imag<<"i"<<endl; | |
} | |
friend complx sum(complx,complx);//friend function | |
}; | |
complx sum(complx c1,complx c2)//friend function definition | |
{ | |
complx temp; | |
temp.real=c1.real+c2.real; | |
temp.imag=c1.imag+c2.imag; | |
return temp; | |
} | |
int main() | |
{ | |
complx c1,c2,c3;//objects of class complx | |
c1.SetData(1,2); | |
c1.print(); | |
c2.SetData(3,4); | |
c2.print(); | |
c3=sum(c1,c2);// friend function call without object same as normal function | |
cout<<"Sum of two complex number :"; | |
c3.print(); | |
return 0; | |
} |
1+2i 3+4i Sum of two complex number :4+6i
0 Comments
If you have any doubt and suggestion Please let me know