当前位置:网站首页>Class template case - array class encapsulation
Class template case - array class encapsulation
2022-06-29 14:37:00 【codexixi】
Learning goals :
1. The header file and source file will be merged into a contract file .hpp file
2. Learn how to use class templates
3. Review operator overloading
Project requirements :

Case layout :

Implementation code :
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 The parameterized constructor of " << endl;
}
MyArray(const MyArray& arr)
{
cout << "MyArray Copy constructor call for " << 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 Of operator= Call to " << 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 Destructor call for " << endl;
delete[] this->pAddress;
this->pAddress = NULL;
}
}
private:
T* pAddress; // Pointer to the real array opened by the heap
int m_Capacity;
int m_Size;
};
Class template case .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 << " Current array capacity :" << arrl.getCapacity() << endl;
cout << " Current array size :" << arrl.getSize() << endl;
for (int i = 0; i < 3; i++)
{
arrl.Last_Delete();
}
cout << " Current array capacity :" << arrl.getCapacity() << endl;
cout << " Current array size :" << arrl.getSize() << endl;
}
int main()
{
//test01();
test02();
return 0;
}
Running results :

边栏推荐
- numpy数组创建
- 分布式唯一 ID 生成方案浅谈
- 《canvas》之第10章 canvas路径
- Turbulent intermediary business, restless renters
- Stable currency risk profile: are usdt and usdc safe?
- Methods of accessing external services in istio grid
- Redis的五种数据结构的底层实现原理
- Wechat applet: Yunkai publishes white wall wechat applet source code download server free and domain name support traffic main revenue
- Wechat applet: install B artifact and P diagram, modify wechat traffic main applet source code, Download funny joke diagram, make server free domain name
- 《canvas》之第8章 像素操作
猜你喜欢

go-zero微服务实战系列(七、请求量这么高该如何优化)

驱动器实际运用案例

《canvas》之第12章 其他应用
![[blackduck] configure the specified Synopsys detect scan version under Jenkins](/img/85/73988e6465e8c25d6ab8547040a8fb.png)
[blackduck] configure the specified Synopsys detect scan version under Jenkins

Investors fell off the altar: 0 sales in half a year, transferred to business and delivered takeout

VQA needs not only pictures, but also external knowledge! University of Washington & Microsoft proposed revive, using gpt-3 and wikidata to help answer questions

疯狂的数字藏品,下一个造富神话?

动荡的中介生意,不安的租房人

织梦dedecms资源素材教程下载网站模板源码(带手机移动端)附安装教程

第五届中国软件开源创新大赛 | openGauss赛道直播培训
随机推荐
Redis的五种数据结构的底层实现原理
Kubernetes Pod 排错指南
Stable currency risk profile: are usdt and usdc safe?
Tiktok's global short video dominance may be reversed by YouTube
QRCode自定义二维码中间图片
stm32 mbed 入门教程(四)---PWM
《canvas》之第11章 canvas状态
人不成熟的特征
Redis fragment cluster setup and use tutorial
zabbix 5.0如何将esxi6.7添加到监控
Industry analysis - quick intercom, building intercom
Navicat连接MySQL8.0的正确方法(亲测有效)
《canvas》之第8章 像素操作
Wechat applet: repair collection interface version cloud development expression package
第五届中国软件开源创新大赛 | openGauss赛道直播培训
Investors fell off the altar: 0 sales in half a year, transferred to business and delivered takeout
Characteristics of human immaturity
揭秘!付费会员制下的那些小心机!
每周 Postgres 世界动态 2022w25
国内十大券商,有哪些安全吗?