1 #include <iostream>
2 using namespace std;
3 class BaseClass
4 {
5 public:
6 void show(){cout<<"Base class/n";}
7 };
8
9 class Derived1:public BaseClass
10 {
11 public:
12 void show(){cout<<"Derived1Class class/n";}
13 };
14
15 class Derived2:public BaseClass
16 {
17 public:
18 void show(){cout<<"Derived2 class/n";}
19 };
20
21 int main()
22 {
23 BaseClass bobj;
24 BaseClass *p;
25 Derived1 dobj1;
26 Derived2 dobj2;
27
28 p = &bobj;
29 p->show();
30
31 p = &dobj1;
32 p->show();
33 p = &dobj2;
34 p->show();
35 return 0;
36 }
运行结果:
Base class
Base class
Base class
~
2 using namespace std;
3 class BaseClass
4 {
5 public:
6 void show(){cout<<"Base class/n";}
7 };
8
9 class Derived1:public BaseClass
10 {
11 public:
12 void show(){cout<<"Derived1Class class/n";}
13 };
14
15 class Derived2:public BaseClass
16 {
17 public:
18 void show(){cout<<"Derived2 class/n";}
19 };
20
21 int main()
22 {
23 BaseClass bobj;
24 BaseClass *p;
25 Derived1 dobj1;
26 Derived2 dobj2;
27
28 p = &bobj;
29 p->show();
30
31 p = &dobj1;
32 p->show();
33 p = &dobj2;
34 p->show();
35 return 0;
36 }
运行结果:
Base class
Base class
Base class
~