国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++--第五課-繼承

2019-11-11 06:46:12
字體:
供稿:網(wǎng)友

繼承中的幾種關(guān)系

1. is a的關(guān)系

比如說基類狗,派生了肉狗和寵物狗,我們就可以說肉狗是狗,也可以說寵物狗是狗,這就是is a的關(guān)系。但是需要注意的是,并非所有的繼承都是is a的關(guān)系。

2. has a的關(guān)系

繼承中的權(quán)限繼承

class Base{public: int public_i_; // 外部可訪問PRotected: int protected_i_; // 內(nèi)部可訪問,外部無法訪問, 可繼承,子類可訪問private: int private_i_; // 內(nèi)部可訪問,外部不可訪問, 不可繼承,子類不可訪問};class A : public Base // public繼承所有的權(quán)限都沒有發(fā)生改變{ void SetI() { protected_i_ = 100; }};class B : protected Base // protected 繼承會將基類中public的權(quán)限改為protected權(quán)限{ void SetI() { protected_i_ = 100; }};class C : private Base // 將基類中的protected和public權(quán)限都改為private權(quán)限{ void SetI() { protected_i_ = 100; }};

基類中有默認(rèn)構(gòu)造函數(shù)的繼承中的構(gòu)造和析構(gòu)順序

#include <iostream>class Base{public: Base() { std::cout << "Base::Base()" << std::endl; } ~Base() { std::cout << "Base::~Base()" << std::endl; } int GetNum() { return num_; }private: int num_;};class A : public Base{public: A() { std::cout << "A::A()" << std::endl; } ~A() { std::cout << "A::~A()" << std::endl; }};int main(){ A demo; demo.GetNum(); return 0;}

這里寫圖片描述

基類中沒有默認(rèn)構(gòu)造函數(shù)的繼承中的構(gòu)造和析構(gòu)順序

#include <iostream>class Base{public: Base(int num) : num_(num) { std::cout << "Base::Base()" << std::endl; } ~Base() { std::cout << "Base::~Base()" << std::endl; } int GetNum() { return num_; }private: int num_;};class A : public Base{public: A() : Base(10) // 此時會默認(rèn)的調(diào)用基類的默認(rèn)構(gòu)造函數(shù),如果基類中沒有默認(rèn)的構(gòu)造函數(shù)時, // 那么必須在派生類中的初始化列表中顯示的來構(gòu)造 { std::cout << "A::A()" << std::endl; } ~A() { std::cout << "A::~A()" << std::endl; }};int main(){ A demo; demo.GetNum(); return 0;}

繼承中的函數(shù),派生類中沒有實現(xiàn)基類的函數(shù)方法

#include <iostream>class Base{public: Base(int num) : num_(num) { std::cout << "Base::Base()" << std::endl; } ~Base() { std::cout << "Base::~Base()" << std::endl; } int GetNum() const { return num_; }private: int num_;};class A : public Base{public: A(int num) : Base(0), num_(num) // 此時會默認(rèn)的調(diào)用基類的默認(rèn)構(gòu)造函數(shù),如果基類中沒有默認(rèn)的構(gòu)造函數(shù)時, // 那么必須在派生類中的初始化列表中顯示的來構(gòu)造 { std::cout << "A::A()" << std::endl; } ~A() { std::cout << "A::~A()" << std::endl; } //int GetNum() const //{ // return num_; //}private: int num_;};int main(){ A demo(100); demo.GetNum(); std::cout << demo.GetNum() << std::endl; return 0;}

結(jié)果是 這里寫圖片描述

另一種情況是,派生類中實現(xiàn)了基類的函數(shù)方法

#include <iostream>class Base{public: Base(int num) : num_(num) { std::cout << "Base::Base()" << std::endl; } ~Base() { std::cout << "Base::~Base()" << std::endl; } int GetNum() const { return num_; }private: int num_;};class A : public Base{public: A(int num) : Base(0), num_(num) // 此時會默認(rèn)的調(diào)用基類的默認(rèn)構(gòu)造函數(shù),如果基類中沒有默認(rèn)的構(gòu)造函數(shù)時, // 那么必須在派生類中的初始化列表中顯示的來構(gòu)造 { std::cout << "A::A()" << std::endl; } ~A() { std::cout << "A::~A()" << std::endl; } int GetNum() const { return num_; }private: int num_;};int main(){ A demo(100); demo.GetNum(); std::cout << demo.GetNum() << std::endl; return 0;}

輸出結(jié)果是 這里寫圖片描述

基類指針指向派生類對象的函數(shù)調(diào)用情況

#include <iostream>class Base{public: Base(int num) : num_(num) { std::cout << "Base::Base()" << std::endl; } ~Base() { std::cout << "Base::~Base()" << std::endl; } int GetNum() const { std::cout << "Base::GetNum()" << std::endl; return num_; }private: int num_;};class A : public Base{public: A(int num) : Base(0), num_(num) // 此時會默認(rèn)的調(diào)用基類的默認(rèn)構(gòu)造函數(shù),如果基類中沒有默認(rèn)的構(gòu)造函數(shù)時, // 那么必須在派生類中的初始化列表中顯示的來構(gòu)造 { std::cout << "A::A()" << std::endl; } ~A() { std::cout << "A::~A()" << std::endl; } int GetNum() const // 這個函數(shù)被重寫了,也就是把基類中的方法給覆蓋了,基類中的方法就不存在了 { std::cout << "A::GetNum()" << std::endl; return num_; }private: int num_;};int main(){ Base base(100); A a(200); Base *pBase = &a; // 因為 A is Base // 寵物狗 Is 狗 對 狗 泛指 寵物狗 // 狗 Is 寵物狗 錯 std::cout << pBase->GetNum() << std::endl; // 調(diào)用的是 A 中的 Base中的GetNum方法,但是這樣很顯然不是我們想要的結(jié)果 // 這種用基類的指針指向派生類的對象的情況很多 return 0;}

輸出結(jié)果是 這里寫圖片描述

使用虛函數(shù)在繼承中函數(shù)調(diào)用的好處

#include <iostream>class Base{public: Base(int num) : num_(num) { std::cout << "Base::Base()" << std::endl; } ~Base() { std::cout << "Base::~Base()" << std::endl; } virtual int GetNum() const { std::cout << "Base::GetNum()" << std::endl; return num_; }private: int num_;};class A : public Base{public: A(int num) : Base(0), num_(num) // 此時會默認(rèn)的調(diào)用基類的默認(rèn)構(gòu)造函數(shù),如果基類中沒有默認(rèn)的構(gòu)造函數(shù)時, // 那么必須在派生類中的初始化列表中顯示的來構(gòu)造 { std::cout << "A::A()" << std::endl; } ~A() { std::cout << "A::~A()" << std::endl; } int GetNum() const // 這個函數(shù)被重寫了,也就是把基類中的方法給覆蓋了,基類中的方法就不存在了 { std::cout << "A::GetNum()" << std::endl; return num_; }private: int num_;};int main(){ Base base(100); A a(200); Base *pBase = &a; // 因為 A is Base // 寵物狗 Is 狗 對 狗 泛指 寵物狗 // 狗 Is 寵物狗 錯 std::cout << pBase->GetNum() << std::endl; // 調(diào)用的是 A 中的 Base中的GetNum方法,但是這樣很顯然不是我們想要的結(jié)果 // ,使用虛函數(shù)就可以滿足我們的需求,我們在基類中的GetNum前面加上virtual關(guān)鍵字 // 這種用基類的指針指向派生類的對象的情況很多 return 0;}

結(jié)果是 這里寫圖片描述 為什么會這樣呢?因為使用了虛函數(shù),虛函數(shù)用來指明派生類中需要調(diào)用的函數(shù),也就是說會在派生類中優(yōu)先尋找要調(diào)用的函數(shù),而不是在基類中尋找這個函數(shù)。

虛函數(shù)的其它例子

派生類繼承并操作了基類中的成員變量

繼承了基類中的str_變量,并對其進(jìn)行了操作,導(dǎo)致內(nèi)存泄漏,并且在析構(gòu)的時候重復(fù)析構(gòu)。

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class String{public: String(const char *str = "") { unsigned int len = strlen(str); str_ = new char[len + sizeof(char)]; strcpy(str_, str); } ~String() { delete[] str_; } String &Operator+=(const String &other) { unsigned int len = strlen(str_) + strlen(other.str_); char *temp = new char[len + sizeof(char)]; strcpy(temp, str_); strcat(temp, other.str_); delete[] str_; str_ = temp; return *this; } String operator+(const String &other) { String demo; demo += other; return demo; }protected: char *str_;};class MyString : public String{public: MyString(const char *str = "PoEdu") { unsigned int len = strlen(str); str_ = new char[len + sizeof(char)]; strcpy(str_, str); } MyString operator+(const MyString &other) { } ~MyString() { delete[] str_; str_ = nullptr; }};int main(){ MyString str; //MyString other("Hello"); return 0;}
調(diào)用基類的構(gòu)造函數(shù)來操作基類的成員變量,而不是在派生類中直接操作基類只能怪的成員變量
#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class String{public: String(const char *str = "") { unsigned int len = strlen(str); str_ = new char[len + sizeof(char)]; strcpy(str_, str); } ~String() { delete[] str_; } String &operator+=(const String &other) { unsigned int len = strlen(str_) + strlen(other.str_); char *temp = new char[len + sizeof(char)]; strcpy(temp, str_); strcat(temp, other.str_); delete[] str_; str_ = temp; return *this; } String operator+(const String &other) { String demo; demo += other; return demo; }protected: char *str_;};class MyString : public String{public: MyString(const char *str = "PoEdu") : String(str) { /*unsigned int len = strlen(str); str_ = new char[len + sizeof(char)]; strcpy(str_, str);*/ } MyString operator+(const MyString &other) { } ~MyString() { delete[] str_; str_ = nullptr; }};int main(){ MyString str; //MyString other("Hello"); return 0;}

這樣雖然保證了只對基類中的成員變量操作一次,但是在析構(gòu)的時候還是會重復(fù)析構(gòu)。

虛析構(gòu)函數(shù)的使用情形

沒有虛析構(gòu)函數(shù)的析構(gòu)情形

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class A{public: ~A() { std::cout << "~A()" << std::endl; }};class B : public A{public: ~B() { std::cout << "~B(()" << std::endl; }};int main(){ A *pA = new B(); delete pA; return 0;}

運行結(jié)果如下圖所示 這里寫圖片描述 這樣只是調(diào)用了基類中的析構(gòu)函數(shù),并沒有調(diào)用派生類中的析構(gòu)函數(shù)

有虛析構(gòu)函數(shù)的析構(gòu)情形

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class A{public: virtual ~A() { std::cout << "~A()" << std::endl; }};class B : public A{public: ~B() { std::cout << "~B(()" << std::endl; }};int main(){ A *pA = new B(); delete pA; return 0;}

運行結(jié)果如下: 這里寫圖片描述 這樣就會把派生類中的析構(gòu)函數(shù)也調(diào)用了。

正確的做法是只需要管理好本類中的成員變量即可,無需管理繼承下來的成員變量(其實我覺得在實際的應(yīng)用中,成員變量應(yīng)該都是private的,是不需要被子類繼承的,只有函數(shù)方法才需要被繼承下來)
#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class String{public: String(const char *str = "") { unsigned int len = strlen(str); str_ = new char[len + sizeof(char)]; strcpy(str_, str); } ~String() { delete[] str_; } String &operator+=(const String &other) { unsigned int len = strlen(str_) + strlen(other.str_); char *temp = new char[len + sizeof(char)]; strcpy(temp, str_); strcat(temp, other.str_); delete[] str_; str_ = temp; return *this; } String operator+(const String &other) { String demo; demo += other; return demo; }protected: char *str_;};class MyString : public String{public: MyString(const char *str = "PoEdu") : String(str) { length_ = new int(0); /*unsigned int len = strlen(str); str_ = new char[len + sizeof(char)]; strcpy(str_, str);*/ } MyString operator+(const MyString &other) { } ~MyString() { //delete[] str_; //str_ = nullptr; // 正確的方法是不要去管理基類中的成員變量,只需要管理本類中的成員變量即可 delete length_; // 只需要管理本類中的length變量就可以了 }private: int *length_;};int main(){ //MyString str; //MyString other("Hello"); return 0;}

這樣就保證了程序的正確運行。

虛函數(shù)的調(diào)用

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class String{public: String(const char *str = "") { unsigned int len = strlen(str); str_ = new char[len + sizeof(char)]; strcpy(str_, str); } String(const String &other) { unsigned int len = strlen(other.str_); str_ = new char[len + sizeof(char)]; strcpy(str_, other.str_); } ~String() { delete[] str_; } String &operator+=(const String &other) { unsigned int len = strlen(str_) + strlen(other.str_); char *temp = new char[len + sizeof(char)]; strcpy(temp, str_); strcat(temp, other.str_); delete[] str_; str_ = temp; return *this; } String operator+(const String &other) { String demo(*this); demo += other; return demo; } friend std::ostream &operator<<(std::ostream &os, const String &other) { os << other.str_; return os; }protected: char *str_;};class MyString : public String{public: MyString(const char *str = "PoEdu") : String(str) { } MyString operator+(const MyString &other) { MyString temp(*this); temp += other; // 因為從基類中繼承了一個 += 操作符 temp += "---------PoEdu"; return temp; // 此時在返回的時候會調(diào)用拷貝構(gòu)造函數(shù),如果沒有實現(xiàn)拷貝構(gòu)造函數(shù), // 那么就會出現(xiàn)淺拷貝的情況,導(dǎo)致程序運行錯誤,所以我們必須手動實現(xiàn)拷貝構(gòu)造函數(shù) }};int main(){ MyString str("I Love "); String *pString = &str; std::cout << str + ("Mark") << std::endl; std::cout << *pString + ("Mark") << std::endl; return 0;}

運行結(jié)果 這里寫圖片描述

繼承中的虛析構(gòu)函數(shù)

基類中的析構(gòu)函數(shù)不是虛析構(gòu)函數(shù),而是普通函數(shù)的情況:

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class A{public: ~A() { std::cout << "~A()" << std::endl; }};class B : public A{public: B() : A() { demo_ = new int(0); } ~B() { std::cout << "~B()" << std::endl; delete demo_; }private: int *demo_;};int main(){ A * pA = new B(); delete pA; return 0;}

運行結(jié)果是: 這里寫圖片描述 從結(jié)果上可以看出,在delete的時候,只是調(diào)用了基類中的析構(gòu)函數(shù),并沒有調(diào)用派生類中的析構(gòu)函數(shù),那么,如何能讓派生類中的析構(gòu)函數(shù)也被調(diào)用呢?請看下面的情況!!! 基類中的析構(gòu)函數(shù)是虛析構(gòu)函數(shù)的情況:

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class A{public: virtual ~A() { std::cout << "~A()" << std::endl; }};class B : public A{public: B() : A() { demo_ = new int(0); } ~B() { std::cout << "~B()" << std::endl; delete demo_; }private: int *demo_;};int main(){ A * pA = new B(); delete pA; return 0;}

運行結(jié)果: 這里寫圖片描述 關(guān)于繼承的構(gòu)造和析構(gòu)順序的總結(jié): 一般的繼承體系的類 基類構(gòu)造 基類成員構(gòu)造 子類構(gòu)造 子類析構(gòu) 基類成員析構(gòu) 基類析構(gòu)

繼承中的虛繼承

著名的菱形繼承

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class A{public: int a_;};class B : public A{public: int b_;};class C : public A{public: int c_;};class D : public B, public C{ // a_ b_ c_,并且a_是兩個 // 我們稱這種繼承方式為菱形繼承};int main(){ D d; d.a_; return 0;}

此時在編譯的時候就會出錯,如下所示: 這里寫圖片描述 但是,我們也是可以訪問的

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class A{public: int a_;};class B : public A{public: int b_;};class C : public A{public: int c_;};class D : public B, public C{ // a_ b_ c_,并且a_是兩個 // 我們稱這種繼承方式為菱形繼承};int main(){ D d; d.B::a_; d.C::a_; return 0;}

指明要訪問哪個基類中的變量,就不會出現(xiàn)編譯錯誤了。 這樣雖然解決了編譯錯誤的問題,但是我們不需要這樣的情況,我們只需要一份變量就可以了,那么這樣該怎么解決呢?我們可以通過虛繼承來解決這個問題!!!

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class A{public: int a_;};class B : virtual public A{public: int b_;};class C : virtual public A{public: int c_;};class D : virtual public B, virtual public C{ // a_ b_ c_,并且a_是兩個 // 我們稱這種繼承方式為菱形繼承};int main(){ D d; d.a_; return 0;}

此時編譯也就可以通過了。

繼承中的純虛函數(shù)

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class Animal{public: virtual void Cry() = 0; // 純虛函數(shù),無需實現(xiàn),需要子類來實現(xiàn)};class Dog : public Animal{public:};int main(){ Dog dog; // 此時編譯不能通過,因為沒有實現(xiàn)基類中的純虛函數(shù) return 0;}#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class Animal{public: virtual void Cry() = 0; // 純虛函數(shù),無需實現(xiàn),需要子類來實現(xiàn)};class Dog : public Animal{public: void Cry() { }};int main(){ Dog dog; // 當(dāng)實現(xiàn)了基類中的純虛函數(shù)后,就能實例化對象了 return 0;}#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>// 純虛函數(shù) 不能實例化class Animal{public: virtual void Cry() = 0; // 純虛函數(shù),無需實現(xiàn),需要子類來實現(xiàn)(強制性的進(jìn)行實現(xiàn))};class Dog : public Animal{public: void Cry() { }};class A : public Dog{};int main(){ Dog dog; // 當(dāng)實現(xiàn)了基類中的純虛函數(shù)后,就能實例化對象了 // 只要有純虛函數(shù)的類,我們稱之為抽象類,抽象類是無法實例化的 A a; // 此時A類就可以實例化,因為Dog類中已經(jīng)實現(xiàn)了純虛函數(shù) return 0;}#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>// 純虛函數(shù) 不能實例化 是其它類的基類 經(jīng)常用來作為接口使用 接口是用來進(jìn)行解耦的// 接口// 沒有任何的數(shù)據(jù)成員,只是把函數(shù)名列出來class Animal{public: virtual void Cry() = 0; // 純虛函數(shù),無需實現(xiàn),需要子類來實現(xiàn)(強制性的進(jìn)行實現(xiàn)) virtual void Jump() = 0; // 跳的接口};class Dog : public Animal{public: void Cry() { }};class A : public Dog{};int main(){ Dog *dog = new A(); // 用來指向子類 dog->Cry(); return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 定西市| 庆阳市| 池州市| 银川市| 桃园市| 老河口市| 方城县| 恩平市| 沂水县| 宁海县| 吴堡县| 乐安县| 广南县| 绥中县| 长寿区| 舟曲县| 宜兴市| 泰兴市| 兰溪市| 高州市| 旬邑县| 临汾市| 乌拉特前旗| 任丘市| 华安县| 长汀县| 龙海市| 乌恰县| 汶上县| 天峨县| 富顺县| 东城区| 晋宁县| 饶河县| 古交市| 荣昌县| 葵青区| 甘德县| 怀来县| 防城港市| 三明市|