上机五

#include
using namespace std;
class Base {
public:
void setx(int i)
{
x = i;
}
int getx()
{
return x;
}
public:
int x;
};
class Derived :public Base {
public:
void sety(int i)
{
y = i;
}
int gety()
{
return y;
}
void show()
{
cout << “Base::x=” << x << endl;
}
public:
int y;
};
int main()
{
Derived bb;
bb.setx(16);
bb.sety(25);
bb.show();
cout << “Base::x=” << bb.x << endl;
cout << “Derived::y=” << bb.y << endl;
cout << “Base::x=” << bb.getx() << endl;
cout << “Derived::y=” << bb.gety() << endl;
return 0;
}

1.将基类 Base 中数据成员 x 的访问权限改为 private 时,会出现哪些错误,为什么?
错误:Base::x无法访问
原因:x在Base声明中是private成员,派生类成员函数不能直接访问基类的私有成员。
2.将基类 Base 中数据成员×的访问权限改为 protected 时,会出现哪些错误,为什么?
错误:Base::x无法访问
原因:x在Base声明中是protected成员,是保护的,只允许内部和派生函数访问,类外不能调用。
3.在原程序的基础上,将派生类Derived 的继承方式改为 private 时,会出现哪些错误,为什么?
错误:Base::x不可访问;Base::getx不可访问;Base::setx不可访问。
原因:Derived使用private从Base无法继承。
4.在原程序的基础上,将派生类Derived 的继承方式改为 protected 时,会出现哪些错误,为什么?
错误:Base::x不可访问;Base::getx不可访问;Base::setx不可访问。
原因:Derived使用protected从Base继承,类外无法调用。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值