C++中重载、覆盖(重写)和隐藏的区别

基本概念:

重载:是指同一可访问区内被声明的几个具有不同参数列(参数的类型,个数,顺序不同)的同名函数,根据参数列表确定调用哪个函数,重载不关心函数返回类型。

1 class A{
2 public:
3   void test(int i);
4   void test(double i);//overload
5   void test(int i, double j);//overload
6   void test(double i, int j);//overload
7   int test(int i);         //错误,非重载。注意重载不关心函数返回类型。
8 };

隐藏:是指派生类的函数屏蔽了与其同名的基类函数,注意只要同名函数,不管参数列表是否相同,基类函数都会被隐藏。

 1 #include "stdafx.h"
 2 #include "iostream"
 3 
 4 using namespace std;
 5 
 6 class Base
 7 {
 8 public:
 9     void fun(double ,int ){ cout << "Base::fun(double ,int )" << endl; }
10 };
11 
12 class Derive : public Base
13 {
14 public:
15     void fun(int ){ cout << "Derive::fun(int )" << endl; }
16 };
17 
18 int main()
19 {
20     Derive pd;
21     pd.fun(1);//Derive::fun(int )
22     pb.fun(0.01, 1);//error C2660: “Derive::fun”: 函数不接受 2 个参数
23 
24     Base *fd = &pd;
25     fd->fun(1.0,1);//Base::fun(double ,int);
26     fd->fun(1);//error 
27     system("pause");
28     return 0;
29 }

重写(覆盖):是指派生类中存在重新定义的函数。其函数名,参数列表,返回值类型,所有都必须同基类中被重写的函数一致。只有函数体不同(花括号内),派生类调用时会调用派生类的重写函数,不会调用被重写函数。重写的基类中被重写的函数必须有virtual修饰。

注:fd为基类的指针,这是调用fun是基类的函数

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 class Base
 6 {
 7 public:
 8     virtual void fun(int i){ cout << "Base::fun(int) : " << i << endl;}
 9 };
10 
11 class Derived : public Base
12 {
13 public:
14     virtual void fun(int i){ cout << "Derived::fun(int) : " << i << endl;}
15 };
16 int main()
17 {
18     Base b;
19     Base * pb = new Derived();
20     pb->fun(3);//Derived::fun(int)
21 
22     system("pause");
23     return 0;
24 }

重载和重写的区别:

(1)范围区别:重写和被重写的函数在不同的类中,重载和被重载的函数在同一类中。

(2)参数区别:重写与被重写的函数参数列表一定相同,重载和被重载的函数参数列表一定不同。

(3)virtual的区别:重写的基类必须要有virtual修饰,重载函数和被重载函数可以被virtual修饰,也可以没有。

隐藏和重写,重载的区别:

(1)与重载范围不同:隐藏函数和被隐藏函数在不同类中。

(2)参数的区别:隐藏函数和被隐藏函数参数列表可以相同,也可以不同,但函数名一定同;当参数不同时,无论基类中的函数是否被virtual修饰,基类函数都是被隐藏,而不是被重写。 

 1 #include "stdafx.h"
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 class Base
 7 {
 8 public:
 9     virtual void f(float x){ cout << "Base::f(float) " << x << endl; }
10     void g(float x){ cout << "Base::g(float) " << x << endl; }
11     void h(float x){ cout << "Base::h(float) " << x << endl; }
12 };
13 
14 class Derived : public Base
15 {
16 public:
17     virtual void f(float x){ cout << "Derived::f(float) " << x << endl; }
18     void g(int x){ cout << "Derived::g(int) " << x << endl; }
19     void h(float x){ cout << "Derived::h(float) " << x << endl; }
20 };
21 
22 int main(void)
23 {
24     Derived d;
25     Base *pb = &d;
26     Derived *fd = &d;
27     // Good : behavior depends solely on type of the object
28     pb->f(3.14f); //Derived::f(float) 3.14
29     fd->f(3.14f); //Derived::f(float) 3.14
30 
31     // Bad : behavior depends on type of the pointer
32     pb->g(3.14f); //Base::g(float) 3.14
33     fd->g(3.14f); //Derived::g(int) 3
34 
35     // Bad : behavior depends on type of the pointer
36     pb->h(3.14f); //Base::h(float) 3.14
37     fd->h(3.14f); //Derived::h(float) 3.14
38 
39     system("pause");
40     return 0;
41 }

(1)函数Derived::f(float)覆盖了Base::f(float)。

(2)函数Derived::g(int)隐藏了Base::g(float),而不是重载。

(3)函数Derived::h(float)隐藏了Base::h(float),而不是覆盖。

 

猜你喜欢

转载自blog.csdn.net/qq_36553031/article/details/88547031