c++ 运算符重载实现数组案例

#include<iostream>
using namespace std;
class Array {
public:
	Array(int m_length); //构造函数
	Array(const Array& obj); //拷贝构造函数
	~Array(); //析构函数
	int length(); //求取数组长度
	//a1[i] = i 需要重载[]操作符
	int &operator[](int i);
	Array &operator=(Array a1);
	bool operator==(Array& a1);
	bool operator!=(Array& a1);
private:
	int m_length;
	int* m_space;
};
// 构造函数
Array::Array(int m_length) {
	this->m_length = m_length;
	this->m_space = new int[m_length];
}
// 拷贝构造函数
Array::Array(const Array& obj) {
	this->m_length = obj.m_length;
	m_space = new int[m_length];
	for (int i = 0; i < obj.m_length; i++) {
		this->m_space[i] = obj.m_space[i];
	}
}
//析构函数
Array::~Array() {
	if (this->m_space != NULL) {
		delete[] m_space;
		this->m_space = NULL;
		m_length = 0;
	}
}
//求数组的长度
int Array::length() {
	return this->m_length;
}
// 重载[]操作符, 函数返回值做左值, 需要返回一个引用
int& Array::operator[](int i) {
	return this->m_space[i];
}
// 执行数组的深拷贝
Array& Array::operator=(Array a1) {
	if (this->m_space != NULL) {
		delete[] m_space;
		this->m_space = NULL;
		this->m_length = 0;
	}
	this->m_length = a1.m_length;
	this->m_space = new int[a1.m_length];
	for (int i = 0; i < a1.m_length; i++) {
		this->m_space[i] = a1.m_space[i];
	}
	return *this;
}
// 判断两个数组是否相等
bool Array::operator==(Array& a1) {
	if (this->m_length != a1.m_length) {
		return false;
	}
	for (int i = 0; i < a1.m_length; i++) {
		if (this->m_space[i] != a1.m_space[i]) {
			return false;
		}
	}
	return true;
}
// 重载 != 运算符, 判断两个数组是否不相等 
bool Array::operator!=(Array& a1) {
	if (this->m_length != a1.m_length) {
		return true;
	}
	for (int i = 0; i < a1.m_length; i++) {
		if (this->m_space[i] != a1.m_space[i]) {
			return true;
		}
	}
	return false;
}
int main()
{
	Array  a1(10);

	for (int i = 0; i < a1.length(); i++)
	{
		//a1.setData(i, i);
		//2 
		a1[i] = i;    //需要重载[]运算符

		//
		//函数返回值当左值,需要返回一个引用
		//a1.operator [i]
	}

	cout << "\n打印数组a1: ";
	for (int i = 0; i < a1.length(); i++)
	{
		//cout<<a1.getData(i)<<" ";
		//1
		cout << a1[i] << endl;
	}
	cout << endl;

	Array a2 = a1;
	//cout << "\n打印数组a2: ";
	//for (int i = 0; i < a2.length(); i++)
	//{
	//	cout << a2.getData(i) << " ";
	//}
	//cout << endl;

	//3
	Array a3(5);
	{
		a3 = a1; //重载=运算符 
		a3 = a2 = a1;

		cout << "\n打印数组a3: ";
		for (int i = 0; i < a3.length(); i++)
		{
			cout << a3[i] << " ";
		}

		//a3.operator=(a1)
		//Array& operator=(Array &a1)
	}


	//功能4

	if (a3 == a1)
	{
		printf("相等\n");
	}
	else
	{
		printf("不相等\n");
	}
	//a3.operator==(a1);
	//bool operator==(Array &a1);


	if (a3 != a1)
	{
		printf("不相等\n");
	}
	else
	{
		printf("相等\n");
	}
	//
	a3.operator!=(a1)
	 bool operator!=(Array &a1);


	cout << "hello..." << endl;
	system("pause");
	//return;
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值