#include <iostream> using namespace std; class shape { public: void setHigh(double _high) { high = _high; } void setWidth(double _width) { width = _width; } public: double high; double width; }; class cost { public: int getCost(double area) { return area * 2; } }; class rectangle : public shape { public: double getarea() { return high * width; } }; class rectangleCost : public shape, public cost { public: double getarea() { return high * width; } }; int main () { rectangle rect; rect.setHigh(2); rect.setWidth(4); double area = rect.getarea(); cout << "面积:" << area << endl; rectangleCost rectCost; rectCost.setHigh(2); rectCost.setWidth(5); double area_c = rectCost.getarea(); double cost = rectCost.getCost(area_c); cout << "面积 :" << area_c << endl; cout << "花费 :" << cost << endl; return 0; }
输出结果:
面积:8 面积 :10 花费 :20