Friend Class
Program in C Plus Plus
Today
we are going to develop a program in C++ of friend class. You have learn the
concept of friend function which is made the friend of both classes and both
can use it.
Now
the concept is of the Friend class
Friend Class is class
which has the full authority over the other class. It can access the public and
protected data of its friend class also the private data too.
Friend
keyword is used to declare one class the friend of other class.
Scenario
Scenario
is quite simple, we have to print the private data of class A, with the object
and functions of class B. And class A is the friend of class B.
Program in
C++ of Friend Class
Program Code
//
softoset.blogspot.com
#include<iostream>
using
namespace std;
class
A{
private:
int a,b;
public:
A()
{
a=10;
b=40;
}
friend class B;
};
class
B{
public:
void show(A x)
{
cout<<"X =
"<<x.a<<endl;
}
void showb(A x)
{
cout<<"Y =
"<<x.b<<endl;
}
};
int main()
{
A a;
B
b;
b.show(a);
b.showb(a);
return
0;
}
Program in
C++ of Friend Class
Program Output
You
can see the output below
You
can see that the private data of class A is accessed by the object and
functions of class B. So, it is proved that the friend class access the all
data of its friend.
Thank
You for becoming the Best Friend of the
SoftOSet
Reset
You Technology
Be
Soft and Simple.
Post a Comment
Feel Free to Comment Us