zoukankan      html  css  js  c++  java
  • public、potected 、private继承下的子类对父类成员的访问情况

    #include<iostream>
      2 #include<string>
      3 using namespace std;
      4 class parent{
      5 protected:
      6         int m_a;
      7         int m_b;
      8 public:
      9         int m_c;
     10         void set(int a,int b,int c){
     11                 m_a = a;
     12                 m_b = b;
     13                 m_c = c;
     14         }
     15 };
     16 class child_A:public parent{
     17 public:
     18         void print(){
     19                 cout << "m_a=" << m_a << endl;
     20                 cout << "m_b=" << m_b << endl;
     21                 cout << "m_c=" << m_c << endl;
     22         }
     23 };
     24 class child_B:protected parent{
     25 public:
     26         void print(){
     27                 cout << "m_a=" << m_a << endl;
     28                 cout << "m_b=" << m_b << endl;
     29                 cout << "m_c=" << m_c << endl;
                                                                                                                                       1,8          顶端
     30         }
     31 
     32 };
     33 class child_C:private parent{
     34 public:
     35         void print(){
     36                 cout << "m_a=" << m_a << endl;
     37                 cout << "m_b=" << m_b << endl;
     38                 cout << "m_c=" << m_c << endl;
     39         }
     40 
     41 };
     42 int main(){
     43         child_A a;//public继承
     44         child_B b;//protected 继承
     45         child_C c;//private继承
     46         a.m_c = 100;//100
     47         //b.m_c = 100;//100
     48         //c.m_c = 100;
     49         return 0;
     50         a.print();
     51         b.print();
     52         c.print();
     53         cout << endl;
     54         a.set(1,1,1);
     55         b.set(2,2,2);
     56         c.set(3,3,3);
     57 }
    //结果

    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’

  • 相关阅读:
    log4j 配置文件详解
    Java 发送Get和Post请求
    java 基于百度地图API GPS经纬度解析地址
    Spring MVC 注解json 配置
    web.xml中classpath 解释
    【错误信息】springMVC No mapping found for HTTP request with URI
    栈和堆
    结构体和类的区别,联系
    Delegate,Block,Notification, KVC,KVO,Target-Action
    Protocol, Delegate
  • 原文地址:https://www.cnblogs.com/DXGG-Bond/p/11919527.html
Copyright © 2011-2022 走看看