类模板案例_数组类封装

案例描述:实现一个通用的数组类,要求如下:

  • 可以对内置数据类型以及自定义数据类型的数据进行存储
  • 将数据中的数据存储到堆区(new)
  • 构造函数中可以传入数组的容量
  • 提供对应的拷贝构造函数以及opertor= 防止浅拷贝问题
  • 提供尾插法和尾删法对数组中的数据进行增加或删除
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组中当前元素个数和数组的容量

实现前四:

MyArray.hpp

//自己通用的数组类
#pragma once
#include
#include
using namespace std;

template<class T>
class MyArray
{
public:
	//有参构造,参数容量
	MyArray(int capacity)
	{
		cout << "MyArray的有参构造调用" << endl;
		this->m_Capacity = capacity;
		this->m_Size = 0;
		//T * pAddress= new T[];
		this->pAddress = new T[this->m_Capacity];
	}

	//拷贝构造函数
	MyArray(const MyArray & arr)
	{
		cout << "MyArray的拷贝构造调用" << endl;
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//this->pAddress = arr.pAddress;		浅拷贝回导致堆区数据重复释放
		//重新在堆区开辟数据
		this->pAddress = new T[arr.m_Capacity];
		//将arr中的数据拷贝
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	//operator= 防止浅拷贝 重载=号
	MyArray & operator=(const MyArray & arr)
	{
		cout << "MyArray的operator=调用" << endl;
		//先判断原来堆区是否有数据
		if (this->pAddress != NULL)
		{
			delete[]this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		//深拷贝
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[arr.m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}

	//析构函数
	~MyArray()
	{
		cout << "MyArray析构调用" << endl;
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
private:
	T * pAddress;	//指针指向堆区开辟的真实数组

	int m_Capacity;		//数组容量

	int m_Size;		//数组大小
};

数组类封装.cpp

#include
using namespace std;
#include"MyArray.hpp"

void test01()
{
	MyArray <int>arr1(5);

	MyArray<int> arr2(arr1);

	MyArray<int>arr3(100);
	arr3 = arr1;
}
int main()
{
	test01();
	system("pause");
	return 0;
}
-------------------------------------------------------------------------------------
MyArray的有参构造调用
MyArray的拷贝构造调用
MyArray的有参构造调用
MyArray的operator=调用
MyArray析构调用
MyArray析构调用
MyArray析构调用
请按任意键继续. . .

整理实现

MyArray.hpp

//自己通用的数组类
#pragma once
#include
#include
using namespace std;

template<class T>
class MyArray
{
public:
	//有参构造,传入参数:参数容量
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		//T * pAddress= new T[];
		this->pAddress = new T[this->m_Capacity];
	}

	//拷贝构造函数
	MyArray(const MyArray & arr)
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//this->pAddress = arr.pAddress;		浅拷贝回导致堆区数据重复释放
		//重新在堆区开辟数据
		this->pAddress = new T[arr.m_Capacity];
		//将arr中的数据拷贝
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	//operator= 重载=号 防止浅拷贝 
	MyArray & operator=(const MyArray & arr)
	{
		//先判断原来堆区是否有数据
		if (this->pAddress != NULL)
		{
			delete[]this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		//深拷贝
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
        //重新在堆区开辟数据
		this->pAddress = new T[arr.m_Capacity];
        将arr中的数据拷贝
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}

	//operator[] 重载[],让用户通过下标方式访问数组中的元素 
	//传入参数:int 数组下标
	T& operator[](int index)	//为了保证其返回值可以作为左值存在,返回值类型应为 T&
	{
		return this->pAddress[index];
	}
    
    //尾插法
	void Push_Back(const T &val)
	{
		//判断容量是否等于大小
		if (this->m_Capacity == this->m_Size)
		{
			cout << "容量已满" << endl;
			return;
		}
		//插到数组的最后
		this->pAddress[this->m_Size] = val;
		this->m_Size++;		//更新数组的大小
	}

	//尾删法
	void Pop_Back()
	{
		//让用户访问不到最后一个元素,即为尾删 逻辑删除
		if (this->m_Size == 0)
		{
			cout << "数组中没有元素" << endl;
			return;
		}
		this->m_Size--;
	}


	//返回数组的容量
	int getCapacity()
	{
		return this->m_Capacity;
	}

	//返回数组的大小
	int getSize()
	{
		return this->m_Size;
	}

	//析构函数
	~MyArray()
	{
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
    
private:
	T * pAddress;	//指针指向堆区开辟的真实数组

	int m_Capacity;		//数组容量

	int m_Size;		//数组大小
};

main.cpp

#include
using namespace std;
#include
#include"MyArray.hpp"

void printIntArray(MyArray <int>arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr[i] << " ";
	}
}

void test01()
{
	MyArray <int>arr1(5);

	for (int i = 0; i < 5; i++)
	{
		//利用尾插法向数组中插入数据
		arr1.Push_Back(i);
	}

	cout << "arr1的打印输出为:" <<" ";
	printIntArray(arr1);
	cout << endl;

	cout << "arr1的容量为:" << arr1.getCapacity() << endl;
	cout << "arr1的大小为:" << arr1.getSize() << endl;

	MyArray<int> arr2(arr1);

	cout << "arr2的打印输出为:" << " ";
	printIntArray(arr2);
	cout << endl;

	//尾删
	arr2.Pop_Back();
	cout << "尾删后arr2的打印输出为:" << " ";
	printIntArray(arr2);
	cout << endl;
	cout << "尾删后arr2的容量为:" << arr2.getCapacity() << endl;
	cout << "尾删后arr2的大小为:" << arr2.getSize() << endl;
}

//测试自定义数据类型
class Person
{
public:

	Person() {};

	Person(string name,int age)
	{
		this->m_name = name;
		this->m_age = age;
	}

	string m_name;
	int m_age;
};

void printPersonArray(MyArray<Person> &arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << "name:" << arr[i].m_name << " age:" << arr[i].m_age << endl;
	}
}

void test02()
{
	MyArray<Person> arr(10);

	Person p1("胡图图", 23);
	Person p2("胡英俊", 46);
	Person p3("张晓丽", 44);
	Person p4("小美", 23);
	Person p5("刷子", 23);

	//将数据插入到数组中
	arr.Push_Back(p1);
	arr.Push_Back(p2);
	arr.Push_Back(p3);
	arr.Push_Back(p4);
	arr.Push_Back(p5);

	//打印数组
	printPersonArray(arr);

	//输出容量和大小
	cout << "arr的容量:" << arr.getCapacity() << endl;
	cout << "arr的大小:" << arr.getSize() << endl;
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
---------------------------------------------------------------------------------
测试int数据类型
arr1的打印输出为: 0 1 2 3 4
arr1的容量为:5
arr1的大小为:5
arr2的打印输出为: 0 1 2 3 4
尾删后arr2的打印输出为: 0 1 2 3
尾删后arr2的容量为:5
尾删后arr2的大小为:4
请按任意键继续. . .
---------------------------------------------------------------------------------
测试自定义数据类型
name:胡图图 age:23
name:胡英俊 age:46
name:张晓丽 age:44
name:小美 age:23
name:刷子 age:23
arr的容量:10
arr的大小:5
请按任意键继续. . .

你可能感兴趣的:(C++,c++)