我有一个包含protected
声明属性的父类。我知道protected
成员可以在子类中访问。但是如何在孙子类中访问相同的内容呢?
例如,如何在TooSmall
类中访问width
?
请考虑以下代码示例:
#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;
}
在子类中有办法将父类属性设置为public
吗?
class SmallBox: protected Box
和class TooSmall: protected SmallBox
。 - Eljay