实验七:继承下的构造函数与析构函数

一.实验目的:

 二.实验内容:

#include "twj.h"

using namespace std;

class MyArray {
public:
	MyArray(int length);
	~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++)
	{
		cout << *p << " ";
	}
	cout << endl;
}
void MyArray::Input()
{
	cout << "请从键盘输入" << length << "个整数:";
	int i;
	int* p = alist;
	for (i = 0; i < length; i++, p++)
	{
		cin >> *p;
	}
}
int main()
{
	//SortArray a(5);
	MyArray a(5);
	a.Input();
	a.Display("显示已经输入的");
	//a.Sort();
	return 0;
}

实验结果:

内容二:

 添加修改之后代码如下:

#include "twj.h"

using namespace std;

class MyArray {
public:
	MyArray(int length);
	~MyArray();
	void Input();
	void Display(string);
protected:
	int* alist;
	int length;
};


class SortArray :public MyArray {
public:
	SortArray(int length)
		:MyArray(length)
	{
		cout << "SortArray类对象已创建!" << endl;
	}
	~SortArray()
	{
		cout << "SortArray类对象已撤销!" << endl;
	}
	void Sort();
};


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++)
	{
		cout << *p << " ";
	}
	cout << endl;
}
void MyArray::Input()
{
	cout << "请从键盘输入" << length << "个整数:";
	int i;
	int* p = alist;
	for (i = 0; i < length; i++, p++)
	{
		cin >> *p;
	}
}

void SortArray::Sort()
{
	int i;
	int j;
	int temp = 0;
	int* p = alist;
	int* q = alist;
	cout << "未排序的" << length << "个整数:";
	for (i = 0; i < length; i++, p++)
	{
		cout << *p << " ";
	}
	cout << endl << "经排序后的" << length << "个整数:";


	for (i = 0; i < length; i++)
	{
		for (j = i; j < length; j++)
		{
			if (*(q + j) < *(q + i))
			{
				temp = *(q + i);
				*(q + i) = *(q + j);
				*(q + j) = temp;
			}
		}
		cout << *(q + i) << " ";
	}
	cout << endl;
}

int main()
{
	SortArray a(5);
	//MyArray a(5);
	a.Input();
	//a.Display("显示已经输入的");
	a.Sort();
	return 0;
}

实验结果:

三:实验总结

1.构造函数语法:类名(){}

  1. 构造函数,没有返回值也不写void
  2. 函数名称与类名相同
  3. 构造函数可以有参数,因此可以发生重载
  4. 程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次

2.析构函数语法: ~类名(){}

  1. 析构函数,没有返回值也不写void
  2. 函数名称与类名相同,在名称前加上符号 ~
  3. 析构函数不可以有参数,因此不可以发生重载
  4. 程序在对象销毁前会自动调用析构,无须手动调用,而且只会调用一次

3. 对象构造过程中执行的顺序

      1.先构造基类的成员

      2.执行基类的构造函数(在执行构造函数里我们写的代码之前基类的成员已经创建好了)

      3.构造派生类的成员

      4.执行派生类的构造函数

4..对象析构过程中的执行顺序

      1.先执行派生类的析构函数

      2.销毁派生类的成员变量

      3.再执行基类的析构函数

      4.销毁基类的成员变量

更多见构造函数和析构函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值