淺析C++中的重載,隱藏和覆蓋
重載關(guān)系
一組函數(shù)要重載,必須處在同一個作用域中 ,而且函數(shù)名字相同,參數(shù)列表不同
代碼1中的Base中的 show() 和show(int) 屬于重載
代碼2中的Base中的 show() 和Derive中的show()不屬于重載不在同一個作用域下面
隱藏的關(guān)系(主要是指作用域隱藏)
在繼承結(jié)構(gòu)當中,派生類的同名成員,把基類的同名成員給隱藏掉了
例如代碼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)文章
C++實現(xiàn)LeetCode(158.用Read4來讀取N個字符之二 - 多次調(diào)用)
這篇文章主要介紹了C++實現(xiàn)LeetCode(158.用Read4來讀取N個字符之二 - 多次調(diào)用),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
深入C++拷貝構(gòu)造函數(shù)的總結(jié)詳解
本篇文章是對C++中拷貝構(gòu)造函數(shù)進行了總結(jié)與介紹。需要的朋友參考下2013-05-05
C++利用stringstream進行數(shù)據(jù)類型轉(zhuǎn)換實例
這篇文章主要介紹了C++利用stringstream進行數(shù)據(jù)類型轉(zhuǎn)換的方法,實例分析了使用stringstream進行string轉(zhuǎn)int的操作技巧,需要的朋友可以參考下2015-01-01
Qt數(shù)據(jù)庫應(yīng)用之實現(xiàn)通用數(shù)據(jù)庫分頁
數(shù)據(jù)庫分頁展示,在所有的涉及到數(shù)據(jù)庫記錄的項目中都是需要的。本文將利用Qt實現(xiàn)通用數(shù)據(jù)庫的分頁展示,感興趣的小伙伴可以跟隨小編學習一下2022-02-02
C++非繼承時函數(shù)成員訪問屬性和類繼承過程中的訪問控制
這篇文章主要介紹了C++非繼承時函數(shù)成員訪問屬性和類繼承過程中的訪問控制,非繼承時,protected成員和private成員沒有任何區(qū)別,都是類內(nèi)部可以直接訪問它們、類外部的類對象不可訪問它們、類內(nèi)部的類對象可以訪問它們,更多詳細內(nèi)容請參考下面相關(guān)資料2022-03-03

