#include <iostream>
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;
}
心得体会:在本次实验中,加深了我对类与继承也印象。基本掌握了三种继承方式的规律与区别。在今后的编程中,应更加注意这方面的问题,继承作为一个强大的功能更应掌握到位,不能出错。