c+±基础知识-类模板案例-数组封装
要求:
- 可以对内置类型以及自定义数据类型的数据进行存储
- 将数组中的数据存储到堆区
- 构造函数中可以传入数组的容量
- 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
- 提供尾插法和尾删法对数组中的数据进行增加和删除
- 可以通过下标的方式访问数组中的元素
- 可以获取数组中当前元素个数和数组的容量
1.版本1
#pragma once
#include <iostream>
#include <string>
using namespace std;
template <class T>
class MyArray
{
public:
//有参构造函数
MyArray(int capacity)
{
cout<<"有参构造函数"<<endl;
this->m_Capacity = capacity;
this->m_Size = 0;
this->m_pAddress = new T[this->m_Capacity];
}
//拷贝构造函数
MyArray(const MyArray& arr)
{
cout<<"拷贝构造函数"<<endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//深拷贝
this->m_pAddress = new T[this->m_Capacity];
//复制
for (int i = 0; i < this->m_Size;i++)
{
this->m_pAddress[i] = arr.m_pAddress[i];
}
}
//重载运算符,防止浅拷贝
MyArray& operator=(const MyArray& arr)
{
cout<<"重载运算符,防止浅拷贝"<<endl;
//先判断原来堆区是否有数据,有则释放
if(this->m_pAddress != NULL)
{
delete [] this->m_pAddress;
this->m_pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//深拷贝
this->m_pAddress = new T[this->m_Capacity];
//复制
for (int i = 0; i < this->m_Size;i++)
{
this->m_pAddress[i] = arr.m_pAddress[i];
}
//返回自身
return *this;
}
~MyArray()
{
cout<<"析构函数"<<endl;
if(this->m_pAddress != NULL)
{
delete [] this->m_pAddress;
this->m_pAddress = NULL;
}
}
private:
int m_Capacity;
int m_Size;
int *m_pAddress;
};
void test()
{
MyArray<int>p(2);
cout<<"***1***"<<endl;
MyArray<int>p1(p);
cout<<"***2***"<<endl;
MyArray<int>p2(555);
cout<<"***3***"<<endl;
p2 = p;
//输出
//有参构造函数
// ***1***
// 拷贝构造函数
// ***2***
// 有参构造函数
// ***3***
// 重载运算符,防止浅拷贝
// 析构函数
// 析构函数
// 析构函数
}
2.版本2
#pragma once
#include <iostream>
#include <string>
using namespace std;
template <class T>
class MyArray
{
public:
//有参构造函数
MyArray(int capacity)
{
cout<<"有参构造函数"<<endl;
this->m_Capacity = capacity;
this->m_Size = 0;
this->m_pAddress = new T[this->m_Capacity];
}
//拷贝构造函数
MyArray(const MyArray& arr)
{
cout<<"拷贝构造函数"<<endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//深拷贝
this->m_pAddress = new T[this->m_Capacity];
//复制
for (int i = 0; i < this->m_Size;i++)
{
this->m_pAddress[i] = arr.m_pAddress[i];
}
}
//重载运算符,防止浅拷贝
MyArray& operator=(const MyArray& arr)
{
cout<<"重载运算符,防止浅拷贝"<<endl;
//先判断原来堆区是否有数据,有则释放
if(this->m_pAddress != NULL)
{
delete [] this->m_pAddress;
this->m_pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
//深拷贝
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//深拷贝
this->m_pAddress = new T[this->m_Capacity];
//复制
for (int i = 0; i < this->m_Size;i++)
{
this->m_pAddress[i] = arr.m_pAddress[i];
}
//返回自身
return *this;
}
//尾插法
void pushVal(const T & val)
{
//判断容量是否等于大小
if(this->m_Capacity == this->m_Size)
{
return;
}
this->m_pAddress[this->m_Size] = val;//插入元素
this->m_Size += 1;//数组数量更新
}
//尾删法
void deleteVal(const T & val)
{
//判断数组是否为空
if (this->m_Size == 0)
{
return;
}
this->m_Size -= 1;
}
//访问指定索引数据
T& operator[](int index)
{
return this->m_pAddress[index];
}
//返回数组容量
inline int getCapacity()
{
return this->m_Capacity;
}
//返回数组大小
inline int getSize()
{
return this->m_Size;
}
void showInfo()
{
cout<<"show information !"<<endl;
for (int i = 0; i < this->m_Size; i++)
{
cout<<this->m_pAddress[i]<<endl;
}
}
~MyArray()
{
cout<<"析构函数"<<endl;
if(this->m_pAddress != NULL)
{
delete [] this->m_pAddress;
this->m_pAddress = NULL;
}
}
private:
int m_Capacity;
int m_Size;
int *m_pAddress;
};
void test()
{
MyArray<int>p(5);
for (int i = 0;i < p.getCapacity(); i++)
{
//尾插法
p.pushVal(i);
}
p.showInfo();
cout<<"size = "<<p.getSize()<<endl;
cout<<"capacity = "<<p.getCapacity()<<endl;
p.deleteVal(5);
p.deleteVal(4);
p.showInfo();
cout<<"size = "<<p.getSize()<<endl;
cout<<"capacity = "<<p.getCapacity()<<endl;
}