您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++成員函數的重載,繼承,覆蓋和隱藏是什么”,在日常操作中,相信很多人在C++成員函數的重載,繼承,覆蓋和隱藏是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++成員函數的重載,繼承,覆蓋和隱藏是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
一、 C++成員函數的重載
C++中的成員函數有四種,分別是普通成員函數,virtual虛函數,const成員函數。
(1) void func(int a);
(2) virtual void func(int a);
(3) void func(int a) const;
如果在一個類中,聲明這四種函數,哪是重復定義?哪些是重載?
其中(1)(2)是重復定義,故編譯不能通過,而(3)與(1)(2)是不同類型的函數,是重載。
成員函數被重載的特征是:
(1)具有相同的作用域(即同一個類定義中);
(2)函數名字相同
(3)參數類型,順序 或 數目不同(包括const參數和非const函數)
(4)virtual關鍵字可有可無。
從成員函數的重載特征中,可以知道(1)(2)是重復定義。那么(3)為什么和(1)(2)不同呢?
因為類中的函數,都會自動添加一個自身類指針this,所以
void func(int a) ==== void func(Base * this, int a) virtual func(int a) ==== virtual func(Base *this, int a) void func(int a)const === void func(const Base *this, int a) const
所以(3)可以與(1)(2)發生重載,因為參數有一個const。
二 、C++成員函數的繼承
#include <iostream> using namespace std; class Base { public: void f(int a){ cout << "Base::f(int a)" << endl; } virtual void g(int a) { cout << "virtual Base::g(int a)" << endl; } }; class Derived : public Base { public: void h(int a) { cout << "Derivd::h(int a)" << endl; } }; int main() { Base b; b.f(3); b.g(4); Derived d; d.f(3); d.g(4); d.h(3); } #include <iostream> using namespace std; class Base { public: void f(int a){ cout << "Base::f(int a)" << endl; } virtual void g(int a) { cout << "virtual Base::g(int a)" << endl; } }; class Derived : public Base { public: void h(int a) { cout << "Derivd::h(int a)" << endl; } }; int main() { Base b; b.f(3); b.g(4); Derived d; d.f(3); d.g(4); d.h(3); }
Base b的對象模型:
Derived d的對象模型:
則在子類Derived d中繼承了父類中的virtual void g(int a) ; void f(int a);
運行結果為:
三、C++成員函數的覆蓋
覆蓋是指派生類重新實現(或者改寫)了基類的成員函數,其特征是:
(1)不同的作用域(非別位于派生類和基類中);
(2)函數名稱相同
(3)參數列表完全相同;
(4)基類函數必須是虛函數。
從(4)中我們得知覆蓋只是針對虛函數的。
#include <iostream> using namespace std; class Base { public: void f(int a){ cout << "Base::f(int a)" << endl; } virtual void g(int a) { cout << "virtual Base::g(int a)" << endl; } }; class Derived : public Base { public: void h(int a) { cout << "Derivd::h(int a)" << endl; } virtual void g(int a) { cout << "virtual Derived::g(int a)" << endl; } }; int main() { Base b; b.f(3); b.g(4); Derived d; d.f(3); d.g(4); d.h(3); } #include <iostream> using namespace std; class Base { public: void f(int a){ cout << "Base::f(int a)" << endl; } virtual void g(int a) { cout << "virtual Base::g(int a)" << endl; } }; class Derived : public Base { public: void h(int a) { cout << "Derivd::h(int a)" << endl; } virtual void g(int a) { cout << "virtual Derived::g(int a)" << endl; } }; int main() { Base b; b.f(3); b.g(4); Derived d; d.f(3); d.g(4); d.h(3); }
Derived d對象模型如下:
其中Derived中重新定義了基類的虛成員函數virtual void g(int a);
四、C++成員函數的隱藏
隱藏是指派生類的成員函數遮蔽了與其同名的基類成員函數,具體規則如下:
(1) 派生類的函數與基類的函數同名,但是參數列表有所差異。此時,不論有無virtual關鍵字,基類的函數在派生類中將被隱藏。(注意別與重載混合)
(2)派生類的函數與基類的函數同名,參數列表也相同,但是基類函數沒有virtual關鍵字。此時,基類的函數在派生類中將被吟唱。(注意別與覆蓋混合)
判斷下面哪些函數是覆蓋,哪些函數是隱藏?
#include <iostream> using namespace std; class Base { public: virtual void f(float x) { cout << "virtual Base::f(float) " << x << endl; } void g(float x) { cout << "Base::g(float) " << x << endl; } void h(float x) { cout << "Base::h(float) " << x << endl; } }; class Derived : public Base{ public: virtual void f(float x) { cout << "virtual Derived::f(float) " << x << endl; } void g(int x) { cout << "Derived::g(int) " << x << endl; } void h(float x) { cout << "Derived::h(float) " << x << endl; } }; int main(void) { Derived d; Base *pb = &d; Derived *pd = &d; pb->f(3.14f); pd->f(3.14f); pb->g(3.14f); pd->g(3.14f); pb->h(3.14f); pd->h(3.14f); } #include <iostream> using namespace std; class Base { public: virtual void f(float x) { cout << "virtual Base::f(float) " << x << endl; } void g(float x) { cout << "Base::g(float) " << x << endl; } void h(float x) { cout << "Base::h(float) " << x << endl; } }; class Derived : public Base{ public: virtual void f(float x) { cout << "virtual Derived::f(float) " << x << endl; } void g(int x) { cout << "Derived::g(int) " << x << endl; } void h(float x) { cout << "Derived::h(float) " << x << endl; } }; int main(void) { Derived d; Base *pb = &d; Derived *pd = &d; pb->f(3.14f); pd->f(3.14f); pb->g(3.14f); pd->g(3.14f); pb->h(3.14f); pd->h(3.14f); }
其中子類Derived中 vitual void f(float x) 是覆蓋,而void g(int x) 和void h(float x)都是隱藏。
運行結果:
再看一個例子:
#include <iostream> using namespace std; class Base { public: virtual void f(int a) { cout << "virtual Base::f(int a)" << endl; } void f(double d) { cout << "Base::f(double d)" << endl; } }; class Derived : public Base { public: void f(double d) { cout << "Derivd::f(double d)" << endl; } }; int main() { Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Base b; b.f(5); b.f(3.5); Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); } #include <iostream> using namespace std; class Base { public: virtual void f(int a) { cout << "virtual Base::f(int a)" << endl; } void f(double d) { cout << "Base::f(double d)" << endl; } }; class Derived : public Base { public: void f(double d) { cout << "Derivd::f(double d)" << endl; } }; int main() { Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Base b; b.f(5); b.f(3.5); Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); }
其中父類中的void f(double d)隱藏了子類的virtual void f(int a), void f(double d)函數。
所以在主函數中
Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5);只要通過Derived對象或者Derived指針執行f()函數,都只執行void Derived::f(double d)該函數。 [html] Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5);
在調用pBase->f(5)時,首先要去pBase類中找到對應需要執行的函數,因為Base類中有兩個函數virtual void f(int a) 和 void f(double)重載,因為該實參是5,為int類型,所以要調用virtual void f(int a)函數,因為該f(int a)是一個虛函數,所以再去判斷pBase所指向的具體對象,具體對象為Derived子類,再去Derived子類的虛函數表中找到void f(int a)函數。因為Derived子類繼承了父類Base的虛函數vitural void f(int a),所以輸出 virtual Base::f(int a);
在調用pBase->f(3.5)時,首先要去pBase類中找到對應需要執行的函數,因為因為Base類中有兩個函數virtual void f(int a) 和 void f(double)重載,因為該實參是3.5,為double類,所以要調用void f(double d)函數,因為該函數是一個普通成員函數,故直接輸出。 void Base::f(double d);
再舉一個例子:
#include <iostream> using namespace std; class Base { public: virtual void f(int a) { cout << "virtual Base::f(int a)" << endl; } void f(double d) { cout << "Base::f(double d)" << endl; } }; class Derived : public Base { public: void f(int a) { cout << "virtual Derived::f(int a)" << endl; } }; int main() { Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Base b; b.f(5); b.f(3.5); Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); } #include <iostream> using namespace std; class Base { public: virtual void f(int a) { cout << "virtual Base::f(int a)" << endl; } void f(double d) { cout << "Base::f(double d)" << endl; } }; class Derived : public Base { public: void f(int a) { cout << "virtual Derived::f(int a)" << endl; } }; int main() { Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Base b; b.f(5); b.f(3.5); Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); }
子類Derived中的void f(int a)既覆蓋了基類Base的虛函數virtual void f(int a),也隱藏了基類的virtual void f(int a), void f(double d)函數。
Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5);
同理所有用子類對象或者子類指針來調用f()函數時,都只執行virtual void f(int a),輸出virtual Derived::f(int a)
view plaincopyprint?Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5);
pBase->f(5),首先去Base類中尋找相應的函數,同理Base類中的兩個函數virtual void f(int a)和void f(double d)是重載函數,因為實參為5,為int類型,所以我們要調用virtual void f(int a)虛函數,因為該函數是虛函數,所以要去判斷pBase指向的具體對象,因為pBase指向的是一個子類的對象,所以需要去子類的虛函數表中取找virtual void f(int a)函數,找到之后,執行該函數,故輸出virtual Derived::f(int a)。
pBase->f(3.5),首先去Base類中尋找相應的函數,同理Base類中的兩個函數virtual void f(int a)和void f(double d)是重載函數,因為實參為3.5,為double類型,所以我們要調用void f(double d),因為該函數為普通成員函數,故執行輸出: void Base::f(double d);
到此,關于“C++成員函數的重載,繼承,覆蓋和隱藏是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。