需求分析:

1、数组初始化,构造,拷贝(深),析构
MyArray.hpp
#pragma once
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
//有参构造 参数 容量
MyArray(int capacity)
{
cout << "MyArray的有参构造调用" << endl;
this->m_Capacity = capacity;
this->m_Size = 0;
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 = 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];
//如果原来就有数据量,将arr中的数据都拷贝过来
for (int i=0;i<this->m_Size;i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
//析构函数
~MyArray()
{
if (this->pAddress != NULL)
{
cout << "MyArray的析构函数调用" << endl;
delete[]pAddress;
pAddress = NULL;
}
}
private:
T* pAddress;//指针指向堆区开辟的数组
int m_Capacity;//数组容量
int m_Size;//数组太小
};
实现测试:
#include<iostream>
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;
}
结果:

2、尾插法,尾删法插入或删除数组中元素;获取容量,大小,更新大小;测试插入自定义数据类型
MyArray.hpp
#pragma once
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
//有参构造 参数 容量
MyArray(int capacity)
{
cout << "MyArray的有参构造调用" << endl;
this->m_Capacity = capacity;
this->m_Size = 0;
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 = 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];
//如果原来就有数据量,将arr中的数据都拷贝过来
for (int i=0;i<this->m_Size;i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
//尾插法
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--;
}
//通过下标的方式访问数组中的元素
T& operator[](int index)
{
return this->pAddress[index];
}
//返回数组容量
int getCapacity()
{
return this->m_Capacity;
}
//返回数组大小
int getSize()
{
return this->m_Size;
}
//析构函数
~MyArray()
{
if (this->pAddress != NULL)
{
cout << "MyArray的析构函数调用" << endl;
delete[]pAddress;
pAddress = NULL;
}
}
private:
T* pAddress;//指针指向堆区开辟的数组
int m_Capacity;//数组容量
int m_Size;//数组太小
};
测试:
#include<iostream>
using namespace std;
#include"MyArray.hpp"
void printIntArray(MyArray<int>&arr)
{
for (int i = 0; i < arr.getSize(); i++)
{
cout << arr[i] << " ";//由于前面重载了[]
}
cout << endl;
}
void test01()
{
MyArray<int>arr1(5);
for (int i = 0; i < 5; i++)
{
//利用尾插法向数组中插入数据
arr1.Push_Back(i);
}
printIntArray(arr1);
cout << "arr1的容量为:" << arr1.getCapacity() << endl;
cout << "arr1的大小为:" << arr1.getSize() << endl;
MyArray<int>arr2(arr1);
printIntArray(arr2);
//尾删
arr2.Pop_Back();
cout << "arr2尾删后:" << endl;
cout << "arr2的容量为:" << arr2.getCapacity() << endl;
cout << "arr2的大小为:" << arr2.getSize() << endl;
MyArray<int>arr3(100);
arr3 = arr1;
}
//测试自定义数据类型
class Person
{
public:
Person() {};
Person(string name, int age)
{
m_Name = name;
m_Age = age;
}
string m_Name;
int m_Age;
};
void printPersonArray(MyArray<Person>&arr)
{
for (int i = 0; i < arr.getSize(); i++)
{
cout << "姓名:" << arr[i].m_Name << " " << "年龄:" << arr[i].m_Age << endl;
}
}
void test02()//测试数组中存放person数据类型
{
MyArray<Person>arr(10);
Person p1("孙悟空", 999);
Person p2("韩信", 30);
Person p3("妲己", 20);
Person p4("赵云", 25);
Person p5("安琪拉", 27);
//将数据插入到数组中
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;
}
文章展示了如何使用C++实现一个名为MyArray的动态数组类,包括有参构造、拷贝构造(深拷贝)、赋值运算符重载(防止浅拷贝)、析构函数。此外,还实现了尾插法Push_Back、尾删法Pop_Back以及访问元素、获取容量和大小的方法。文章通过测试用例演示了类的功能,包括插入整数和自定义Person对象到数组中。
168

被折叠的 条评论
为什么被折叠?



