在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 ;
}


————————————————
版权声明:本文为CSDN博主「扮猪吃饺子」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_28712713/article/details/80967650

C++ 公有继承、保护继承和私有继承的对比的更多相关文章

  1. Effective C++ 第二版 40)分层 41)继承和模板 42)私有继承

    条款40 通过分层来体现"有一个"或"用...来实现" 使某个类的对象成为另一个类的数据成员, 实现将一个类构筑在另一个类之上, 这个过程称为 分层Layeri ...

  2. c++继承详解:共有(public)继承,私有继承(private)继承,保护(protected)继承

    公有继承(public)继承.私有继承(private).保护继承(protected)是常用的三种继承方式. 1.公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...

  3. C++学习笔记14,private/protected/public继承,私有继承,保护继承,公有继承(五)(总结)

    各种继承方式: 特征 公有继承 保护继承 私有继承 公有成员变为 派生类的公有成员 派生类的保护成员 派生类的私有成员 保护成员变为 派生类的保护成员 派生类的保护成员 派生类的私有成员 私有成员变为 ...

  4. C++ 中私有继承、保护继承与公有继承

    区别 下面通过一个示例来介绍三种继承的区别. 定义一个基类(假设为一个快退休的富豪): class RichMan { public: RichMan(); ~RichMan(); int m_com ...

  5. C++中公有继承、保护继承、私有继承的区别

    公有继承时基类中各成员属性保持不变,基类中private成员被隐藏.派生类的成员只能访问基类中的public/protected成员,而不能访问private成员:派生类的对象只能访问基类中的publ ...

  6. C++公有继承、保护继承和私有继承

    C++中的继承方式有: public.private.protected三种(它们直接影响到派生类的成员.及其对象对基类成员访问的规则). (1)public(公有继承):继承时保持基类中各成员属性不 ...

  7. C++之共有继承、保护继承、私有继承

    1.封装,public,private作用就是这个目的. 类外只能访问public成员而不能方位private成员: private成员只能被类成员和友元访问: 2.继承,protected的作用就是 ...

  8. C++公有继承、私有继承以及友元

    公有继承: 基类的成员在派生类中维持原来的访问权限,基类的publice成员为派生类的public成员,基类的protected成员为派生类的protected成员,基类的private成员在派生类的 ...

  9. C++ 继承方式 //语法:class 子类 :继承方式 父类 //继承方式 三种: //1.公共继承 //2.保护继承 //3.私有继承

    1 //继承方式 2 //语法:class 子类 :继承方式 父类 3 //继承方式 三种: 4 //1.公共继承 5 //2.保护继承 6 //3.私有继承 7 8 #include <ios ...

随机推荐

  1. 应小姐姐要求,整理常用Git操作命令,她都学会了,你确定不收藏

    前言 因为个人原因,转化了部门之后已经很久没有接触过开发层级的东西了,好多东西基本都忘记了,但是新的部门有时候会用到相应的研发部的代码和文档手册,所以耳边就充斥这一句话 这个为什么下载不了?这个为什么 ...

  2. Java中的自动装箱拆箱

    Java中的自动装箱拆箱 一.自动装箱与自动拆箱 自动装箱就是将基本数据类型转换为包装类类型,自动拆箱就是将包装类类型转换为基本数据类型. 1 // 自动装箱 2 Integer total = 90 ...

  3. word修改页眉使本页的页眉与别的页不一样

    关键回答:双击要修改的页的页眉,word顶端的工具栏“页面设置”当中的“链接到前一个”选项不要选中,即可修改本页页眉使之与别的页不一样. 详见:https://iask.sina.com.cn/b/2 ...

  4. C# 根据BackgroundWoker异步模型和ProgressBar控件,自定义进度条控件

    前言 程序开发过程中,难免会有的业务逻辑,或者算法之类产生让人能够感知的耗时操作,例如循环中对复杂逻辑处理;获取数据库百万乃至千万级数据;http请求的时候等...... 用户在使用UI操作并不知道程 ...

  5. 一次FGC导致CPU飙高的排查过程

    今天测试团队反馈说,服务A的响应很慢,我在想,测试环境也会慢?于是我自己用postman请求了一下接口,真的很慢,竟然要2s左右,正常就50ms左右的. 于是去测试服务器看了一下,发现服务器负载很高, ...

  6. 50个SQL语句(MySQL版) 问题九

    --------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...

  7. 小谢第2问:后端返回为数组list时候,怎么实现转为tree

    要求后端返回给我的list时候,在数组中定义有id , parentid, 可以用双重循环的方法,得到tree需要的数据结构,这样得到的数据就可以直接复制给树组件的data啦const oldData ...

  8. 【朝夕专刊】RabbitMQ消息的持久化优先级

    欢迎大家阅读<朝夕Net社区技术专刊> 我们致力于.NetCore的推广和落地,为更好的帮助大家学习,方便分享干货,特创此刊!很高兴你能成为忠实读者,文末福利不要错过哦! 上篇文章介绍了R ...

  9. Chisel3 - Wire & Reg

    https://mp.weixin.qq.com/s/Y26N5P4XOr5e3uyi5XQY-w   不同于Verilog,Chisel中Wire和Reg并不是数据类型,而是数据容器,作为数据的一个 ...

  10. UML ——六种依赖关系.md

    在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization), 关联(Association), 聚合(Aggregation), 组合(Compos ...