public、potected 、private继承下的子类对父类成员的访问情况
#include<iostream>
#include<string>
using namespace std;
class parent{
protected:
int m_a;
int m_b;
public:
int m_c;
void set(int a,int b,int c){
m_a = a;
m_b = b;
m_c = c;
}
};
class child_A:public parent{
public:
void print(){
cout << "m_a=" << m_a << endl;
cout << "m_b=" << m_b << endl;
cout << "m_c=" << m_c << endl;
}
};
class child_B:protected parent{
public:
void print(){
cout << "m_a=" << m_a << endl;
cout << "m_b=" << m_b << endl;
cout << "m_c=" << m_c << endl;
, 顶端
} };
class child_C:private parent{
public:
void print(){
cout << "m_a=" << m_a << endl;
cout << "m_b=" << m_b << endl;
cout << "m_c=" << m_c << endl;
} };
int main(){
child_A a;//public继承
child_B b;//protected 继承
child_C c;//private继承
a.m_c = ;//
//b.m_c = 100;//
//c.m_c = 100;
return ;
a.print();
b.print();
c.print();
cout << endl;
a.set(,,);
b.set(,,);
c.set(,,);
}
//结果
class.cpp:10:7: error: ‘void parent::set(int, int, int)’ is inaccessible
void set(int a,int b,int c){
^
class.cpp:54:13: error: within this context
b.set(2,2,2);
^
class.cpp:54:13: error: ‘parent’ is not an accessible base of ‘child_B’
class.cpp:10:7: error: ‘void parent::set(int, int, int)’ is inaccessible
void set(int a,int b,int c){
^
class.cpp:55:13: error: within this context
c.set(3,3,3);
^
class.cpp:55:13: error: ‘parent’ is not an accessible base of ‘child_C’
public、potected 、private继承下的子类对父类成员的访问情况的更多相关文章
- c++ public protected private 继承
1:访问控制 1.1 public 这类型成员可以被类本身函数访问,也可以被外部创建的类对象调用.子类对象与子类内部可以访问 1.2 protected类型成员,只能被类本身函数访问.外部创建的类对象 ...
- 关于protected在子类创建父类对象无法访问父类protected方法或成员
子类(父类的外部包)中访问父类的protetcted属性或者方法,是不可以通过创建父类对象调用的.注意:此处不讨论同包下的父类子类,因为同包下所有类都可访问protected属性或者方法. 请参见Ja ...
- c++子类和父类成员函数重名
子类和父类返回值参数相同,函数名相同,有virtual关键字,则由对象的类型决定调用哪个函数. 子类和父类只要函数名相同,没有virtual关键字,则子类的对象没有办法调用到父类的同名函数,父类的同名 ...
- 7,C++ public, protected, private 继承的区别
在某处看到一张图,简单明了的说明了三者的关系,很是佩服,遂记录下来. //公有继承 对象访问 成员访问 public --> public Y Y protected --> protec ...
- 访问权限PPP(public、private、protected、default)之成员变量、成员变量权限解析
首先,我们需要清楚一下方法是由哪些部分构成的: [权限修饰符列表][别的修饰符列表] 返回值类型 方法名(参数列表){ 方法体:} 然后我们需要知道成员变量和成员方法访问有几种情况:1.当前包同一 ...
- c/c++ 继承与多态 子类隐藏父类的同名非虚函数
问题1:若类A里有公有成员方法int fcn(),类B公有继承类A,并类B里有公有成员方法int fcn(int),可以在用户代码里,用类B的对象调用类A的int fcn()吗?? 不可以,编译出错, ...
- python 子类调用父类成员的方法
1.直接写类名调用: parent_class.parent_attribute(self) class Animal(): def __init__(self, name): self.name = ...
- c++继承构造子类调用父类构造函数的问题及关于容器指针的问题及当容器里储存指针时,记得要手动释放
看下面的一个问题: class Person { private: string name; public: Person(const string& s=""){ nam ...
- C++中public,protected,private派生类继承问题和访问权限问题
C++中public,protected,private派生类继承问题和访问权限问题 当一个子类从父类继承时,父类的所有成员成为子类的成员,此时对父类成员的访问状态由继承时使用的继承限定符决定. 1. ...
随机推荐
- React:Composition
在日常的UI构建中,经常会遇到一种情况:组件本身更多是作为一个容器,它所包含的内容可能是动态的.未预先定义的.这时候它的内容取决另一个组件或外部的输入.比如弹层. props.children: Re ...
- 如何安放你的大文件,MongoDB GridFS可以帮助你
1 简介 众所周知(你不知也当你知),MongoDB是以文档(Document)组织数据的.除了常用于存储Json数据,它也是可以存储普通文件的.我们可以把一些文件以BSOON的格式存入MongoDB ...
- Jmeter(二) - 从入门到精通 - 创建测试计划(Test Plan)(详解教程)
1.简介 上一篇中宏哥已经教你把JMeter的测试环境搭建起来了,那么这一篇我们就将JMeter启动起来,一睹其芳容,首先宏哥给大家介绍一下如何来创建一个测试计划(Test Plan). 2.创建一个 ...
- Hyperledger Fabric开发(二):创建网络
运行fabric-samples项目中的一个例子:first-network,创建第一个网络(Building Your First Network). 该网络共有4个peer节点,划分为2个组织(o ...
- poj2987 最大闭合权子图基础题
Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 10905 Accepted: 3291 Descript ...
- .Net基础之2——C#基础
1.注释符的作用 1).注销 2).解释 2.C#中的3种解释符 1).单行注释(//要注释的内容) //这行代码的作用是将hello world输出到控制台上 ...
- JavaScript Basic
Exercise-1 Write a JavaScript program to display the current day and time in the following format. T ...
- android自动化
1.环境安装 JDK 1.8 Appium Android_SDK python https://www.cnblogs.com/xiaohanzi/p/10676720.html https://b ...
- android小Demo--圆球跟随手指轨迹移动
eatwhatApp的客户端基本实现,会再后续进行整改,今天做一个在网上找到的小Demo,让屏幕中出现一个圆球,并跟随手指移动. 写个java类DrawView继承于View: public clas ...
- Spring_Bean的配置方式
1.通过工厂方法配置bean beans-factory.xml <?xml version="1.0" encoding="UTF-8"?> &l ...