Overloading Operator Using A Friend Function In C++
The operator overloading function must precede with friend
keyword, and declare a function class scope. Keeping in mind, friend operator
function takes two parameters in a binary operator, varies one parameter in a
unary operator.
Fig:Overloading Operator Using A Friend Function In C++ |
Program:
#inlcude<iostream.h>
#include<conio.h>
class bca
{
public:
int i,j;
bca()
{
this->i=0;
this->j=0;
}
bca(int n,int m)
{
this->i=n;
this->j=m;
}
friend bca operator+(bca&,bca&);
};
bca operator+(bca & d1, bca& d2)
{
bca d3;
d3.i=d1.i+d2.i;
d3.j=d1.j+d3.j;
return d3;
}
void main()
{
bca d1(8,9);
bca d2(10,2);
bca d3;
d3=d1+d2;
cout<<”/n total number of i and
j<<”d3.i<<” “<<d3.j;
getch();
}
Output:
Total number of I and j 18’11
No comments:
Post a Comment