Searching...
Wednesday, 23 November 2016

Friend Function in c and c++ to use data of private Classes with example

Friend Function


Command use to link private classes which have no link or inheritance with other.
Friend word is use to share data f such type of classes data.
Friend Function in c and c++ to use data of private Classes with example


Program:-------

#include<iostream>
using namespace std;
class B;
class A
{
                private:
                                int a;
                                public:
                                                A()
                                                {
                                                                a=10;
                                                }
                                                friend void add(A ,B);
                                               
};

class B
{
  private:
                int b;
                public:
                                B()
                                {
                                                b=5;
                                  }
friend void add(A  , B);  
};
 void add(A obj1 , B obj2)
{
                int s;
                s = obj1.a + obj2.b;
                cout<<"\n Sum Of "<<obj1.a<<"and "<<obj2.b<<"="<<s<<endl;
}
int main()
{
                A a1;
                B b1;
     add(a1 , b1);
                return 0;
}


0 comments:

Post a Comment