Friend Class
2 posters
Unix C++ :: UNIX CPP :: CPP :: Example Programs
Page 1 of 1
Friend Class
- Code:
#include<iostream.h>
using namespace std;
class Sample
{
int a;
public:
Sample(int a)
{
this->a=a;
}
int getA()
{
return a;
}
friend class Test;
};
class Test
{
public:
Test(Sample* obj,int a)
{
obj->a=a;
}
};
int main()
{
Sample s(20);
Test t(&s,30);
cout<<"Changed value of a : "<<s.getA();
}
Test is a friend class of Sample. So Test can access private members of Sample. In this example Test access private member 'a' of Sample.
hii
I think a friend class can alos be used to access the protected member of the base class.
Vineet_More- Posts : 27
Points : 31
Join date : 2010-02-26
Re: Friend Class
yes.. friend class can access all the members of the class.
In this example "Test" can access private, protected and public members of "Sample"
In this example "Test" can access private, protected and public members of "Sample"
Unix C++ :: UNIX CPP :: CPP :: Example Programs
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum