C++虚基类 类接口应用示例程序

 1 #include <string>
 2 
 3 using namespace std;
 4 
 5 struct Sres
 6 {
 7     string content;
 8     int value;
 9 };
10 
11 class BaseABC
12 {
13 public:
14     BaseABC(string name = "base"):
15         name(name)
16     { }
17     BaseABC()
18     { }
19     ~BaseABC()
20     { }
21     string name;
22     virtual Sres Skill() const = 0;
23 };
24 
25 class My:public BaseABC
26 {
27 public:
28     My(string name) :
29         BaseABC(name)
30     {
31     
32     }
33     virtual Sres Skill() const;
34 };
35 Sres My::Skill() const
36 {
37     return {"my",100};
38 }
39 
40 class Son :public BaseABC
41 {
42 public:
43     Son(string name) :
44         BaseABC(name)
45     {
46 
47     }
48     virtual Sres Skill() const;
49 };
50 Sres Son::Skill() const
51 {
52     return { "son", 80 };
53 }
54 
55 void Show(const BaseABC& abc)
56 {
57     auto tmp = abc.Skill();
58     cout << abc.name << ":" << tmp.content + "  " << tmp.value << endl;
59 }
60 
61 int main()
62 {
63     My aa(string("cxl"));
64     Son bb(string("son"));
65     Show(aa);
66     Show(bb);
67 }

 

posted @ 2022-04-13 11:07  小小林林  阅读(49)  评论(0编辑  收藏  举报