Wednesday, 11 March 2015

Write a program that will demonstrate friendly function

Write a program that will demonstrate friendly function. The program that uses the following properties – two class names- getcn, getdn. getcn class contain one integer type private member variables- c, public member function get_val(int e). getdn class contain one integer type private member variables- d, public member function get_val(int e). Both class contains friend function - friend void add(getcn x, getdn y). get_val(int e) function get the value of e from main function. Friend function add(getcn x, getdn y) will add both c and d values and display the addition result as output.

#include<iostream>
using namespace std;
class getdn;
class getcn
{
    int c;
public:
    int get_val(int e){c=e;}
    friend void add(getcn,getdn);
};
class getdn
{
    int d;
public:
    int get_val(int e){d=e;}
    friend void add(getcn,getdn);
};
void add(getcn x,getdn y)

    {
        cout<<"Addition is : "<<x.c+y.d<<endl;
    }
 int main()
 {
     getcn obj1;
     obj1.get_val(10);
     getdn obj2;
     obj2.get_val(10);
     add(obj1,obj2);
     return 0;

 }

No comments:

Post a Comment