类继承
★ 对于父类函数(virtual、非virtual),如果子类没有同名函数,则正常继承
★ 对于父类函数(virtual、非virtual),如果子类有同名函数,无同型函数,则不能调用父类函数
★ 对于父类函数(virtual、非virtual),如果有同型函数:
----非virtual函数由指针类型决定调用哪个
----virtual函数由指针指向的对象决定调用哪个(运行时决定)
1//如果对于父类函数(virtual/非virtual),如果子类没有同名函数,则正常继承
2
3class Base
4
5{
6
7public: void func(int i){ cout <<"Base::func(int)"<< endl; }
8
9};
10
11
12
13class Derived : public Base
14
15{ };
16
17
18
19int main()
20
21{
22
23 Base *pb = new Derived();
24
25 pb->func(1); //Base::func(int)
26
27 delete pb;
28
29
30
31 Derived *pd = new Derived();
32
33 pd->func(1); //Base::func(int)
34
35 delete pd;
36
37}
2
3class Base
4
5{
6
7public: void func(int i){ cout <<"Base::func(int)"<< endl; }
8
9};
10
11
12
13class Derived : public Base
14
15{ };
16
17
18
19int main()
20
21{
22
23 Base *pb = new Derived();
24
25 pb->func(1); //Base::func(int)
26
27 delete pb;
28
29
30
31 Derived *pd = new Derived();
32
33 pd->func(1); //Base::func(int)
34
35 delete pd;
36
37}
1//对于父类函数(virtual、非virutal),子类有同名函数,无同型函数,则不能调用父类函数
2
3class Base
4
5{
6
7public:
8
9 void func(int i){ cout <<"Base::func(int i)"<< endl; }
10
11 virtual void func2(int i) { cout << "Base::func2(int i)" << endl;}
12
13};
14
15
16
17class Derived : public Base
18
19{
20
21public:
22
23 void func(){ cout <<"Derived::func()"<< endl; }
24
25 void func2(){ cout <<"Derived::func2()"<< endl; }
26
27};
28
29
30
31int main()
32
33{
34
35 Base *pb = new Derived();
36
37 pb->func(1); //Base::func(int)
38
39 pb->func2(1); //Base::func2(int i)
40
41 delete pb;
42
43
44
45 Derived *pd = new Derived();
46
47 pd->func(); //Derived::func()
48
49 pd->func2(); //Derived::func2()
50
51 // pd->func2(1); //不能调用
52
53 delete pd;
54
55}
2
3class Base
4
5{
6
7public:
8
9 void func(int i){ cout <<"Base::func(int i)"<< endl; }
10
11 virtual void func2(int i) { cout << "Base::func2(int i)" << endl;}
12
13};
14
15
16
17class Derived : public Base
18
19{
20
21public:
22
23 void func(){ cout <<"Derived::func()"<< endl; }
24
25 void func2(){ cout <<"Derived::func2()"<< endl; }
26
27};
28
29
30
31int main()
32
33{
34
35 Base *pb = new Derived();
36
37 pb->func(1); //Base::func(int)
38
39 pb->func2(1); //Base::func2(int i)
40
41 delete pb;
42
43
44
45 Derived *pd = new Derived();
46
47 pd->func(); //Derived::func()
48
49 pd->func2(); //Derived::func2()
50
51 // pd->func2(1); //不能调用
52
53 delete pd;
54
55}
1//对于父类函数(virtual、非virtual),如果有同型函数:
2
3//----非virtual函数由指针类型决定调用哪个
4
5//----virtual函数由指针指向的对象决定调用哪个(运行时决定)
6
7class Base
8
9{ public:
10
11 void func(int i){ cout <<"Base::func(int i)"<< endl; }
12
13 void func() {cout << "Base::func() " << endl;}
14
15 virtual void func2(int i) { cout << "Base::func2(int i)" << endl;}
16
17};
18
19
20
21class Derived : public Base
22
23{ public:
24
25 void func(int i){ cout <<"Derived::func()"<< endl; }
26
27 void func2(int i){ cout <<"Derived::func2(int i)"<< endl; }
28
29};
30
31
32
33int main()
34
35{
36
37 Base *pb = new Derived();
38
39 pb->func(1); //Base::func(int i)
40
41 pb->func(); //Base:func()
42
43 pb->func2(1); //Derived::func2(int i)
44
45 delete pb;
46
47
48
49 Derived *pd = new Derived();
50
51 pd->func(1); //Derived::func(int i)
52
53 // pd->func(); //不能调用
54
55 pd->func2(1); //Derived::func2(int i)
56
57 delete pd;
58
59}
2
3//----非virtual函数由指针类型决定调用哪个
4
5//----virtual函数由指针指向的对象决定调用哪个(运行时决定)
6
7class Base
8
9{ public:
10
11 void func(int i){ cout <<"Base::func(int i)"<< endl; }
12
13 void func() {cout << "Base::func() " << endl;}
14
15 virtual void func2(int i) { cout << "Base::func2(int i)" << endl;}
16
17};
18
19
20
21class Derived : public Base
22
23{ public:
24
25 void func(int i){ cout <<"Derived::func()"<< endl; }
26
27 void func2(int i){ cout <<"Derived::func2(int i)"<< endl; }
28
29};
30
31
32
33int main()
34
35{
36
37 Base *pb = new Derived();
38
39 pb->func(1); //Base::func(int i)
40
41 pb->func(); //Base:func()
42
43 pb->func2(1); //Derived::func2(int i)
44
45 delete pb;
46
47
48
49 Derived *pd = new Derived();
50
51 pd->func(1); //Derived::func(int i)
52
53 // pd->func(); //不能调用
54
55 pd->func2(1); //Derived::func2(int i)
56
57 delete pd;
58
59}