File tree 1 file changed +7
-7
lines changed
practical_exercises/10_day_practice/day6/虚函数 1 file changed +7
-7
lines changed Original file line number Diff line number Diff line change 3
3
using namespace std ;
4
4
class A {
5
5
public:
6
- void f (int i){cout<<" …A " <<endl;};
6
+ void f (int i){cout<<" A::f() " <<endl;};
7
7
};
8
8
class B : public A {
9
9
public:
10
- virtual void f (int i){cout<<" …B " <<endl;}
10
+ virtual void f (int i){cout<<" B::f() " <<endl;}
11
11
};
12
12
class C : public B {
13
13
public:
14
- void f (int i){cout<<" …C " <<endl;}
14
+ void f (int i){cout<<" C::f() " <<endl;}
15
15
};
16
16
// 一旦将某个成员函数声明为虚函数后,它在继承体系中就永远为虚函数了
17
17
class D : public C {
18
18
public:
19
- void f (int ){cout<<" …D " <<endl;}
19
+ void f (int ){cout<<" D::f() " <<endl;}
20
20
};
21
21
int main (){
22
22
A *pA,a;
23
23
B *pB, b; C c; D d;
24
24
pA=&a; pA->f (1 ); // 调用A::f
25
- pB=&b; pB->f (1 ); // 调用A ::f
26
- pB=&c; pB->f (1 ); // 调用A ::f
27
- pB=&d; pB->f (1 ); // 调用A ::f
25
+ pB=&b; pB->f (1 ); // 调用B ::f
26
+ pB=&c; pB->f (1 ); // 调用C ::f
27
+ pB=&d; pB->f (1 ); // 调用D ::f
28
28
system (" pause" );
29
29
return 0 ;
30
30
}
You can’t perform that action at this time.
0 commit comments