C++ 370-24题-1 继承

来源:互联网 发布:mysql服务器安装包 编辑:程序博客网 时间:2024/04/28 12:16

//结果:
//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();