实验目的
1.掌握派生类的声明方法和派生类构造函数的定义方式
2.掌握不同方式下,构造函数与析构函数的执行顺序与构造规则
实验内容
MyArray.cpp代码如下:
#include<iostream>
#include"MyArray.h"
using namespace std;
MyArray::MyArray(int leng) {
if (leng <= 0)
{
cout << "error length";
exit(1);
}
length = leng;
alist = new int[length];
// length = leng;
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 << " ";
}
cout << endl;
}
void MyArray::Input()
{
cout << "请从键盘输入" << length <&l