数组类的实现

自己写一个类,实现数组功能


MyArray.h

#ifndef __MYARRAY_H__
#define __MYARRAY_H__

class Array
{
public:
	Array(int a);
	Array(Array &obj);
	~Array();
	int length ();
	void setData (int index, int a);
	int getData (int index);
	int &operator[](int index)const;
	Array &operator=(const Array &obj);
	bool operator==(const Array &obj);
	bool operator!=(const Array &obj);
private:
	int m_len;
	int * m_p;
};




#endif

MyArray.cpp

#include "MyArray.h"
#include <iostream>

Array::Array(int a)
{
	m_len = a;
	m_p = new int[m_len]; 
}

int Array::length ()
{
	return m_len;
}

void Array::setData (int index, int a)
{
	m_p[index] = a;
}

int Array::getData (int index)
{
	return m_p[index];
}

Array::Array(Array &obj)
{
	m_len = obj.m_len;
	m_p = new int[obj.m_len];

	for(int i = 0; i < obj.m_len ; i++)
	{
		m_p[i] = obj.m_p[i];
	}
}

Array::~Array()
{
	delete []m_p;
}

int &Array::operator[](int index)const
{
	return m_p[index];
}

Array &Array::operator=(const Array &obj)
{
	if(this == &obj)
		return *this;

	if(m_p != NULL)
	{
		delete []m_p;
		m_p = NULL;
	}

	m_p = new int[obj.m_len];

	m_len = obj.m_len;
	for(int i = 0; i < obj.m_len ; i++)
	{
		m_p[i] = obj.m_p[i];
	}

	return *this;
}


bool Array::operator==(const Array &obj)
{
	if(m_len != obj.m_len)
		return false;

	for(int i = 0; i<m_len ; i++ )
	{
		if(m_p[i] != obj[i])
			return false;
	}
	return true;
}

bool Array::operator!=(const Array &obj)
{
	return !(*this == obj);
}

main.c

#include <iostream>
#include "MyArray.h"

using namespace std;

int main1()
{
	// 自己写MyArray.h 头文件和 MyArray.cpp 源文件 实现数组类 Array
	// 数组类的构造函数有一个 int 型参数 表示数组的大小
	Array  a1(10);

	// 数组类有一个 length 方法 用于获取数组的大小
	for (int i=0; i<a1.length(); i++)
	{
		// 设置数组的元素 ,第一个参数表示下标,第二个参数表示 下标对应的值
		a1.setData(i, i);
	}

	cout<<"\n打印数组a1: ";
	for (int i=0; i<a1.length(); i++)
	{
		// 获取数组元素 ,参数表示数组下标
		cout<<a1.getData(i)<<" ";
	}
	cout<<endl;

	// 完成数组的拷贝构造函数,允许两个数组对象互相赋值
	Array a2 = a1;
	cout<<"\n打印数组a2: ";
	for (int i=0; i<a2.length(); i++)
	{
		cout<<a2.getData(i)<<" ";
	}
	cout<<endl;


	cout<<"hello..."<<endl;
	return 0;
}

int main()
{
	Array  a1(10);

	for (int i=0; i < a1.length(); i++)
	{
		a1[i] = i;
	}

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

	Array a2 = a1;
	cout << "\n打印数组a2: ";
	for (int i=0; i< a2.length(); i++)
	{
		cout << a2[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] << " ";
		}
		cout << endl;
		//a3.operator=(a1)
		//Array& operator=(Array &a1)
	}

	//a3[2] = 100;
	//功能4
	if (a3 == a1)
	{
		printf("相等\n");
	}
	else
	{
		printf("不相等\n");
	}

	if (a3 != a1)
	{
		printf("不相等\n");
	}
	else
	{
		printf("相等\n");
	}

	cout<<"hello..."<<endl;

	system("pause");

	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值