1 Answer
Smita Kapse

In C++, the friendship is not inherited. It means that, if one parent class has some friend functions, then the child class will not get them as friend.

In this example it will generate an error because the display() function is friend of MyBaseClass but not the friend of MyDerivedClass. The display() can access the private member of MyBaseClass.


#include <iostream>
using namespace std;
class MyBaseClass {
      int x;
      MyBaseClass() {
         x = 20;
      friend void display();
class MyDerivedClass : public MyBaseClass {
      int y;
      MyDerivedClass() {
         x = 40;
void display() {
   MyDerivedClass derived;
   cout << "The value of private member of Base class is: " << derived.x << endl;
   cout << "The value of private member of Derived class is: " << derived.y << endl;
main() {


[Error] 'int MyDerivedClass::y' is private
[Error] within this context


We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.