案例要求:创建一个数组类模板
//可以对内置数据类型以及自定义数据类型的数据进行存储
//将数组中的数据存储到堆区
//构造函数中可以传入数组的容量
//提供对应的拷贝构造函数以及operator = 防止浅拷贝问题
//提供尾插法和尾删法对数组中的数据进行增加和删除
//可以通过下标的方式访问数组中的元素
//可以获取数组中当前元素个数和数组的容量
分析:首先创建一个类模板,包含构造函数、析构函数、拷贝函数、operator=、尾插、尾删、operator[] 、获取容量大小、获取元素个数。
代码实现:
MyArray.hpp
#pragma once
#include <iostream>
using namespace std;
#include <string>
//可以对内置数据类型以及自定义数据类型的数据进行存储
//将数组中的数据存储到堆区
//构造函数中可以传入数组的容量
//提供对应的拷贝构造函数以及operator = 防止浅拷贝问题
//提供尾插法和尾删法对数组中的数据进行增加和删除
//可以通过下标的方式访问数组中的元素
//可以获取数组中当前元素个数和数组的容量
template <class T>
class MyArray
{
public:
//构造函数是用来 初始化属性的
MyArray(int capacity)
{
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];//开辟一个m_Capacity大小的堆区空间,类型为T
}
//拷贝函数 深拷贝防止浅拷贝
MyArray(const MyArray& arr)
{
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//this->pAddress = arr.pAddress;//编辑器默认提供的浅拷贝,开辟堆区的数据要用深拷贝!
this->pAddress = new T[this->m_Capacity];//深拷贝
//以前的数组数据先拷过来
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_Size = 0;
this->m_Capacity = 0;
}
//重新分配内存空间
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddress = new T[this->m_Capacity];
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this; //最后返回* this
}
//尾插法 可以插入 int、double以及自定义类型的数据
void Push_Back(const T & val)
{
if (this->m_Capacity == this->m_Size)
{
return;//数组已满,插不了数据了
}
this->pAddress[this->m_Size] = val;
this->m_Size++;
}
//尾删法
void Pop_Back()
{
if (this->m_Size == 0)
{
return;
}
this->m_Size--;
}
//获取数组容量
int getCapacity()
{
return this->m_Capacity;
}
//获取数组大小,是获取数组中有多少个元素
int getSize()
{
return this->m_Size;
}
//重载 []
T& operator[](int index)
{
return this->pAddress[index];
}
~MyArray()
{
if (this->pAddress != NULL)
{
delete[]this->pAddress;
this->pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
}
private:
T* pAddress;//指向一个堆空间,这个空间存储真正的数据
int m_Capacity; //容量
int m_Size; // 大小
};
MyArray.cpp
#include <iostream>
using namespace std;
#include "MyArray.hpp"
//创建一个打印int型的函数
void myPrint(MyArray<int> & arr)
{
for (int i = 0; i < arr.getSize(); i++)
{
cout << arr[i] << " ";//如果不对[]重载,这里会报错 想要把i返回到arr[i]中
}
cout << endl;
}
void test01()
{
MyArray<int> arr1(10);//实例化10个int类型的数组
for (int i = 0; i < 10; i++)
{
arr1.Push_Back(i);
}
cout << "arr1的容量:" << arr1.getCapacity() << endl;
cout << "arr1的大小为:" << arr1.getSize() << endl;
myPrint(arr1);
cout << "----------------------------" << endl;
MyArray<int> arr2(arr1);//拷贝构造
arr2.Pop_Back();
cout << "arr2的容量:" << arr2.getCapacity() << endl;
cout << "arr2的大小为:" << arr2.getSize() << endl;
myPrint(arr2);
cout << "----------------------------" << endl;
MyArray<int> arr3=arr2;//operator= 赋值
cout << "arr3的容量:" << arr3.getCapacity() << endl;
cout << "arr3的大小为:" << arr3.getSize() << endl;
myPrint(arr3);
}
//自定义数据类型测试
class Person
{
public:
Person() {};//构造函数调用规则:写了有参构造编译器就不再提供默认构造,所以自己要写出来
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void myPerson_Print(MyArray<Person>& arr)//要打印什么玩意,就要传进去它的类型
{
for (int i = 0; i < arr.getSize(); i++)
{
cout << "姓名:" << arr[i].m_Name << " 年龄" << arr[i].m_Age << endl;
}
}
void test02()
{
MyArray<Person> arr1(10);//实例化10个Person类型的数组
Person p1("路飞", 18);
Person p2("索隆", 20);
Person p3("娜美", 18);
arr1.Push_Back(p1);
arr1.Push_Back(p2);
arr1.Push_Back(p3);
cout << "arr3的容量:" << arr1.getCapacity() << endl;
cout << "arr3的大小为:" << arr1.getSize() << endl;
myPerson_Print(arr1);
cout << "----------------------------" << endl;
MyArray<Person> arr2(arr1);
arr2.Pop_Back();//把"娜美"干掉,路飞和索隆才是真爱
cout << "arr2的容量:" << arr2.getCapacity() << endl;
cout << "arr2的大小为:" << arr2.getSize() << endl;
myPerson_Print(arr2);
cout << "----------------------------" << endl;
MyArray<Person> arr3 = arr2;
cout << "arr3的容量:" << arr3.getCapacity() << endl;
cout << "arr3的大小为:" << arr3.getSize() << endl;
myPerson_Print(arr3);
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
测试结果:
test01():
test02():