C++中的简单继承

Father.cpp:

 1 #include<iostream>
 2 using namespace std;
 3 class Father
 4 {
 5 protected:
 6     int width;
 7     int height;
 8 public:
 9     void setWidth(int widths)
10     {
11         width = widths;
12     }
13     void setHeight(int heights)
14     {
15         height = heights;
16     }
17 };
18 
19 class Son : public Father
20 {
21 private:
22     int weight;
23 public:
24     void setWeight(int weights)
25     {
26         weight = weights;
27     }
28     void show()
29     {
30     cout<<"width ="<<width<<"	"<<"height="<<height<<"	"<<"weight="<<weight<<endl;
31     }
32 };
33 
34 int main()
35 {
36     Son a;
37     a.setWeight(100);
38     a.setHeight(180);
39     a.setWidth(70);
40     a.show();
41     return 0;
42 } 

运行结果:

原文地址:https://www.cnblogs.com/leihupqrst/p/3269600.html