//结果:
//small=2
//small=3
//small=2
#include <iostream.h>
class CRoot
{
public:
int small;
CRoot()
{
small=2;
}
CRoot(int n) //重载
{
small=n;
}
void showsmall()
{
cout<<"small="<<small<<endl;
}
};
class CDer1:public CRoot
{
public:
CDer1(int m):CRoot(m){}
};
class CDer2:public CRoot
{
public:
int small;
CDer2(int n=0){small=n;}
};
void main()
{
CRoot A;
CDer1 B(3);
CDer2 C;
A.showsmall();
B.showsmall();
C.showsmall();
}
本文通过具体的C++代码示例介绍了类继承的基本概念及成员函数重载的方法。示例中定义了一个基类CRoot和两个派生类CDer1、CDer2,演示了如何通过构造函数初始化成员变量并在派生类中调用基类的成员函数。
2299

被折叠的 条评论
为什么被折叠?



