C++ 的三種訪問(wèn)權(quán)限與三種繼承方式
三種訪問(wèn)權(quán)限
我們知道C++中的類,有三種訪問(wèn)權(quán)限(也稱作訪問(wèn)控制),它們分別是public、protected、private。要理解它們其實(shí)也很容易,看下面了一個(gè)例子。
父類:
class Person { public: Person(const string& name, int age) : m_name(name), m_age(age) { } void ShowInfo() { cout << "姓名:" << m_name << endl; cout << "年齡:" << m_age << endl; } protected: string m_name; //姓名 private: int m_age; //年齡 }; class Person { public: Person(const string& name, int age) : m_name(name), m_age(age) { } void ShowInfo() { cout << "姓名:" << m_name << endl; cout << "年齡:" << m_age << endl; } protected: string m_name; //姓名 private: int m_age; //年齡 };
子類:
class Teacher : public Person { public: Teacher(const string& name, int age, const string& title) : Person(name, age), m_title(title) { } void ShowTeacherInfo() { ShowInfo(); //正確,public屬性子類可見(jiàn) cout << "姓名:" << m_name << endl; //正確,protected屬性子類可見(jiàn) cout << "年齡:" << m_age << endl; //錯(cuò)誤,private屬性子類不可見(jiàn) cout << "職稱:" << m_title << endl; //正確,本類中可見(jiàn)自己的所有成員 } private: string m_title; //職稱 }; class Teacher : public Person { public: Teacher(const string& name, int age, const string& title) : Person(name, age), m_title(title) { } void ShowTeacherInfo() { ShowInfo(); //正確,public屬性子類可見(jiàn) cout << "姓名:" << m_name << endl; //正確,protected屬性子類可見(jiàn) cout << "年齡:" << m_age << endl; //錯(cuò)誤,private屬性子類不可見(jiàn) cout << "職稱:" << m_title << endl; //正確,本類中可見(jiàn)自己的所有成員 } private: string m_title; //職稱 };
調(diào)用方法:
void test() { Person person("張三", 22); person.ShowInfo(); //public屬性,對(duì)外部可見(jiàn) cout << person.m_name << endl; //protected屬性,對(duì)外部不可見(jiàn) cout << person.m_age << endl; //private屬性,對(duì)外部不可見(jiàn) } void test() { Person person("張三", 22); person.ShowInfo(); //public屬性,對(duì)外部可見(jiàn) cout << person.m_name << endl; //protected屬性,對(duì)外部不可見(jiàn) cout << person.m_age << endl; //private屬性,對(duì)外部不可見(jiàn) }
總結(jié)
我們對(duì)C++類三種方式控制權(quán)限總結(jié)如下,這與Java中的三種對(duì)應(yīng)的訪問(wèn)權(quán)限是一樣的。
qq%e6%88%aa%e5%9b%be20161104113813
三種繼承方式
C++中繼承的方式還有多種,也分別都用public、protected、private表示。這與Java不一樣,Java只有繼承的概念,默認(rèn)是public繼承的。
1. 三種繼承方式不影響子類對(duì)父類的訪問(wèn)權(quán)限,子類對(duì)父類只看父類的訪問(wèn)控制權(quán)。
如下面三種繼承方式都能訪問(wèn)父類中的public和protected成員。
class Teacher : /*public*/ /*protected*/ private Person { public: Teacher(const string& name, int age, const string& title) : Person(name, age), m_title(title) { } void ShowTeacherInfo() { ShowInfo(); //正確,public屬性子類可見(jiàn) cout << "姓名:" << m_name << endl; //正確,protected屬性子類可見(jiàn) //cout << "年齡:" << m_age << endl; //錯(cuò)誤,private屬性子類不可見(jiàn) cout << "職稱:" << m_title << endl; //正確,本類中可見(jiàn)自己的所有成員 } private: string m_title; //職稱 }; class Teacher : /*public*/ /*protected*/ private Person { public: Teacher(const string& name, int age, const string& title) : Person(name, age), m_title(title) { } void ShowTeacherInfo() { ShowInfo(); //正確,public屬性子類可見(jiàn) cout << "姓名:" << m_name << endl; //正確,protected屬性子類可見(jiàn) //cout << "年齡:" << m_age << endl; //錯(cuò)誤,private屬性子類不可見(jiàn) cout << "職稱:" << m_title << endl; //正確,本類中可見(jiàn)自己的所有成員 } private: string m_title; //職稱 };
2. 繼承方式是為了控制子類(也稱派生類)的調(diào)用方(也叫用戶)對(duì)父類(也稱基類)的訪問(wèn)權(quán)限。
public繼承
class Teacher : public Person { public: Teacher(const string& name, int age, const string& title) : Person(name, age), m_title(title) { } void ShowTeacherInfo() { ShowInfo(); //正確,public屬性子類可見(jiàn) cout << "職稱:" << m_title << endl; //正確,本類中可見(jiàn)自己的所有成員 } private: string m_title; //職稱 }; class Teacher : public Person { public: Teacher(const string& name, int age, const string& title) : Person(name, age), m_title(title) { } void ShowTeacherInfo() { ShowInfo(); //正確,public屬性子類可見(jiàn) cout << "職稱:" << m_title << endl; //正確,本類中可見(jiàn)自己的所有成員 } private: string m_title; //職稱 }; void TestPublic() { Teacher teacher("李四", 35, "副教授"); teacher.ShowInfo(); cout << endl; teacher.ShowTeacherInfo(); } void TestPublic() { Teacher teacher("李四", 35, "副教授"); teacher.ShowInfo(); cout << endl; teacher.ShowTeacherInfo(); }
結(jié)果:
姓名:李四
年齡:35
姓名:李四
年齡:35
職稱:副教授
private繼承:
class Teacher : private Person { public: Teacher(const string& name, int age, const string& title) : Person(name, age), m_title(title) { } void ShowTeacherInfo() { ShowInfo(); //正確,public屬性子類可見(jiàn) cout << "職稱:" << m_title << endl; //正確,本類中可見(jiàn)自己的所有成員 } private: string m_title; //職稱 }; class Teacher : private Person { public: Teacher(const string& name, int age, const string& title) : Person(name, age), m_title(title) { } void ShowTeacherInfo() { ShowInfo(); //正確,public屬性子類可見(jiàn) cout << "職稱:" << m_title << endl; //正確,本類中可見(jiàn)自己的所有成員 } private: string m_title; //職稱 }; void TestPrivate() { Teacher teacher("李四", 35, "副教授"); teacher.ShowInfo(); //錯(cuò)誤,因?yàn)門(mén)eacher采用了private的繼承方式,外部不可訪問(wèn)。 cout << endl; teacher.ShowTeacherInfo(); } void TestPrivate() { Teacher teacher("李四", 35, "副教授"); teacher.ShowInfo(); //錯(cuò)誤,因?yàn)門(mén)eacher采用了private的繼承方式,外部不可訪問(wèn)。 cout << endl; teacher.ShowTeacherInfo(); }
3. public、protected、private三種繼承方式,相當(dāng)于把父類的public訪問(wèn)權(quán)限在子類中變成了對(duì)應(yīng)的權(quán)限。
如protected繼承,把父類中的public成員在本類中變成了protected的訪問(wèn)控制權(quán)限;private繼承,把父類的public成員和protected成員在本類中變成了private訪問(wèn)控制權(quán)。
protected繼承:
class Teacher : protected Person { public: Teacher(const string& name, int age, const string& title) : Person(name, age), m_title(title) { } void ShowTeacherInfo() { ShowInfo(); //正確,public屬性子類可見(jiàn) cout << "職稱:" << m_title << endl; //正確,本類中可見(jiàn)自己的所有成員 } private: string m_title; //職稱 }; class Teacher : protected Person { public: Teacher(const string& name, int age, const string& title) : Person(name, age), m_title(title) { } void ShowTeacherInfo() { ShowInfo(); //正確,public屬性子類可見(jiàn) cout << "職稱:" << m_title << endl; //正確,本類中可見(jiàn)自己的所有成員 } private: string m_title; //職稱 }; void TestProtected() { Teacher teacher("李四", 35, "副教授"); teacher.ShowInfo(); //錯(cuò)誤,基類Person的ShowInfo此時(shí)對(duì)Teacher相當(dāng)于protected的,外部不可以被訪問(wèn) cout << endl; teacher.ShowTeacherInfo(); } void TestProtected() { Teacher teacher("李四", 35, "副教授"); teacher.ShowInfo(); //錯(cuò)誤,基類Person的ShowInfo此時(shí)對(duì)Teacher相當(dāng)于protected的,外部不可以被訪問(wèn) cout << endl; teacher.ShowTeacherInfo(); } class Leader : public Teacher { public: Leader(const string& name, int age, const string& title, string position) : Teacher(name, age, title), m_position(position) { } void ShowLeaderInfo() { ShowInfo(); //基類Person的ShowInfo此時(shí)相當(dāng)于protected的,但子類仍可以訪問(wèn) ShowTeacherInfo(); //ShowTeacherInfo仍然是public的,可以訪問(wèn) cout << m_position << endl; } private: string m_position; }; class Leader : public Teacher { public: Leader(const string& name, int age, const string& title, string position) : Teacher(name, age, title), m_position(position) { } void ShowLeaderInfo() { ShowInfo(); //基類Person的ShowInfo此時(shí)相當(dāng)于protected的,但子類仍可以訪問(wèn) ShowTeacherInfo(); //ShowTeacherInfo仍然是public的,可以訪問(wèn) cout << m_position << endl; } private: string m_position; };
以上所述是小編給大家介紹的C++ 的三種訪問(wèn)權(quán)限與三種繼承方式,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C語(yǔ)言結(jié)構(gòu)體數(shù)組常用的三種賦值方法(包含字符串)
C語(yǔ)言只有在定義字符數(shù)組的時(shí)候才能用“=”來(lái)初始化變量,其它情況下是不能直接用“=”來(lái)為字符數(shù)組賦值的,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言結(jié)構(gòu)體數(shù)組常用的三種賦值方法,需要的朋友可以參考下2022-06-06C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(文件版)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07opencv實(shí)現(xiàn)機(jī)器視覺(jué)檢測(cè)和計(jì)數(shù)的方法
在機(jī)器視覺(jué)中,有時(shí)需要對(duì)產(chǎn)品進(jìn)行檢測(cè)和計(jì)數(shù)。其難點(diǎn)無(wú)非是對(duì)于產(chǎn)品的圖像分割。本文就來(lái)介紹一下機(jī)器視覺(jué)檢測(cè)和計(jì)數(shù)的實(shí)現(xiàn),感興趣的可以參考一下2021-05-05利用C++ R3層斷鏈實(shí)現(xiàn)模塊隱藏功能
在R3層的模塊隱藏,我們需要做的就是將其該鏈表斷鏈,將某一模塊從這個(gè)雙向鏈表中摘除,這樣再調(diào)用傳統(tǒng)的API時(shí)就會(huì)搜索不到。本文重點(diǎn)給大家介紹利用C++ R3層斷鏈實(shí)現(xiàn)模塊隱藏功能,感興趣的朋友一起看看吧2019-10-10C++中為何推薦要把基類析構(gòu)函數(shù)設(shè)置成虛函數(shù)
這篇文章主要介紹了C++中為何推薦要把基類析構(gòu)函數(shù)設(shè)置成虛函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12