- A friend is permitted full access to private and protected members.
- A friend can be a function.
- A friend can be an entire class. In this case all member functions of the class are given access.
EXAMPLE: Demonstrate the usage of friend function
#include <iostream>
using namespace std;
class Check {
int data;
public:
Check() {
data = 10;
}
friend void MyFunc(Check c);
};
void MyFunc(Check c) {
cout << c.data << endl;
}
void main()
{
Check c;
MyFunc(c);
}
OUTPUT:
10
EXAMPLE: Demonstrate the usage of friend classes#include <iostream>
using namespace std;
class One {
int data;
public:
One() {
data = 10;
}
friend class Two;
};
class Two {
public:
void AccessOne(One obj) {
cout << obj.data << endl;
}
};
void main()
{
One obj1;
Two obj2;
obj2.AccessOne(obj1);
}
OUTPUT:
10






0 comments:
Post a Comment