Friend Class In C++
Friend Class A friend class can access
private and protected members of other class in which it is declared as
friend. It is sometimes useful to allow a particular class to access
private members of other class.
Fig: Friend Class In C++ |
Syntax:
class B;
class A
{
private:
data member;
public:
friend class B;
};
Program:
#include<iostream.h>
#include<conio.h>
class A
{
private:
int a;
public:
A()
{
a=10;
}
friend class B;
};
class B
{
private:
int b;
public:
void show(A & x)
{
cout<<”x.a=”<<x.a;
}
};
void main()
{
A
a;
B
b;
b.show(a);
getch();
}
Output:
x.a 10
No comments:
Post a Comment