A program to find the sum of two numbers using the concept of friend function in C++

#include<iostream>
using namespace std;
class Demo
{
int fn,sn;
public:
void setdata(int a,int b)
{
fn=a;
sn=b;
}
friend int sum(Demo);
};
int sum(Demo p)
{
return(p.fn + p.sn);
}
int main()
{
int c;
Demo x;
x.setdata(2,2);
c=sum(x);
cout<<“Sum=”<<c;
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *