Question #187810

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.



Expert's answer

#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;
}
LATEST TUTORIALS
APPROVED BY CLIENTS