Sunday 3 February 2013

friend class in c++


#include<iostream.h>
#include<conio.h>

//class B;   here class b declaration is not necessary b/c in class A,
            //we are friending class not a function
class A
{
private:
int a,b;
public:

A()
{
a=10;
b=20;
}

friend class B;
};

class B
{
public:
void show1(A obj)
{
cout<<"value of a = "<<obj.a;
}

void show2(A obj)
{
cout<<"value of b = "<<obj.b;    //if u again use friend in class b then it will
}                                //give error.
};

void main(void)  //write (void) or not but it means that the function is not
                 //taking any argument and nor returning any value
{
A x;            //creating an object x of class A
B y;
y.show1(x);
y.show2(x);

getch();
}

0 comments:

Post a Comment