C++ 类继承 子类调用父类

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class MyClass1
 5 {
 6     public:
 7         int a;
 8         void Show(bool bSwitch)
 9         {
10             cout << "a=" << a << endl;
11         }
12     private:
13         int b; 
14 };
15 
16 class MyClass2 : public MyClass1
17 {
18     public:
19         int c;
20         void Show()
21         {
22             cout << "c=" << c << endl;
23         }
24         
25         void Show(bool bSwitch) //调用父类重名函数,这里是推荐写法 
26         {
27             MyClass1::Show(bSwitch);
28         }
29     private:
30         int d;
31 };
32 
33 
34 int main(int argc, char *argv[])
35 {
36     MyClass1 a;
37     a.a = 10;
38     cout << "a.Show():";
39     a.Show(1);
40     
41     MyClass2 b;
42     b.a = 20;
43     b.c = 30;
44     cout << "b.Show():";
45     b.Show();
46     cout << "b.Show(1):";
47     b.Show(1); //这里是推荐写法 
48     cout << "b.MyClass1::Show(1):"; 
49     b.MyClass1::Show(1);//虽然可以实现,但是不推荐这种写法 
50     return 0;
51 }
原文地址:https://www.cnblogs.com/autumoonchina/p/3587231.html