更新時(shí)間:2018-12-14 來源:黑馬程序員技術(shù)社區(qū) 瀏覽量:
一、 C++成員函數(shù)的重載
C++中的成員函數(shù)有四種,分別是普通成員函數(shù),virtual虛函數(shù),const成員函數(shù)。
(1) void func(int a);(2) virtual void func(int a);(3) void func(int a) const;如果在一個(gè)類中,聲明這四種函數(shù),哪是重復(fù)定義?哪些是重載?
其中(1)(2)是重復(fù)定義,故編譯不能通過,而(3)與(1)(2)是不同類型的函數(shù),是重載。
成員函數(shù)被重載的特征是:
(1)具有相同的作用域(即同一個(gè)類定義中);(2)函數(shù)名字相同(3)參數(shù)類型,順序 或 數(shù)目不同(包括const參數(shù)和非const函數(shù))(4)virtual關(guān)鍵字可有可無。從成員函數(shù)的重載特征中,可以知道(1)(2)是重復(fù)定義。那么(3)為什么和(1)(2)不同呢?
因?yàn)轭愔械暮瘮?shù),都會自動(dòng)添加一個(gè)自身類指針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)發(fā)生重載,因?yàn)閰?shù)有一個(gè)const。
二 、C++成員函數(shù)的繼承#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);
運(yùn)行結(jié)果為:
覆蓋是指派生類重新實(shí)現(xiàn)(或者改寫)了基類的成員函數(shù),其特征是:
(1)不同的作用域(非別位于派生類和基類中);(2)函數(shù)名稱相同(3)參數(shù)列表完全相同;(4)基類函數(shù)必須是虛函數(shù)。從(4)中我們得知覆蓋只是針對虛函數(shù)的。
#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中重新定義了基類的虛成員函數(shù)virtual void g(int a);
四、C++成員函數(shù)的隱藏隱藏是指派生類的成員函數(shù)遮蔽了與其同名的基類成員函數(shù),具體規(guī)則如下:
(1) 派生類的函數(shù)與基類的函數(shù)同名,但是參數(shù)列表有所差異。此時(shí),不論有無virtual關(guān)鍵字,基類的函數(shù)在派生類中將被隱藏。(注意別與重載混合)
(2)派生類的函數(shù)與基類的函數(shù)同名,參數(shù)列表也相同,但是基類函數(shù)沒有virtual關(guān)鍵字。此時(shí),基類的函數(shù)在派生類中將被吟唱。(注意別與覆蓋混合)
判斷下面哪些函數(shù)是覆蓋,哪些函數(shù)是隱藏?
#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)都是隱藏。
運(yùn)行結(jié)果:
再看一個(gè)例子:
#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)函數(shù)。
所以在主函數(shù)中
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指針執(zhí)行f()函數(shù),都只執(zhí)行void Derived::f(double d)該函數(shù)。[html]Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); Base *pBase = new Derived();pBase->f(5);pBase->f(3.5);在調(diào)用pBase->f(5)時(shí),首先要去pBase類中找到對應(yīng)需要執(zhí)行的函數(shù),因?yàn)锽ase類中有兩個(gè)函數(shù)virtual void f(int a) 和 void f(double)重載,因?yàn)樵搶?shí)參是5,為int類型,所以要調(diào)用virtual void f(int a)函數(shù),因?yàn)樵揻(int a)是一個(gè)虛函數(shù),所以再去判斷pBase所指向的具體對象,具體對象為Derived子類,再去Derived子類的虛函數(shù)表中找到void f(int a)函數(shù)。因?yàn)镈erived子類繼承了父類Base的虛函數(shù)vitural void f(int a),所以輸出 virtual Base::f(int a);
在調(diào)用pBase->f(3.5)時(shí),首先要去pBase類中找到對應(yīng)需要執(zhí)行的函數(shù),因?yàn)橐驗(yàn)锽ase類中有兩個(gè)函數(shù)virtual void f(int a) 和 void f(double)重載,因?yàn)樵搶?shí)參是3.5,為double類,所以要調(diào)用void f(double d)函數(shù),因?yàn)樵摵瘮?shù)是一個(gè)普通成員函數(shù),故直接輸出。 void Base::f(double d);
再舉一個(gè)例子:
#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的虛函數(shù)virtual void f(int a),也隱藏了基類的virtual void f(int a), void f(double d)函數(shù)。
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);同理所有用子類對象或者子類指針來調(diào)用f()函數(shù)時(shí),都只執(zhí)行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類中尋找相應(yīng)的函數(shù),同理Base類中的兩個(gè)函數(shù)virtual void f(int a)和void f(double d)是重載函數(shù),因?yàn)閷?shí)參為5,為int類型,所以我們要調(diào)用virtual void f(int a)虛函數(shù),因?yàn)樵摵瘮?shù)是虛函數(shù),所以要去判斷pBase指向的具體對象,因?yàn)閜Base指向的是一個(gè)子類的對象,所以需要去子類的虛函數(shù)表中取找virtual void f(int a)函數(shù),找到之后,執(zhí)行該函數(shù),故輸出virtual Derived::f(int a)。
pBase->f(3.5),首先去Base類中尋找相應(yīng)的函數(shù),同理Base類中的兩個(gè)函數(shù)virtual void f(int a)和void f(double d)是重載函數(shù),因?yàn)閷?shí)參為3.5,為double類型,所以我們要調(diào)用void f(double d),因?yàn)樵摵瘮?shù)為普通成員函數(shù),故執(zhí)行輸出: void Base::f(double d);