C++语法之继承:访问权限

#ifndef CLASS1_H
#define CLASS1_H
class A{
public:
    int a = 1;
    int f(){return a;}
    int g(){return b;}
    int h(){return c;}
    
protected:
    int b = 2;
    
private:
    int c = 3;
};
class B : public A{//访问说明符对派生类成员访问基类成员无影响(类内访问),,影响派生类用户 (类外访问)对基类的访问

public:
    int f1(){return a;}
    int g1(){return b;}//派生类可以直接访问基类protected
  //  int h1(){return c;}//派生类不能直接访问基类private
    int h11(){return h();}//派生类能间接访问基类private
    int a1 = 10;
    
protected:
    int b1 = 20;
    
private:
    int c1 = 30;
};

class C : protected A{//访问说明符对派生类成员访问基类成员无影响(类内访问),,影响派生类用户 (类外访问)对基类的访问

public:
    int f2(){return a;}
    int g2(){return b;}//派生类可以直接访问基类protect
  //int h2(){return c;}//派生类不能直接访问基类private
     int h22(){return h();}//派生类能间接访问基类private
    int a2 = 100;
protected:
    int b2 = 200;
    
private:
    int c2 = 300;
};

class D : private A{//访问说明符对派生类成员访问基类成员无影响(类内访问),,影响派生类用户 (类外访问)对基类的访问

public:
    int f3(){return a;}
    int g3(){return b;}//派生类可以直接访问基类protect
  //int h3(){return c;}//派生类不能直接访问基类private
    int h33(){return h();}//派生类能间接访问基类private
    int a3 = 1000;
    
protected:
    int b2 = 2000;
    
private:
    int c2 = 3000;
};
#endif // CLASS1_H




#include <iostream>
#include"class1.h"
using namespace std;
int main()
{
    A A1;//类的用户包括类对象和派生类,两者访问级别类似,相当于类外访问
  //cout << A::a << endl;//a是static才用作用域解析符
    cout << A1.a << endl;
    cout << A1.f() << endl;
  //cout << A1.b << endl;//类对象不能直接访问protect,只能通过函数成员访问
    cout << A1.g() << endl;
  //cout << A1.c << endl;//类对象不能直接访问private,只能通过函数成员访问
    cout << A1.h() << endl;

    B B1;//public继承
    cout << B1.a << endl;//派生类对象能直接访问基类public成员
    cout << B1.f1() << endl;//派生类对象能间接访问基类public成员
  //cout << B1.b << endl;//派生类对象不能直接访问基类protect成员
    cout << B1.g1() << endl;//派生类对象能间接访问基类public成员
  //cout << B1.c << endl;//派生类对象不能直接访问基类private成员
  //cout << B1.h1() << endl;//派生类对象能间接访问基类private成员,调函数
    cout << B1.h11() << endl;

    C C1;//protect继承
  //  cout << C1.a << endl;//派生类对象不能直接访问基类public成员(相当于protect)
    cout << C1.f2() << endl;//派生类对象能间接访问基类public成员
  //cout << C1.b << endl;//派生类对象不能直接访问基类protect成员(相当于protect)
    cout << C1.g2() << endl;//派生类对象能间接访问基类public成员
 // cout << C1.c << endl;//派生类对象不能直接访问基类private成员(相当于private)
 // cout << C1.h2() << endl;//派生类对象能间接访问基类private成员,调函数
    cout << C1.h22() << endl;

    D D1;//private继承
  //  cout << D1.a << endl;//派生类对象能直接访问基类public成员(相当于private)
    cout << D1.f3() << endl;//派生类对象能间接访问基类private成员
   // cout << D1.b << endl;//派生类对象不能直接访问基类protect成员(相当于private)
    cout << D1.g3() << endl;//派生类对象能间接访问基类public成员
  //cout << D1.c << endl;//派生类对象不能直接访问基类private成员(相当于private)
  //cout << D1.h3() << endl;//派生类对象能间接访问基类private成员,调函数
    cout <<D1.h33() << endl;
    
    cout << "Hello World!" << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值