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?.