认识C++中的类-2

//writer:zyli
//date:20190623
//构建child类,认识protect修饰:
#include
using namespace std;
class rectangle{
//private修饰的成员变量只有类内可直接访问,类的实例要通过成员函数才可以访问 ;
private:
int length;
//public修饰的成员变量,实例可以直接访问;
public:
void set_length(int len);
protected:
//用protect修饰的成员,跟私有成员一样,无法被外界直接访问,但是能被子类直接访问;
int width=15;
void set_length2(int len);
};

void rectangle::set_length(int len)
{
cout<<“set length”<<endl;
this->length=len;
cout<<“length=”<<length<<endl;
}
void rectangle::set_length2(int len)
{
cout<<“set length2”<<endl;
this->length=len;
cout<<“length2=”<<len<<endl;
}

class child:public rectangle{
public:
void set_child(int a);
};
void child::set_child(int a)
{
this->set_length2(a);
cout<<“width=”<width<<endl;
}
int main()
{
child chi;
chi.set_length(5);
chi.set_child(15);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值