Tuesday, 2 July 2013

Write a program to show the concept of Friend Function in c++

#include<iostream.h>
#include<conio.h>
class two;//forward declaration
class one
{
    int m;
    public:
    void getdata()
    {
        cout<<"enter the value for m:";
        cin>>m;
    }
    friend int sum(one,two);
};//end of class one definiton

class two
{
    int n;
    public:
    void getdata()
    {
        cout<<"enter the value for n:";
        cin>>n;
    }
    friend int sum(one,two);
};//end of class two
int sum(one a, two b)//definition of friend function
{
    return(a.m + b.n);
}
void main()
{
    one x;
     two y;
     x.getdata();
     y.getdata();
     cout<<"sum="<<sum(x,y);//calling friend function
}



No comments:

Post a Comment