2 using namespace std;
3
4 class A{
5 public:
6 A(){
7 cout << "1" << endl;
8 }
9
10 void func1(){
11 cout << "2" << endl;
12 }
13
14 virtual void func2(){
15 cout << "3" << endl;
16 }
17
18 ~A(){
19 cout << "4" << endl;
20 }
21
22
23 };
24
25 class B:A{
26 public:
27 B(){
28 cout << "5" << endl;
29 }
30
31 void func1(){
32 cout << "6" << endl;
33 }
34
35 virtual void func2(){
36 cout << "7" << endl;
37 }
38
39 ~B(){
40 cout << "8" << endl;
41 }
42
43 };
1 #include iostream
2 #include "my-header.h"
3
4 int main(void){
5
6 A *ptr;
7
8 B *my_B = new B();//1 5
9
10 ptr = (A*)my_B;
11
12 ptr->func1();//2
13 ptr->func2();//7
14
15 my_B->func1();//6
16 my_B->func2();//7
17
18 delete my_B;//8 4
19
20 return 0;
21 }
沒有留言:
張貼留言