類(lèi)的繼承
#include <iostream>
using namespace std;
// 基類(lèi)
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// 派生類(lèi)
class Rectangle : public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// 輸出對(duì)象的面積
cout << "Total area:" << Rect.getArea() << endl;
system("pause");
return 0;
}
結(jié)果:
result.png