继承

 1 #include <iostream>
 2  
 3 using namespace std;
 4  
 5 // 基类 Shape
 6 class Shape 
 7 {
 8    public:
 9       void setWidth(int w)
10       {
11          width = w;
12       }
13       void setHeight(int h)
14       {
15          height = h;
16       }
17    protected:
18       int width;
19       int height;
20 };
21  
22 // 基类 PaintCost
23 class PaintCost 
24 {
25    public:
26       int getCost(int area)
27       {
28          return area * 70;
29       }
30 };
31  
32 // 派生类
33 class Rectangle: public Shape, public PaintCost
34 {
35    public:
36       int getArea()
37       { 
38          return (width * height); 
39       }
40 };
41  
42 int main(void)
43 {
44    Rectangle Rect;
45    int area;
46  
47    Rect.setWidth(5);
48    Rect.setHeight(7);
49  
50    area = Rect.getArea();
51    
52    // 输出对象的面积
53    cout << "Total area: " << Rect.getArea() << endl;
54  
55    // 输出总花费
56    cout << "Total paint cost: $" << Rect.getCost(area) << endl;
57  
58    return 0;
59 }

 

posted @ 2023-02-17 10:25  迷人的危险~  阅读(14)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css