派生类对基类各种成员的访问权限
继承方式:
public 可以对基类public protected成员公有访问
protected 可以对public 保护访问 protected公有访问
private 可以对public private私有访问
总之 派生类不能访问基类的private 其他类成员都可以访问
但根据继承方式不同 基类中的public和protected在派生类中的访问控制属性不同
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
class time{
public:
time(int h = 20,int m = 32,int s = 6) : h (h),m(m),s(s){}
void set_time(int h,int m,int s){h = h;m = m;s = s;}
void get_time(){cout << "h : " << h << " m : " << m << " s : " << s << '\n';}
protected://private 则派生类无法访问 protected则派生类里可以访问但对象实例不能访问
int h,m,s;
};
class time2 : public time{//jicheng
public:
time2(int y = 0,int mon = 0,int d = 0) : y(y),mon(mon),d(d){};
void get_fun(){//在派生类的函数里可以访问基类的protected,但对象实例不能访问
cout << "y : " << y << " mon : " << mon << " d : " << d << " h : " << h << " m : " << m << " s : " << s << '\n';
}
private:
int y,mon,d;
};
int main(){
time2 t(2020,9,17);
t.get_fun();
return 0;
}