数组类的构造析构以及运算符重载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FAKER_0X0/article/details/89317165

数组类的构造析构以及拷贝函数

//数组类的构造和析构

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>
using namespace std;

class Array
{
public:
	Array()
	{
		length = 0;
		space = NULL;
		cout << "Array() is run" << endl;
	}
	//有参构造函数
	Array(int len)
	{
		if (len <= 0) {
			length = 0;
			return;
		}
		length = len;
		space = new int[length];		//分配内存,整形数组最后无‘/0’
		cout << "Array() is run" << endl;
	}
	//拷贝构造函数
	Array(const Array& obj)
	{
		if (obj.length > 0) {	//大于0才需拷贝
			length = obj.length;
			space = new int[length];
			for (int i = 0; i < length; ++i) {
				space[i] = obj.space[i];
			}
			cout << "Array(const Array& obj) is run" << endl;
		}
	}
	int getLen()
	{
		return length;
	}
	void setData(int index, int value)	//参数为下标和对应元素数据
	{
		if (space != NULL) {
			space[index] = value;
		}
	}
	int getData(int index)
	{
		return space[index];
	}
	~Array()
	{
		if (space != NULL) {
			delete[] space;
			length = 0;
			space = NULL;
			cout << "~Array() is run" << endl;
		}
	}

private:
	int length;
	int *space;
};

int main()
{
	Array a1(10);
	for (int i = 0; i < a1.getLen(); ++i) {		//为数组每个元素赋值
		a1.setData(i, i + 20);
	}
	for (int i = 0; i < a1.getLen(); ++i) {		//输出数组每个元素
		cout << a1.getData(i) << " ";
	}
	cout << endl;
	Array a2 = a1;	//调用拷贝构造函数
	for (int i = 0; i < a2.getLen(); ++i) {		//输出数组每个元素
		cout << a2.getData(i) << " ";
	}
	cout << endl;
	return 0;
}

数组类运算符重载 [] = == !=

//数组类的构造和析构
//重载运算符 = [] == !=
#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>
using namespace std;

class Array
{
public:
	Array()
	{
		length = 0;
		space = NULL;
		cout << "Array() is run" << endl;
	}
	//有参构造函数
	Array(int len)
	{
		if (len <= 0) {
			length = 0;
			return;
		}
		length = len;
		space = new int[length];		//分配内存,整形数组最后无‘/0’
		cout << "Array() is run" << endl;
	}
	//拷贝构造函数
	Array(const Array& obj)
	{
		if (obj.length > 0) {	//大于0才需拷贝
			length = obj.length;
			space = new int[length];
			for (int i = 0; i < length; ++i) {
				space[i] = obj.space[i];
			}
			cout << "Array(const Array& obj) is run" << endl;
		}
	}
	//重载运算符[]
	int& operator[](int i)		//函数返回值当左值时需要返回一个引用
	{
		return space[i];	//很简单,直接返回对应元素内容
	}
	//重载运算符=
	Array& operator=(Array& obj2)
	{
		if (space != NULL) {	//1 释放旧内存
			delete[] space;
			length = 0;
		}
		length = obj2.length;	//2 分配新内存
		space = new int[length];
		for (int i = 0; i < length; ++i) {		//3 复制数组内容
			space[i] = obj2[i];
		}
		return *this;
	}
	//重载==
	bool operator==(Array& obj3)
	{
		if (length != obj3.length)
			return false;
		for (int i = 0; i < length; ++i) {
			if (space[i] != obj3[i])
				return false;
		}
		return true;
	}
	//重载!=
	bool operator!=(Array& obj4)
	{
		return !(*this == obj4);	//利用上面已重载的==
	}

	int getLen()
	{
		return length;
	}
	void setData(int index, int value)	//参数为下标和对应元素数据
	{
		if (space != NULL) {
			space[index] = value;
		}
	}
	int getData(int index)
	{
		return space[index];
	}
	~Array()
	{
		if (space != NULL) {
			delete[] space;
			length = 0;
			space = NULL;
			cout << "~Array() is run" << endl;
		}
	}

private:
	int length;
	int *space;
};

int main()
{
	Array a1(10);
	for (int i = 0; i < a1.getLen(); ++i) {		//为数组每个元素赋值
		//a1.setData(i, i + 20);	
		//思路:a1[i]=i+20,即a1.operator[](index)
		a1[i] = i + 20;	//[]重载成功
	}
	for (int i = 0; i < a1.getLen(); ++i) {		//输出数组每个元素
		//cout << a1.getData(i) << " ";
		cout << a1[i] << " ";
	}
	cout << endl;
	Array a2(5), a3;
	a3 = a2 = a1;	//链式编程,=重载成功
	for (int i = 0; i < a2.getLen(); ++i) {		//输出数组每个元素
		//cout << a2.getData(i) << " ";
		cout << a2[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < a3.getLen(); ++i) {		//输出数组每个元素
		cout << a3[i] << " ";
	}
	cout << endl;
	if (a1 == a3)
		cout << "equal" << endl;
	else
		cout << "not equal" << endl;
	if (a1 != a2)
		cout << "not equal" << endl;
	else
		cout << "equal" << endl;
	return 0;
}

运算符 []是二元运算符,[] 运算符用于访问数据对象的元素。
重载格式 类型 类 :: operator[] ( 类型 ) ;
设 x 是类 X 的一个对象,则表达式
x [ y ]
可被解释为
x . operator [ ] ( y )
函数返回值当左值时需要返回一个引用。

增加 << 和 >> 运算符重载

//数组类的构造和析构
//重载运算符 = [] == != 加上<< >>
#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>
using namespace std;

class Array
{
public:
	Array()
	{
		length = 0;
		space = NULL;
		cout << "Array() is run" << endl;
	}
	//有参构造函数
	Array(int len)
	{
		if (len <= 0) {
			length = 0;
			return;
		}
		length = len;
		space = new int[length];		//分配内存,整形数组最后无‘/0’
		cout << "Array() is run" << endl;
	}
	//拷贝构造函数
	Array(const Array& obj)
	{
		if (obj.length > 0) {	//大于0才需拷贝
			length = obj.length;
			space = new int[length];
			for (int i = 0; i < length; ++i) {
				space[i] = obj.space[i];
			}
			cout << "Array(const Array& obj) is run" << endl;
		}
	}
	//重载运算符[]
	int& operator[](int i)		//函数返回值当左值时需要返回一个引用
	{
		return space[i];	//很简单,直接返回对应元素内容
	}
	//重载运算符=
	Array& operator=(Array& obj2)
	{
		if (space != NULL) {	//1 释放旧内存
			delete[] space;
			length = 0;
		}
		length = obj2.length;	//2 分配新内存
		space = new int[length];
		for (int i = 0; i < length; ++i) {		//3 复制数组内容
			space[i] = obj2[i];
		}
		return *this;
	}
	//重载==
	bool operator==(Array& obj3)
	{
		if (length != obj3.length)
			return false;
		for (int i = 0; i < length; ++i) {
			if (space[i] != obj3[i])
				return false;
		}
		return true;
	}
	//重载!=
	bool operator!=(Array& obj4)
	{
		return !(*this == obj4);	//利用上面已重载的==
	}
	//重载<< >>,只能使用友元函数重载
	friend ostream& operator<<(ostream& out, Array& obj5);
	friend istream& operator>>(istream& in, Array& obj6);

	int getLen()
	{
		return length;
	}
	void setData(int index, int value)	//参数为下标和对应元素数据
	{
		if (space != NULL) {
			space[index] = value;
		}
	}
	int getData(int index)
	{
		return space[index];
	}
	~Array()
	{
		if (space != NULL) {
			delete[] space;
			length = 0;
			space = NULL;
			cout << "~Array() is run" << endl;
		}
	}

private:
	int length;
	int *space;
};
//
ostream& operator<<(ostream& out, Array& obj5)
{
	cout << "The elements of the array are:" << endl;
	for (int i = 0; i < obj5.getLen(); ++i) {		//输出数组每个元素
		out << obj5[i] << " ";	//函数中怎么返回数组输出的多个值??
	}							//似乎out可以当成存储多个输出元素的数组
	cout << endl;
	return out;
	
}
istream& operator>>(istream& in, Array& obj6)
{
	cout << "Please input " << obj6.getLen() << " elements:" << endl;
	for (int i = 0; i < obj6.getLen(); ++i) {		
		in >> obj6[i];
	}
	return in;
}

int main()
{
	Array a1(5);
	//for (int i = 0; i < a1.getLen(); ++i) {		//为数组每个元素赋值
	//											//a1.setData(i, i + 20);	
	//											//思路:a1[i]=i+20,即a1.operator[](index)
	//	a1[i] = i + 20;	//[]重载成功
	//}

	cin >> a1;

	//for (int i = 0; i < a1.getLen(); ++i) {		//输出数组每个元素
	//											//cout << a1.getData(i) << " ";
	//	cout << a1[i] << " ";
	//}
	//cout << endl;
	cout << a1;

	//Array a2(5), a3;
	//a3 = a2 = a1;	//链式编程,=重载成功
	//for (int i = 0; i < a2.getLen(); ++i) {		//输出数组每个元素
	//											//cout << a2.getData(i) << " ";
	//	cout << a2[i] << " ";
	//}
	//cout << endl;
	//for (int i = 0; i < a3.getLen(); ++i) {		//输出数组每个元素
	//	cout << a3[i] << " ";
	//}
	//cout << endl;
	//if (a1 == a3)
	//	cout << "equal" << endl;
	//else
	//	cout << "not equal" << endl;
	//if (a1 != a2)
	//	cout << "not equal" << endl;
	//else
	//	cout << "equal" << endl;
	return 0;
}

重载<<可以直接返回out!

猜你喜欢

转载自blog.csdn.net/FAKER_0X0/article/details/89317165