What if two classes for example “Human and Hospital” are friend classes, which
member functions of these classes have access to the private and protected members of each
class. Write a program in C++ to differentiate these classes.
#include <iostream>
class Human {
private:
int a;
public:
Human() { a = 0; }
friend class Hospital; // Friend Class
};
class Hospital {
private:
int b;
public:
void showHuman(Human& x)
{
std::cout << "Human::a=" << x.a;
}
};
int main()
{
Human a;
Hospital b;
b.showHuman(a);
return 0;
}
Comments
Leave a comment