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’

  • 相关阅读:
    asp.net 曲线图
    asp.net 图片下载
    使用a标签删除进行提示
    asp.net 后台获取input的值
    asp.net Excel数据导入到数据库中
    asp.net DataSet数据导出到Excel中
    select top 所有
    asp.net 上一条和下一条记录的显示
    日期相减函数
    mybatis mapper学习1(补充)-mapper.xml映射文件生成补充
  • 原文地址:https://www.cnblogs.com/DXGG-Bond/p/11919527.html
Copyright © 2011-2022 走看看