类的继承问题.cpp

来自「c++实例~ 初学基础」· C++ 代码 · 共 68 行

CPP
68
字号
#include <iostream.h>

class CBase
{
public:
	CBase()
	{
		myintdata=new int[50];
		cout<<"CBase::Constructor,allocating 50 integers!"<<endl;
	}
	~CBase()
	{
		if(myintdata)
		{
			delete[] myintdata;
			cout<<"CBase::Destructor,deallocating 50 integers!"<<endl;
		}
	}
protected:
	int * myintdata;
};

class CDerived:public CBase
{
public:
	CDerived()
	{
		
		myfloatdata=new float[50];
		cout<<"CDerived::Constructor,allocating 50 floating numbers !"<<endl;
	}
	~CDerived()
	{
		if(myfloatdata)
		{
			delete[] myfloatdata;
			cout<<"CDerived::Destructor,deallocating 50 floating numbers !"<<endl;
		}
	}
protected:
	float * myfloatdata;
};

class CDerivedDerived:public CDerived
{
public:
	CDerivedDerived()
	{
		mydoubledata=new double[50];
		cout<<"CDerivedDerived::Constructor,allocating 50 double floating numbers !"<<endl;
	}
	~CDerivedDerived()
	{
		if(mydoubledata)
		{
			delete[] mydoubledata;
			cout<<"CDerivedDerived::Destructor,deallocating 50 double floating numbers !"<<endl;
		}
	}
protected:
	double * mydoubledata;
};

void main()
{
	CDerivedDerived myobject;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?