当前位置:网站首页>类模板案例-【数组类封装】
类模板案例-【数组类封装】
2022-06-29 13:56:00 【codexixi】
学习目标:
1.会将头文件和源文件合并成约定俗成的.hpp文件
2.学会类模板的使用
3.复习运算符重载的知识
项目需求:

案例布局:

实现代码:
MyArray.hpp
#pragma once
#include <iostream>
using namespace std;
template <class T>
class MyArray
{
public:
MyArray(int capacity)
{
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[capacity];
cout << "MyArray的有参构造函数调用" << endl;
}
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];
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
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;
}
void Last_Insert(T& p)
{
if (this->m_Size == this->m_Capacity)
{
return;
}
this->pAddress[this->m_Size] = p;
this->m_Size++;
}
void Last_Delete()
{
if (this->m_Size == 0)
{
return;
}
this->m_Size--;
}
T& operator[](int index)
{
return this->pAddress[index];
}
int getSize()
{
return this->m_Size;
}
int getCapacity()
{
return this->m_Capacity;
}
~MyArray()
{
if (this->pAddress != NULL)
{
cout << "MyArray的析构函数调用" << endl;
delete[] this->pAddress;
this->pAddress = NULL;
}
}
private:
T* pAddress; //指针指向堆区开辟的真实数组
int m_Capacity;
int m_Size;
};
类模板案例.cpp
#include "MyArray.hpp"
#include <string>
void test01()
{
MyArray <int>arr1(5);
MyArray <int>arr2(arr1);
MyArray <int>arr3(100);
arr3 = arr1;
}
class Person
{
public:
Person() {
};
Person(int age, string name)
{
m_Age = age;
m_Name = name;
}
int m_Age;
string m_Name;
};
void test02()
{
MyArray <int>arrl(5);
for (int i = 0; i < 5; i++)
{
arrl.Last_Insert(i);
}
for (int i = 0; i < arrl.getSize(); i++)
{
cout << arrl[i] << endl;
}
cout << "当前数组容量:" << arrl.getCapacity() << endl;
cout << "当前数组大小:" << arrl.getSize() << endl;
for (int i = 0; i < 3; i++)
{
arrl.Last_Delete();
}
cout << "当前数组容量:" << arrl.getCapacity() << endl;
cout << "当前数组大小:" << arrl.getSize() << endl;
}
int main()
{
//test01();
test02();
return 0;
}
运行结果:

边栏推荐
- Kubernetes Pod 排错指南
- JUC multithreading: creation and working principle of thread pool
- 关于MongoDB报错:connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
- leetcode:226. Flip binary tree
- 超 Nice 的表格响应式布局小技巧
- 微信小程序:全新独家云开发微群人脉
- golang代码规范整理
- [use of veux developer tools - use of getters]
- 【jenkins】pipeline控制多job顺序执行,进行定时持续集成
- go-zero微服务实战系列(七、请求量这么高该如何优化)
猜你喜欢

浅析 Istio——可观测性

微信小程序:装B神器P图修改微信流量主小程序源码下载趣味恶搞图制作免服务器域名

win10安装Monggodb的基本使用教程

深度学习的坎坷六十年
![[Jenkins] pipeline controls the sequential execution of multiple jobs for timed continuous integration](/img/04/a650ab76397388bfb62d0dd190dbd0.png)
[Jenkins] pipeline controls the sequential execution of multiple jobs for timed continuous integration

Are you still reading the log by command? Use kibana, one picture is better than ten thousand lines of log

Tiktok's global short video dominance may be reversed by YouTube

Stable currency risk profile: are usdt and usdc safe?
redis 分片集群搭建与使用教程

电脑时间校对后不自动更新要如何解决
随机推荐
微信小程序:云开发表白墙微信小程序源码下载免服务器和域名支持流量主收益
Nuscenes configuration information about radar
Unity SplashImage 缩放问题
Redis' cache avalanche, cache breakdown, cache penetration, cache preheating, and cache degradation
Thinkpad VMware 安装虚拟机出现此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态(问题解决方法)
Sixty years of deep learning
瑞达期货可以开户吗?安全可靠吗?
【 网络带宽 】MBps & Mbps
"Dead" Nokia makes 150billion a year
HTAP X 云原生: TiDB 加速释放数据价值,实现数据敏捷
VeeamBackup&Replication简介
【VEUX开发者工具的使用-getters使用】
用手机在指南针上开户靠谱吗?这样炒股有没有什么安全隐患
MySQL数据库:使用show profile命令分析性能
mysql多表查询
By proxy, by buyout, the wild era of domestic end-to-end travel is waiting for the next "eternal robbery"
TikTok全球短视频霸主地位或被YouTube反超
Interview high concurrent, cool!! (high energy in the whole process, collection recommended)
stm32 mbed 入门教程(四)---PWM
Industry analysis - quick intercom, building intercom