据我研究,派生类可以访问基类的受保护成员。在派生类中,基类的受保护成员在派生类中作为公共成员。但是当我实现这个时,我得到一个错误
\n我的代码:
\n\n#include <iostream>\n \nusing namespace std;\n\nclass Shape {\n protected :\n void setWidth(int w) {\n width = w;\n }\n void setHeight(int h) {\n height = h;\n }\n \n protected:\n int width;\n int height;\n};\n\n// Derived class\nclass Rectangle: public Shape {\n public:\n int getArea() { \n return (width * height); \n }\n};\n\nint main(void) {\n Rectangle Rect;\n \n Rect.setWidth(5);\n Rect.setHeight(7);\n\n // Print the area of the object.\n cout << "Total area: " << Rect.getArea() << endl;\n\n return 0;\n}\n\nRun Code Online (Sandbox Code Playgroud)\n …