C++中重載、重寫(覆蓋)和隱藏的區別


轉載自:https://blog.csdn.net/zx3517288/article/details/48976097

基本概念:

重載:是指同一可訪問區內被聲明的幾個具有不同參數列(參數的類型,個數,順序不同)的同名函數,根據參數列表確定調用哪個函數,重載不關心函數返回類型。

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),而不是覆蓋。

 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM