在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. [工具-002]把png图片转换成ico图标

    最近我收到了上级的一个需求,我们需要使用产品的png图片,批量转换成ico图片,然后调用上一篇的方法,替换可执行程序的图标.一开始查看资料的时候,C#有直接可以转成ico图片的方法,很简单.但是生成的 ...

  2. string 去重复

    //AABB>>AB         //AAA>>A         //ABBAA>ABA public static string SpiltString(stri ...

  3. Rocket - diplomacy - wirePrefix

    https://mp.weixin.qq.com/s/DVcA2UixnB_6vgI3SjZGyQ   调试wirePrefix方法.   1. 实现   wirePrefix用于调整名称格式,其实现 ...

  4. meta个人学习纪录

    < meta > 元素 元素可提供相关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. 标签位于文档的头部,不包含任何内容. 标签的属性定义了与 ...

  5. 【HIVE】数据分析HQL的编写方法/思路

    SQL编写一般思路: 1)复杂的查询,先划分为小任务,以降低难度.分别实现各个小任务后,再进行汇总: 2)涉及多表时,先进行联表查询: 3)简单分组,一般只需要group by即可: 4)组内TopN ...

  6. JAVASE(十五) 泛型 :泛型用例、自定义泛型类、通配符

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.泛型在集合中的使用 1.1 在集合中使用泛型之前的例子 ​ 为什么要有泛型(Generic)? 1. ...

  7. 移动端fixed兼容问题

    最近做移动端页面,有个需求类似下图 底部导航用fixed定位时在部分iOS版本中会有问题: 1.上滑是底部会跟着滑动,手指松开时才会又回到底部 2.软键盘唤起的情况下,也会出现许多莫名其妙的问题 网上 ...

  8. CPU亲和度

    CPU亲和度(CPU Affinity),就是将一个进程或者线程强制绑定在CPU的某一个core上运行. 参考:https://www.cnblogs.com/zhangxuan/p/6427533. ...

  9. ASP.NET使用HttpHandler进行页面静态化(自动生成页面)

    这次的Demo是,一个根页面,点击链接创建子页面,子页面都是一个Template页面进行替换的 一个根页面 <%@ Page Language="C#" AutoEventW ...

  10. Java实现 LeetCode 764 最大加号标志(暴力递推)

    764. 最大加号标志 在一个大小在 (0, 0) 到 (N-1, N-1) 的2D网格 grid 中,除了在 mines 中给出的单元为 0,其他每个单元都是 1.网格中包含 1 的最大的轴对齐加号 ...