(初学)继承和派生及里面各种定义1

派生类对基类各种成员的访问权限

继承方式:
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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值