Answer to Question #187810 in C++ for Salman khan

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.



1
Expert's answer
2021-05-01T14:36:41-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS