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 ;
}
————————————————
版权声明:本文为CSDN博主「扮猪吃饺子」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_28712713/article/details/80967650
最新文章
- 私有项目免费使用Git
- ASP.NET 5探险(7):使用混合型控制器方便实现单页应用
- Java基础(42):Java中主类中定义方法加static和不加static的区别(前者可以省略类名直接在主方法调用,后者必须先实例化后用实例调用)
- Oracle Database Cloud Services
- HNOI2008越狱(快速幂)
- 《C语言程序设计现代方法》第3章 格式化输入/输出
- Android 图片合成:添加蒙板效果 不规则相框 透明度渐变效果的实现
- Qt之操作Excel
- C++ Placement New
- Cocos2d-x lua游戏开发之安装Lua到mac系统
- LAMBDA表达式常用 (全)
- Memory Dump 分析器
- Swift:Minimizing Annotation with Type Inference
- 采用Anaconda平台调用pymc3时出现错误的解决方法
- 移动端上拉加载,下拉刷新效果Demo
- 解决Windows10或者其他版本Windows Update报错的问题
- luogu P4515 [COCI2009-2010#6] XOR
- Sql 截取字段中的字符串
- webserive学习记录3-eclipse创建webservice
- Jenkins+Docker持续集成