繼承中的幾種關系
1. is a的關系
比如說基類狗,派生了肉狗和寵物狗,我們就可以說肉狗是狗,也可以說寵物狗是狗,這就是is a的關系。但是需要注意的是,并非所有的繼承都是is a的關系。
2. has a的關系
繼承中的權限繼承
class Base{public:    int public_i_;      // 外部可訪問PRotected:    int protected_i_;   // 內部可訪問,外部無法訪問,    可繼承,子類可訪問private:    int private_i_;     // 內部可訪問,外部不可訪問,    不可繼承,子類不可訪問};class A : public Base   // public繼承所有的權限都沒有發生改變{    void SetI()    {        protected_i_ = 100;    }};class B : protected Base    // protected 繼承會將基類中public的權限改為protected權限{    void SetI()    {        protected_i_ = 100;    }};class C : private Base  // 將基類中的protected和public權限都改為private權限{    void SetI()    {        protected_i_ = 100;    }};基類中有默認構造函數的繼承中的構造和析構順序
#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;}
基類中沒有默認構造函數的繼承中的構造和析構順序
#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)  // 此時會默認的調用基類的默認構造函數,如果基類中沒有默認的構造函數時,        // 那么必須在派生類中的初始化列表中顯示的來構造    {        std::cout << "A::A()" << std::endl;    }    ~A()    {        std::cout << "A::~A()" << std::endl;    }};int main(){    A demo;    demo.GetNum();    return 0;}繼承中的函數,派生類中沒有實現基類的函數方法
#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) // 此時會默認的調用基類的默認構造函數,如果基類中沒有默認的構造函數時,        // 那么必須在派生類中的初始化列表中顯示的來構造    {        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;}結果是 
另一種情況是,派生類中實現了基類的函數方法
#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) // 此時會默認的調用基類的默認構造函數,如果基類中沒有默認的構造函數時,        // 那么必須在派生類中的初始化列表中顯示的來構造    {        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;}輸出結果是 
基類指針指向派生類對象的函數調用情況
#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) // 此時會默認的調用基類的默認構造函數,如果基類中沒有默認的構造函數時,        // 那么必須在派生類中的初始化列表中顯示的來構造    {        std::cout << "A::A()" << std::endl;    }    ~A()    {        std::cout << "A::~A()" << std::endl;    }    int GetNum() const  // 這個函數被重寫了,也就是把基類中的方法給覆蓋了,基類中的方法就不存在了    {        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;  // 調用的是 A 中的 Base中的GetNum方法,但是這樣很顯然不是我們想要的結果                                                // 這種用基類的指針指向派生類的對象的情況很多    return 0;}輸出結果是 
使用虛函數在繼承中函數調用的好處
#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) // 此時會默認的調用基類的默認構造函數,如果基類中沒有默認的構造函數時,        // 那么必須在派生類中的初始化列表中顯示的來構造    {        std::cout << "A::A()" << std::endl;    }    ~A()    {        std::cout << "A::~A()" << std::endl;    }    int GetNum() const  // 這個函數被重寫了,也就是把基類中的方法給覆蓋了,基類中的方法就不存在了    {        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;  // 調用的是 A 中的 Base中的GetNum方法,但是這樣很顯然不是我們想要的結果                                                // ,使用虛函數就可以滿足我們的需求,我們在基類中的GetNum前面加上virtual關鍵字                                                // 這種用基類的指針指向派生類的對象的情況很多    return 0;}結果是  為什么會這樣呢?因為使用了虛函數,虛函數用來指明派生類中需要調用的函數,也就是說會在派生類中優先尋找要調用的函數,而不是在基類中尋找這個函數。
 為什么會這樣呢?因為使用了虛函數,虛函數用來指明派生類中需要調用的函數,也就是說會在派生類中優先尋找要調用的函數,而不是在基類中尋找這個函數。
虛函數的其它例子
派生類繼承并操作了基類中的成員變量
繼承了基類中的str_變量,并對其進行了操作,導致內存泄漏,并且在析構的時候重復析構。
#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;}調用基類的構造函數來操作基類的成員變量,而不是在派生類中直接操作基類只能怪的成員變量
#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;}這樣雖然保證了只對基類中的成員變量操作一次,但是在析構的時候還是會重復析構。
虛析構函數的使用情形
沒有虛析構函數的析構情形
#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;}運行結果如下圖所示  這樣只是調用了基類中的析構函數,并沒有調用派生類中的析構函數
 這樣只是調用了基類中的析構函數,并沒有調用派生類中的析構函數
有虛析構函數的析構情形
#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;}運行結果如下:  這樣就會把派生類中的析構函數也調用了。
 這樣就會把派生類中的析構函數也調用了。
正確的做法是只需要管理好本類中的成員變量即可,無需管理繼承下來的成員變量(其實我覺得在實際的應用中,成員變量應該都是private的,是不需要被子類繼承的,只有函數方法才需要被繼承下來)
#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;}這樣就保證了程序的正確運行。
虛函數的調用
#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;    // 此時在返回的時候會調用拷貝構造函數,如果沒有實現拷貝構造函數,                        // 那么就會出現淺拷貝的情況,導致程序運行錯誤,所以我們必須手動實現拷貝構造函數    }};int main(){    MyString str("I Love ");    String *pString = &str;    std::cout << str + ("Mark") << std::endl;    std::cout << *pString + ("Mark") << std::endl;    return 0;}運行結果 
繼承中的虛析構函數
基類中的析構函數不是虛析構函數,而是普通函數的情況:
#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;}運行結果是:  從結果上可以看出,在delete的時候,只是調用了基類中的析構函數,并沒有調用派生類中的析構函數,那么,如何能讓派生類中的析構函數也被調用呢?請看下面的情況!!! 基類中的析構函數是虛析構函數的情況:
 從結果上可以看出,在delete的時候,只是調用了基類中的析構函數,并沒有調用派生類中的析構函數,那么,如何能讓派生類中的析構函數也被調用呢?請看下面的情況!!! 基類中的析構函數是虛析構函數的情況:
#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;}運行結果:  關于繼承的構造和析構順序的總結: 一般的繼承體系的類 基類構造 基類成員構造 子類構造 子類析構 基類成員析構 基類析構
 關于繼承的構造和析構順序的總結: 一般的繼承體系的類 基類構造 基類成員構造 子類構造 子類析構 基類成員析構 基類析構
繼承中的虛繼承
著名的菱形繼承
#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;}指明要訪問哪個基類中的變量,就不會出現編譯錯誤了。 這樣雖然解決了編譯錯誤的問題,但是我們不需要這樣的情況,我們只需要一份變量就可以了,那么這樣該怎么解決呢?我們可以通過虛繼承來解決這個問題!!!
#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;}此時編譯也就可以通過了。
繼承中的純虛函數
#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class Animal{public:    virtual void Cry() = 0; // 純虛函數,無需實現,需要子類來實現};class Dog : public Animal{public:};int main(){    Dog dog;    // 此時編譯不能通過,因為沒有實現基類中的純虛函數    return 0;}#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>class Animal{public:    virtual void Cry() = 0; // 純虛函數,無需實現,需要子類來實現};class Dog : public Animal{public:    void Cry()    {    }};int main(){    Dog dog;    // 當實現了基類中的純虛函數后,就能實例化對象了    return 0;}#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>// 純虛函數 不能實例化class Animal{public:    virtual void Cry() = 0; // 純虛函數,無需實現,需要子類來實現(強制性的進行實現)};class Dog : public Animal{public:    void Cry()    {    }};class A : public Dog{};int main(){    Dog dog;    // 當實現了基類中的純虛函數后,就能實例化對象了                // 只要有純虛函數的類,我們稱之為抽象類,抽象類是無法實例化的    A a;        // 此時A類就可以實例化,因為Dog類中已經實現了純虛函數    return 0;}#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>// 純虛函數 不能實例化 是其它類的基類 經常用來作為接口使用 接口是用來進行解耦的// 接口// 沒有任何的數據成員,只是把函數名列出來class Animal{public:    virtual void Cry() = 0;     // 純虛函數,無需實現,需要子類來實現(強制性的進行實現)    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;}