数组类

Posted on 2016-06-01 12:41  洪爵士  阅读(146)  评论(0编辑  收藏  举报

MyArray.h

#pragma once
#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>
using namespace std;
//int 类型的数组类
class MyArray
{
public:
	friend ostream& operator<<(ostream &out,MyArray &myarray);//友元函数声明
	friend istream& operator>>(istream &in,MyArray &myarray);//友元函数声明
	MyArray();
	MyArray(int len);
	MyArray(const MyArray &another);
	~MyArray();
	void setData(int index,int value);
	int getData(int index);
	int getLen();
	int& operator[](int index);//为了可以当左值返回引用
	MyArray& operator=(MyArray &another);
	bool operator==(const MyArray &another);
	bool operator!=(const MyArray &another);
private:
	int len; //这个连续数组空间目前能够存放的元素个数
	int *space; //space 在堆上开辟连续数组空间的首地址
};

  MyArray.cpp

#include "MyArray.h"
#include <iostream>
MyArray::MyArray()
{
	this->space=NULL;
	this->len=0;
}
MyArray::MyArray(int len)
{
	if (len<=0) 
	{
		this->len=0;
		return;
	}
	else
	{
		this->len=len;
		//给space 开辟内存
		this->space=new int[this->len];
	}
}
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];
		}
	}
}
MyArray::~MyArray()
{
	//目的是释放 array 额外开辟的space空间
	if (this->space!=NULL)
	{
		delete[] this->space;
		this->len=0;
		this->space=NULL;
	}
}
void MyArray::setData(int index,int value)//这里可能会越界
{
	this->space[index]=value;
}
int MyArray::getData(int index)
{
	return this->space[index];
}
int MyArray::getLen()
{
	return this->len;
}
int& MyArray::operator[](int index)
{
	return this->space[index];
}
MyArray& MyArray::operator=(MyArray &another)
{

	if(this->space!=NULL)//释放旧的内存
	{
		delete[] space;
		len=0;
	}
	//根据another的大小重新分配内存
	this->len=another.len;
	this->space=new int[this->len];
	//拷贝数据
	for(int i=0;i<len;i++)
	{
		this->space[i]=another.space[i];
	}
	return *this;
}
bool MyArray::operator==(const MyArray &another)
{
	if(this->len!=another.len)//先判断数组的长度
	{
		return false;
	}
	for(int i=0;i<this->len;i++)
	{
		if(this->space[i]!=another.space[i])
		{
			return false;
		}
	}
	return true;
}
bool MyArray::operator!=(const MyArray &another)
{
	/*if(this->len!=another.len)
	{
		return true;
	}
	for(int i=0;i<this->len;i++)
	{
		if(this->space[i]!=another.space[i])
		{
			return true;
		}
	}
	return false;*/
	return !(*this==another);//利用上一个重载的==	
}
ostream& operator<<(ostream &out,MyArray &myarray)
{
	for(int i=0;i<myarray.len;i++)
	{
		out<<myarray.space[i]<<" ";
	}
	return out; 
}
istream& operator>>(istream &in,MyArray &myarray)
{
	cout<<"请输入"<<myarray.len<<"个数:";
	for(int i=0;i<myarray.len;i++)
	{
		cin>>myarray.space[i];
	}
	return in;
}

  TestMyArray.cpp测试这个数组类

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include "MyArray.h"
using namespace std;
int main(void)
{
	MyArray array1(10);
	//给数组赋值
	for(int i=0;i<10;i++) 
	{
		int value=i+10;
		array1[i]=value;
		//array1.setData(i,value);
	}
	//遍历数组
	/*for (int i=0;i<array1.getLen();i++)
	{
		//cout<<array1.getData(i)<<" ";
	}*/
	cout<<array1;
	MyArray array2=array1;//这是初始化 调用的是拷贝构造函数 再次强调构造函数只有初始化的时候调用
	MyArray s;
	s=array1;//这是赋值调用的是=操作符
	cout<<endl;
	//array2[9]=3;
	for(int i=0;i<10;i++)
	{
		//cout<<array2.getData(i)<<" ";	
		cout<<s[i]<<" ";
	}
	cout<<endl;
	if(array1==array2)
	{
		cout<<"array1==array2"<<endl;
	}
	if(array1!=array2)
	{
		cout<<"array1!=array2"<<endl;
	}
	cin>>array1;
	cout<<array1<<endl;;
	return 0;
}