1实验内容
#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;
}
2 分析
基类中的私有成员,无论哪种继承方式,基类中的私有成员不允许派生类继承,派生类中是不可直接访问的。
基类中的公有成员,当类的继承方式为私有继承时,基类中的所有公有成员在派生类中都以私有成员的身份出现。
当类的继承方式为保护继承时,基类中的所有公有成员在派生类中都以保护成员的身份出现。
基类中的保护成员,当类的继承方式为公有继承时,基类中的所有保护成员在派生类中仍以保护成员的身份出现。



