#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 PaintCost{
public:
int getCost(int area){
return area * 70;
}
};
class Rectangle:public Shape,public PaintCost
{
public:
int getArea(){
return width * height;
}
};
int main() {
Rectangle Rect;
int area;
Rect.setHeight(4);
Rect.setWidth(5);
area = Rect.getArea();
cout << "Total area:" << area << endl;
cout << "Total paint cost:" << Rect.getCost(area) << endl;
return 0;
}
2017.8.1
学了类的继承,用教程上的例子写了下。
感觉继承后可以在一个类中直接用其他类的功能,就像直接在一个平台上调用所有的功能一样,很方便,表述也很清晰。
程序来自c++教程 http://www.runoob.com/cplusplus/cpp-inheritance.html
感谢提供