小编Tus*_*ain的帖子

我们可以在派生类中访问基类的受保护成员函数吗?

据我研究,派生类可以访问基类的受保护成员。在派生类中,基类的受保护成员在派生类中作为公共成员。但是当我实现这个时,我得到一个错误

\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\n
Run Code Online (Sandbox Code Playgroud)\n …

c++ inheritance derived radix

0
推荐指数
1
解决办法
1678
查看次数

标签 统计

c++ ×1

derived ×1

inheritance ×1

radix ×1