😄C++ -- 类的继承
类的继承
#include <iostream>
using namespace std;
// 基类
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// 派生类
class Rectangle : public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// 输出对象的面积
cout << "Total area:" << Rect.getArea() << endl;
system("pause");
return 0;
}
结果:
result.png
本文标题:😄C++ -- 类的继承
本文链接:https://www.haomeiwen.com/subject/mhvctctx.html
网友评论