淺析C++中的重載,隱藏和覆蓋
重載關(guān)系
一組函數(shù)要重載,必須處在同一個作用域中 ,而且函數(shù)名字相同,參數(shù)列表不同
代碼1中的Base中的 show() 和show(int) 屬于重載
代碼2中的Base中的 show() 和Derive中的show()不屬于重載不在同一個作用域下面
隱藏的關(guān)系(主要是指作用域隱藏)
在繼承結(jié)構(gòu)當(dāng)中,派生類的同名成員,把基類的同名成員給隱藏掉了
例如代碼2中的 Derive中的show() 和Base()中的show() ,show(int) 是隱藏關(guān)系
代碼1
class Base { public: Base(int data=10):ma(data){ cout<<"Base"<<endl; } void show(){cout<<"Base Show()"<<endl;} void show(int){cout<<"Base Show(int)"<<endl;} ~Base(){cout<<"~Base()"<<endl;} protected: int ma; }; class Derive : public Base { public: Derive(int data=20):Base(data),mb(data){ cout<<"Derive"<<endl; } ~Derive(){cout<<"~Derive()"<<endl;} private: int mb; }; int main(){ Derive d(20); d.show(); //正常調(diào)用基類show() d.show(100); //正常調(diào)用基類show(int) return 0; }
代碼2
class Base { public: Base(int data=10):ma(data){ cout<<"Base"<<endl; } void show(){cout<<"Base Show()"<<endl;} void show(int){cout<<"Base Show(int)"<<endl;} ~Base(){cout<<"~Base()"<<endl;} protected: int ma; }; class Derive : public Base { public: Derive(int data=20):Base(data),mb(data){ cout<<"Derive"<<endl; } void show(){cout<<"Derive Show()"<<endl;} ~Derive(){cout<<"~Derive()"<<endl;} private: int mb; }; int main(){ Derive d(20); d.show(); //調(diào)用子類show() d.show(100);//調(diào)用報錯 報錯信息 "Derive::show()函數(shù)不接受1個參數(shù)" // 即 Derive中的show()方法把Base中的show()和show(int)都給隱藏掉了 // 所以d.show()沒問題,調(diào)用的是派生類的show(),但是d.show(100)報錯了,因為 // 父類的show()和show(int)都被隱藏了,而派生類Derive中沒有 show(int)方法所以報錯了 // 如果想調(diào)用父類的show(int) 要這樣寫 d.Base.show(100); return 0; }
基類對象 -> 派生類對象
類型由上向下轉(zhuǎn) NOT OK
Base b(10);
Derive d(20);
d=b;// NOT OK
派生類對象 ->基類對象
類型由下向上轉(zhuǎn) OK
Base b(10);
Derive d(20);
b=d;//OK
派生類指針(引用) ->基類指針 類型由下向上轉(zhuǎn) OK
Base b(10);
Derive d(20);
Base *pb =&d;// OK 如下圖, 基類指針只能訪問到基類那一部分的成員,所以是安全的
代碼3
class Base { public: Base(int data=10):ma(data){ cout<<"Base"<<endl; } void show(){cout<<"Base Show()"<<endl;} void show(int){cout<<"Base Show(int)"<<endl;} ~Base(){cout<<"~Base()"<<endl;} protected: int ma; }; class Derive : public Base { public: Derive(int data=20):Base(data),mb(data){ cout<<"Derive"<<endl; } void show(){cout<<"Derive Show()"<<endl;} ~Derive(){cout<<"~Derive()"<<endl;} private: int mb; }; int main(){ Base b(10); Derive d(20); Base *pb =&d; pb->show(); //調(diào)用的是基類的 show pb->show(100);//調(diào)用的是基類的 show(int) ((Derive *)pb)->show(); //強轉(zhuǎn)后 調(diào)用的是派生類的 show }
基類指針(引用) -> 派生類對象 類型由上向下轉(zhuǎn) NOT OK
Base b(10);
Derive d(20);
Derive *pb =&b;// NOT OK pb指針能夠訪問的區(qū)域超過了實際對象b的內(nèi)存塊 ,危險訪問
代碼4
#include <iostream> using namespace std; class Base{ public: Base(){ cout<<"Base()"<<endl; } void show(){ cout<<"Base show()"<<endl; } void show(int x){ cout<<"Base show(int x)"<<endl; } ~Base(){ cout<<"~Base()"<<endl; } private: int ma; }; class Derive :public Base{ public: Derive(){ cout<<"Derive()"<<endl; } void show(){ cout<<"Derive show()"<<endl; } ~Derive(){ cout<<"~Derive()"<<endl; } private: int ma; }; int main(){ Derive d; Derive *pd=&d; d.show(); d.show(100); //編譯報錯, Derive 的void show()方法把Base 的void show() void show(int x) 都隱藏了 pd->show(100);//編譯報錯 Derive 的void show()方法把Base 的void show() void show(int x) 都隱藏了 return 0; }
代碼5
#include <iostream> using namespace std; class Base{ public: Base(){ cout<<"Base()"<<endl; } virtual void show(){ cout<<"Base show()"<<endl; } void show(int x){ cout<<"Base show(int x)"<<endl; } ~Base(){ cout<<"~Base()"<<endl; } private: int ma; }; class Derive :public Base{ public: Derive(){ cout<<"Derive()"<<endl; } virtual void show(){ cout<<"Derive show()"<<endl; } ~Derive(){ cout<<"~Derive()"<<endl; } private: int ma; }; int main(){ Derive *pd=new Derive(); pd->show(100); //編譯報錯,Derive 中的show() 函數(shù),只要名字與Base中有相同名字的函數(shù)的,就會隱藏掉Base中所有的show方法(不管加不加virtual),包括void show() void show(int x) return 0; }
到此這篇關(guān)于淺析C++中的重載,隱藏和覆蓋的文章就介紹到這了,更多相關(guān)C++重載 隱藏 覆蓋內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入學(xué)習(xí)C語言中memset()函數(shù)的用法
這篇文章主要介紹了深入學(xué)習(xí)C語言中memset()函數(shù)的用法,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-08-08C++中關(guān)于std::queue?中遇到釋放內(nèi)存錯誤的問題
這篇文章主要介紹了std::queue中遇到釋放內(nèi)存錯誤的問題,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07