3

I have a parent class containing attribute declared as protected. I know protected members can be accessed within child class. But how the same can be accessed in grandchild class.

For e.g, How to access width in TooSmall class?

Consider this code example:

#include <iostream>
using namespace std;

class Box {
   protected:
      double width;
};

class SmallBox:Box {
   protected:
      double height;
};

class TooSmall:SmallBox {
    public:
        void setSmallWidth( double wid );
        void setHeight(double hei);
        double getSmallWidth( void );
        double getHeight(void);
};


double TooSmall::getSmallWidth(void) {
   return width ;
}

void TooSmall::setSmallWidth( double wid ) {
   width = wid;
}

void TooSmall::setHeight( double hei ) {
   height = hei;
}

double TooSmall::getHeight(void) {
   return height;
}

// Main function for the program
int main() {
   TooSmall box;

   box.setSmallWidth(5.0);
   box.setHeight(4.0);
   cout << "Width of box : "<< box.getSmallWidth() << endl;
   cout << "Height of box : "<< box.getHeight() << endl;

   return 0;
}

Is there a way to make parent class attribute public in child class?.

Abhinav Kinagi
  • 3,653
  • 2
  • 27
  • 43

1 Answers1

9

Your problem is that you're inheriting from you base classes privately, so public and protected members of the base class get the same access control as private members of the derived class. While possible, private inheritance is a very specific tool and used rarely. In the vast majority of cases, you want public inheritance:

class SmallBox: public Box {
   protected:
      double height;
};

class TooSmall: public SmallBox {
    public:
        void setSmallWidth( double wid );
        void setHeight(double hei);
        double getSmallWidth( void );
        double getHeight(void);
};

Done this way, protected members will be visible to all descendants (not just direct children) normally.


If, for some reason, you want to stick with private inheritance, you will have to "promote" the privately-inherited protected members back to protected:

class SmallBox:Box {
   protected:
      double height;
      using Box::width; // make it protected again
};

class TooSmall:SmallBox {
    public:
        void setSmallWidth( double wid );
        void setHeight(double hei);
        double getSmallWidth( void );
        double getHeight(void);
};
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    That works! But could it be done consdering private inheritence? – Abhinav Kinagi Jul 27 '19 at 16:10
  • 4
    @AbhinavKinagi Answer updated. However, please double-check that private inheritance is really what you want. Public inheritance represents the stnadard is-a relationship one normally expects when talking about inheritance. Protected/private inheritance, on the other hand, represents implemented-in-terms-of, and from certain points of view, it's closer to composition than to actual inheritance. – Angew is no longer proud of SO Jul 27 '19 at 16:26