重载[](自定义数组类)

.h

#include <iostream>

using namespace std;

class MyArray
{
public:
	MyArray();
	MyArray(int len);
	MyArray(const MyArray &another);
	~MyArray();

	void setData(int index, int data);
	int getData(int index);
	int getLen() const ;

	MyArray& operator=(const MyArray& another);

	int & operator[](int index) const;

	friend ostream &operator<<(ostream &os,const MyArray &array);
	friend istream &operator>>(istream &is, MyArray &array);

	friend bool operator==(MyArray &array1, MyArray &array2);
	bool operator!=(MyArray &another);
private:
	int len;
	int *space;
};

.cpp

#include "MyArray.h"


MyArray::MyArray()
{
	cout << "MyArray()..." << endl;
	this->len = 0;
	this->space = NULL;
}

MyArray::MyArray(int len)
{
	if (len <= 0) {
		this->len = 0;
		return;
	}
	else {
		this->len = len;

		//给space开辟空间
		this->space = new int[this->len];
		cout << "MyArray::MyArray(int len) ..." << endl;
	}
}
MyArray::MyArray(const MyArray &another)
{
	if (another.len >= 0) {
		this->len = another.len;

		//深拷贝
		this->space = new int[this->len];
		for (int i = 0; i < this->len; i++) {
			this->space[i] = another.space[i];
		}
		cout << "MyArray::MyArray(const MyArray &another) ..." << endl;

	}
}
MyArray::~MyArray()
{
	if (this->space != NULL) {
		delete[]this->space;
		this->space = NULL;
		len = 0;
		cout << "MyArray::~MyArray() ..." << endl;
	}
}

void MyArray::setData(int index, int data)
{
	if (this->space != NULL) {
		this->space[index] = data;
	}
}
int MyArray::getData(int index)
{
	return this->space[index];
}
int MyArray::getLen() const
{
	return this->len;
}

MyArray&  MyArray::operator=(const MyArray& another)
{
	if (this == &another) {
		return *this;
	}

	if (this->space != NULL) {
		delete[]this->space;
		this->space = NULL; 
		this->len = 0;
	}

	if (another.len >= 0) {
		this->len = another.len;

		//深拷贝
		this->space = new int[this->len];
		for (int i = 0; i < this->len; i++) {
			this->space[i] = another.space[i];
		}
		cout << "MyArray::operator=(const MyArray& another) ..." << endl;

	}

	return *this;
}

int & MyArray::operator[](int index) const
{
	return this->space[index];
}


ostream &operator<<(ostream &os,const MyArray &array)
{
	os << "遍历整个数组 " << endl;
	//array.getLen(); //getLen(&array);
	for (int i = 0; i < array.getLen(); i++) {
		os << array[i] <<" ";//array.operator[]( i)
	}

	os << "调用的<<操作符重载" << endl;

	return os;
}

istream &operator>>(istream &is, MyArray &array)
{
	cout << "请输入" << array.getLen() << "个数" << endl;
	for (int i = 0; i < array.getLen(); i++) {
		cin >> array[i];
	}
	return is;
}


bool operator==(MyArray &array1, MyArray &array2)
{
	if (array1.len != array2.len) {
		return false;
	}

	for (int i = 0; i < array1.len; i++) {
		if (array1.space[i] != array2.space[i]) {
			return false;
		}
	}

	return true;
}
bool MyArray::operator!=(MyArray &another)
{
	return !(*this == another);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值