Sunday 3 February 2013

friend function in c++


//develop relation b/w two classes using friend function
#include<iostream.h>
#include<conio.h>

class B;     //declare class
class A       //declare and define
{
private:
int x;
public:
A()
   {
   x=10;
   }

friend void show(A,B);   //declare friend function
};


class B
{
private:
int y;
public:
B()
{
y=20;
}

friend void show(A,B);
};

void show(A a,B b) //creating an object a of A class which is later used to call
{                  //the variable within this class
int r;
r=a.x+b.y;
cout<<"value of data of A:  "<<a.x<<endl;
cout<<"value of data of B:  "<<b.y<<endl;
cout<<"sum:  "<<r;
}

void main()
{
A obj1;
B obj2;
show(obj1,obj2);
getch();}

0 comments:

Post a Comment