当前位置: 首页 > news >正文

C++函数模板案例--数组封装

目录

一、数组封装的需求

案例描述:

二、实操 

创建.hpp文件,编写数组类。

浅拷贝危害

拷贝构造函数

“==”重载

尾插法

尾删法

“[]"重载 

 返回数组容量、大小

完整代码

 编写.cpp文件,对自定义数组进行测试。

打印数组函数

 test01测试函数

测试自定数据类型 

       新建自定义数据类型

       打印自定义数据类型函数

test02测试函数


一、数组封装的需求


案例描述:

实现一个通用的数组类,要求如下: 

  • 可以对内置数据类型以及自定义数据类型的数据进行存储
  • 将数组中的数据存储到堆区
  • 构造函数中可以传入数组的容量
  • 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
  • 提供尾插法和尾删法对数组中的数据进行增加和册删除
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组中当前元素个数和数组的容量

二、实操 

.hpp 文件(以及头文件)在 C++ 编程中发挥着至关重要的作用,它们有助于组织代码、提高可维护性、促进代码重用和减少错误。

创建.hpp文件,编写数组类。

//自己通用的数组类
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
using namespace std;
#include<string>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);//operator= 防止浅拷贝问题 a = b = cMyArray& operator=(const MyArray& arr);//尾插法void Push_Back(const T & val);//尾删法void Pop_Back();//析构函数~MyArray(){if (this->pAddress != NULL){cout << "MyArray析构调用" << endl;delete[] this->pAddress;this->pAddress = NULL;}}private:T* pAddress;//指针指向堆区开辟的真实数组int m_Capacity;//数组容量int m_Size;//数组大小};

浅拷贝危害

       浅拷贝的危害主要源于其对资源管理的不当处理。在C++中,浅拷贝仅仅是复制对象的指针,而不是复制指针所指向的实际内容。这意味着,如果有两个对象通过浅拷贝共享同一块资源(例如动态分配的内存),当一个对象销毁时,它可能会释放这块资源。然而,此时另一个对象仍然持有指向这块已被释放资源的指针,并可能继续尝试访问或操作它。

       这种情况可能导致严重的问题,如访问违规或空指针异常。更糟糕的是,如果这块被释放的资源被其他代码重新分配并修改,那么原来的对象可能会在不知情的情况下操作错误的数据,从而导致程序崩溃或数据损坏。

       为了避免浅拷贝带来的这些问题,通常建议使用深拷贝。深拷贝会复制对象所持有的所有资源,确保每个对象都拥有自己独立的资源副本。这样,当一个对象销毁时,它只会释放自己的资源,而不会影响到其他对象。

       因此,在编写涉及资源管理的C++代码时,需要特别注意拷贝构造函数和赋值操作符的实现,确保它们正确地处理资源的复制和销毁,避免浅拷贝带来的潜在危害。

如果不提供拷贝构造(编译器默认浅拷贝)和重载等候,在后续的数组操作中,会出现浅拷贝带来的错误。 所以需要提供我们自己的拷贝构造函数同时也要重载等号,以及提供后续数组存入数据的方法。

拷贝构造函数

//拷贝构造
MyArray(const MyArray& arr)
{cout << "MyArray拷贝构造调用" << endl;this->m_Capacity = arr.m_Capacity;this->m_Size = arr.m_Size;//this->pAddress = arr.pAddress;//深拷贝this->pAddress = new T[arr.m_Capacity];//将arr中的数据都拷贝过来for (int i = 0; i < this->m_Size; i++){this->pAddress[i] = arr.pAddress[i];}
}

“==”重载

//operator= 防止浅拷贝问题 a = b = c
MyArray& operator=(const MyArray& arr)
{cout << "MyArray等号构造调用" << 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 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--;
}

“[]"重载 

//通过下标的方式访问数组中的元素 arr[0] = 100
T& operator[](int index)
{return this->pAddress[index];
}

 返回数组容量、大小

	//返回数组容量int getCapacity(){return this->m_Capacity;}//返回数组大小int getSize(){return this->m_Size;}

完整代码

//自己通用的数组类
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
using namespace std;
#include<string>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 = arr.pAddress;//深拷贝this->pAddress = new T[arr.m_Capacity];//将arr中的数据都拷贝过来for (int i = 0; i < this->m_Size; i++){this->pAddress[i] = arr.pAddress[i];}}//operator= 防止浅拷贝问题 a = b = cMyArray& operator=(const MyArray& arr){cout << "MyArray等号构造调用" << 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 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--;}//通过下标的方式访问数组中的元素 arr[0] = 100T& 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[] this->pAddress;this->pAddress = NULL;}}private:T* pAddress;//指针指向堆区开辟的真实数组int m_Capacity;//数组容量int m_Size;//数组大小};

 编写.cpp文件,对自定义数组进行测试。

打印数组函数

void printIntArray(MyArray<int>&arr)
{for (int i = 0; i < arr.getSize(); i++){cout << arr[i] << endl;}
}

 test01测试函数

void test01()
{MyArray <int>arr1(5);for (int i = 0; i < 5; i++){//利用尾插法向数组中插入数据arr1.Push_Back(i);}cout << "arr1的打印输出为:" << endl;printIntArray(arr1);cout << "arr1的容量:" << arr1.getCapacity() << endl;cout << "arr1的大小:" << arr1.getSize() << endl;MyArray<int>arr2(arr1);cout << "arr2的打印输出为:" << endl;printIntArray(arr2);arr2.Pop_Back();cout << "arr2尾删后:" << endl;cout << "arr1的容量:" << arr2.getCapacity() << endl;cout << "arr1的大小:" << arr2.getSize() << endl;/*MyArray<int>arr2(arr1);MyArray<int>arr3(100);arr3 = arr1;*/
}

测试自定数据类型 

       新建自定义数据类型

class Person
{
public:Person() {};Person(string name, int age){this->m_Name = name;this->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;}
}

test02测试函数

void test02()
{MyArray<Person>arr(10);Person p1("孙悟空", 999);Person p2("汉斯小尼姑", 12);Person p3("韩信", 22);Person p4("赵云", 20);Person p5("李欣", 30);//将数据插入到数组中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;
}

  分享完毕,关注我,带你了解更多的编程知识。

看到这里,不妨点个攒,关注一下吧!

最后,谢谢你的观看!

 

 

http://www.lryc.cn/news/337146.html

相关文章:

  • 传统文字检测方法+代码实现
  • Jmeter从数据为查找结果集数据方法随笔
  • Objective-C网络请求开发的高效实现方法与技巧
  • Java:OOP之术语或概念
  • 内存地产风云录:malloc、free、calloc、realloc演绎动态内存世界的楼盘开发与交易大戏
  • 个人博客项目笔记_05
  • 基础知识点全覆盖(1)
  • 异常处理java
  • 个人博客项目_09
  • 【2024年MathorCup数模竞赛】C题赛题与解题思路
  • 蓝桥杯省赛冲刺(3)广度优先搜索
  • 网页内容生成图片,这18般武艺你会几种呢?
  • pytest的时候输出一个F后面跟很多绿色的点解读
  • 算法打卡day33
  • 《疯狂java讲义》Java AWT图形化编程中文显示
  • Python3 标准库,API文档链接
  • 【Web】CTFSHOW-ThinkPHP5-6反序列化刷题记录(全)
  • AR智能眼镜方案_MTK平台安卓主板芯片|光学解决方案
  • Android网络抓包--Charles
  • 【LeetCode热题100】238. 除自身以外数组的乘积(数组)
  • 《哈迪斯》自带的Lua解释器是哪个版本?
  • Java内存泄漏内存溢出
  • 【springboot】项目启动时打印全部接口方法
  • 单例19c RMAN数据迁移方案
  • 05—面向对象(上)
  • 【LeetCode热题100】【链表】两数相加
  • Linux命令学习—linux 的硬件管理
  • 通讯录项目(用c语言实现)
  • 让大模型落地有“技”可循
  • java:字符集和字符流