实验目的
掌握派生类的声明方法和派生类构造函数的定义方法
掌握不同方式下,构造函数与析构函数的执行顺序与构造规则
程序如下:
#include<iostream>
#include<string>
using namespace std;
class MyArray {
public:
MyArray(int length);
~MyArray();
void Input();
void Display(string);
protected:
int* alist;
int length;
};
MyArray::MyArray(int leng)
{
length = leng;
if (leng <= 0)
{
cout << "error length";
exit(1);
}
alist = new int[length];
if (alist == NULL)
{
cout << "assign failure";
exit(1);
}
cout << "MyArray类对象已创建!" << endl;
}
MyArray::~MyArray()
{
delete[] alist;
cout << "MyArray类对象已撤销!" << endl;
}
void MyArray::Display(string str)
{
int i;
int* p = alist;
cout << str << length << "个整数:";
for (i = 0; i < length; i++, p++)
cout << *p << ""