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

 

posted @ 2019-09-04 10:00  hiligei  阅读(1009)  评论(0编辑  收藏  举报