C++ 公有繼承、保護繼承和私有繼承的對比


在c++的繼承控制中,有三種不同的控制權限,分別是public、protected和private。定義派生類時,若不顯示加上這三個關鍵字,就會使用默認的方式,用struct定義的類是默認public繼承,class定義的類是默認private繼承。這和Java有很大的不同,Java默認使用public繼承,而且只有公有繼承。

        1.使用public繼承時,派生類內部可以訪問基類中public和protected成員,但是類外只能通過派生類的對象訪問基類的public成員。
        (1)基類的public成員在派生類中依然是public的。
        (2)基類中的protected成員在派生類中依然是protected的。
        (3)基類中的private成員在派生類中不可訪問。
        2.使用protected繼承時,派生類內部可以訪問基類中public和protected成員,並且類外也不能通過派生類的對象訪問基類的成員(可以在派生類中添加公有成員函數接口間接訪問基類中的public和protected成員)。
        (1)基類的public成員在派生類中變為protected成員。
        (2)基類的protected成員在派生類中依然是protected成員。
        (3)基類中的private成員在派生類中不可訪問。
       3.使用private繼承時,派生類內部可以訪問基類中public和protected成員,並且類外也不能通過派生類的對象訪問基類的成員(可以在派生類中添加公有成員函數接口間接訪問基類中的public和protected成員)。
        (1)基類的public成員在派生類中變成private成員。
        (2)基類的protected成員在派生類中變成private成員。
        (3)基類的private成員在派生類中不可訪問。
        為了便於理解,我們用一個表格來說明這幾種控制符使用的情況:

 

派 生 方 式  基類的public成員 基類的protected成員 基類的private成員
public派生 還是public成員 變成protected成員    不可見
protected派生 變成protected成員    變成protected成員    不可見
private派生 變為private成員 變為private成員 不可見
#include<iostream> 
#include<cstdio>
using namespace std;

class Biological{
public:
    string property;
    virtual void prop(){
        cin>>property;
        cout<<"property:"<<property<<endl;
    }    

private:  // c++默認權限為private
    string name;
    void species(){
        cin>>name;
        cout<<"name:"<<name<<endl;
    }

protected:
    string belong;
    void bel(){
        cin>>belong;
        cout<<"belong:"<<belong<<endl;
    }
};

class Animal:public Biological{// 公有繼承
public:
    void display(){
        prop();
        bel();
        //species();  // error: ‘void Biological::species()’ is private 
    }
};

class Plant:private Biological{  // 私有繼承為默認可以省略
public:
    void display(){
        prop();
        bel();
        //species();  // error: ‘void Biological::species()’ is private
    }
};

class Both:protected Biological{  // 私有繼承
public:
    void display(){
        prop();
        bel();
        //species();  // error: ‘void Biological::species()’ is private
    }
};

void animalDis(){
    Animal animal;
    animal.display();
    animal.property="cat";
    cout<<"修改animal.property為:"<<animal.property<<endl;
    // animal.name="xiaohei";  // error: ‘std::__cxx11::string Biological::name’ is private
    // cout<<"animal.name"<<animal.name<<endl;
    // animal.belong="animal";  // error: ‘std::__cxx11::string Biological::belong’ is protected
    // cout<<"animal.belong"<<animal.belong<<endl;
}

void plantDis(){
    Plant plant;
    plant.display();
    // plant.property="tree";  // error: ‘std::__cxx11::string Biological::property’ is inaccessible
    // cout<<"修改plant.property為:"<<plant.property<<endl;
    // plant.name="poplar";  //error: ‘std::__cxx11::string Biological::name’ is private
    // cout<<"修改plant.name為:"<<plant.name<<endl;
    // plant.belong="plant";  //error: ‘std::__cxx11::string Biological::belong’ is protected
    // cout<<"修改plant.belong為:"<<plant.belong<<endl;
}

void bothDis(){
    Both both;
    both.display();
    // both.property="tree";  // error: ‘std::__cxx11::string Biological::property’ is inaccessible
    // cout<<"修改both.property為:"<<both.property<<endl;
    // both.name="poplar";  // error: ‘std::__cxx11::string Biological::name’ is private
    // cout<<"修改both.name為:"<<both.name<<endl;
    // both.belong="plant";  // error: ‘std::__cxx11::string Biological::belong’ is protected
    // cout<<"修改both.belong為:"<<both.belong<<endl;
}

int main(){
    animalDis();
    plantDis();
    bothDis();
    return 0;
}

 

 


————————————————
版權聲明:本文為CSDN博主「扮豬吃餃子」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_28712713/article/details/80967650

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM