面向对象编程实验课随笔(承继下的构造函数和析构函数)

一、试验内容

1.将示例代码用电脑运行,并展示运行结果。

2.声明一个SortArray继承类MyArray,在类中定义一个函数,具有将输入的整数从小到大进行排序的功能。

二、试验步骤

1.运行示例代码,并运行

#include<iostream>
#include<string>
using namespace std;
class MyArray
{
public:
	MyArray(int leng);
	~MyArray();
	void Input();
	void Display(string);
protected:
	int* alist;
	int length;
};
MyArray::MyArray(int leng)
{
	if (leng <= 0)
	{
		cout << "error length";
		exit(1);
	}
	length = leng;
	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++)
	{
		cou
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值