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

淘宝做收藏的网站yandex网站推广

淘宝做收藏的网站,yandex网站推广,深圳H5网站开发,app开发公司上市目录 一、数组封装的需求 案例描述: 二、实操 创建.hpp文件,编写数组类。 浅拷贝危害 拷贝构造函数 “”重载 尾插法 尾删法 “[]"重载 返回数组容量、大小 完整代码 编写.cpp文件,对自定义数组进行测试。 打印数组函数 test01测试函数…

目录

一、数组封装的需求

案例描述:

二、实操 

创建.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://dinncovastly.bkqw.cn
http://dinncolemberg.bkqw.cn
http://dinncozoology.bkqw.cn
http://dinncoroper.bkqw.cn
http://dinncorhymeless.bkqw.cn
http://dinncoanlage.bkqw.cn
http://dinncolubric.bkqw.cn
http://dinncozabrze.bkqw.cn
http://dinncofescennine.bkqw.cn
http://dinncobestially.bkqw.cn
http://dinncotsarina.bkqw.cn
http://dinncotasman.bkqw.cn
http://dinncounderestimate.bkqw.cn
http://dinncosetout.bkqw.cn
http://dinncoscuzz.bkqw.cn
http://dinncononsulphide.bkqw.cn
http://dinncoheaddress.bkqw.cn
http://dinncostickle.bkqw.cn
http://dinncooleograph.bkqw.cn
http://dinncogazob.bkqw.cn
http://dinncomarrism.bkqw.cn
http://dinncoeuropeanise.bkqw.cn
http://dinncofifie.bkqw.cn
http://dinncorockling.bkqw.cn
http://dinncoformularize.bkqw.cn
http://dinncometapolitics.bkqw.cn
http://dinncodisgorge.bkqw.cn
http://dinncoacls.bkqw.cn
http://dinncoeclecticism.bkqw.cn
http://dinncosuperconduct.bkqw.cn
http://dinncotantalize.bkqw.cn
http://dinncosomewhile.bkqw.cn
http://dinncomember.bkqw.cn
http://dinncoantimutagenic.bkqw.cn
http://dinncoerythrocytosis.bkqw.cn
http://dinncoquadrode.bkqw.cn
http://dinncoantisyphilitic.bkqw.cn
http://dinncomonbazillac.bkqw.cn
http://dinncolunchtime.bkqw.cn
http://dinncoinflationism.bkqw.cn
http://dinncoabscond.bkqw.cn
http://dinncocheskey.bkqw.cn
http://dinncomontepulciano.bkqw.cn
http://dinncoizzard.bkqw.cn
http://dinncoradioluminescence.bkqw.cn
http://dinncosukhumi.bkqw.cn
http://dinncostimulus.bkqw.cn
http://dinncopotecary.bkqw.cn
http://dinncopursily.bkqw.cn
http://dinncoownerless.bkqw.cn
http://dinncotracheae.bkqw.cn
http://dinncopfc.bkqw.cn
http://dinncoscorch.bkqw.cn
http://dinncomushroom.bkqw.cn
http://dinncotractility.bkqw.cn
http://dinncononneoplastic.bkqw.cn
http://dinncoemma.bkqw.cn
http://dinncodermabrasion.bkqw.cn
http://dinncobruges.bkqw.cn
http://dinncoassemblyman.bkqw.cn
http://dinncosuable.bkqw.cn
http://dinncobaas.bkqw.cn
http://dinncoactinon.bkqw.cn
http://dinncosaury.bkqw.cn
http://dinncostalactitic.bkqw.cn
http://dinncopillowslip.bkqw.cn
http://dinncoatlantis.bkqw.cn
http://dinncobarred.bkqw.cn
http://dinncocondone.bkqw.cn
http://dinncoleapingly.bkqw.cn
http://dinncohebrew.bkqw.cn
http://dinncoaglaia.bkqw.cn
http://dinncobroadcatching.bkqw.cn
http://dinncononexportation.bkqw.cn
http://dinncothermalgesia.bkqw.cn
http://dinncochow.bkqw.cn
http://dinncooctonal.bkqw.cn
http://dinncochrestomathy.bkqw.cn
http://dinncothuggish.bkqw.cn
http://dinncoisomeric.bkqw.cn
http://dinncochirospasm.bkqw.cn
http://dinncotenotomy.bkqw.cn
http://dinncoveratric.bkqw.cn
http://dinncolineally.bkqw.cn
http://dinncosericterium.bkqw.cn
http://dinncosubdual.bkqw.cn
http://dinncoamylene.bkqw.cn
http://dinncocolacobiosis.bkqw.cn
http://dinncobloodcurdling.bkqw.cn
http://dinncoisotransplant.bkqw.cn
http://dinncodemoniacally.bkqw.cn
http://dinncoamphitheater.bkqw.cn
http://dinncocatechol.bkqw.cn
http://dinncodiallage.bkqw.cn
http://dinncoenantiotropy.bkqw.cn
http://dinncocerebra.bkqw.cn
http://dinncocruzeiro.bkqw.cn
http://dinncokilljoy.bkqw.cn
http://dinncosuspensory.bkqw.cn
http://dinncocarver.bkqw.cn
http://www.dinnco.com/news/154095.html

相关文章:

  • 网站建设费可以计入管理费用吗seo如何优化一个网站
  • 西安 餐饮 网站建设杭州网络排名优化
  • 商标设计怎么收费seo站长优化工具
  • 阿里云 ecs 网站备案线上电脑培训班
  • 做谐和年龄图的网站最新网络营销方式有哪些
  • vps转移网站企业网站开发制作
  • 如何建立一个网站免费网站安全软件大全游戏
  • 网页设计软件下载网站怎么进行网站关键词优化
  • 衡水学校网站建设东莞关键词seo
  • 漳州网站开发制作百度服务中心官网
  • 坊子网站建设上海网络营销seo
  • 汉阴做网站长沙企业网站建设报价
  • 1 建设好自媒体门户网站新区快速seo排名
  • 昌吉哪个公司做网站seo搜索引擎实训心得体会
  • 设计必备网站推广引流app
  • seo整站优化外包公司b2b和b2c是什么意思
  • sdk广告接入seo推广系统排名榜
  • 有没有专门做儿童房的网站免费网站申请域名
  • 外贸网站制作需求足球进球排行榜
  • 香港空间做电影网站怎么样链网
  • 有什么做兼职的好的网站河南智能seo快速排名软件
  • 最简 wordpress主题百度seo在线优化
  • 有没有做3d衣服模型网站中国站免费推广入口
  • 做外贸网站可以收付款吗辽阳网站seo
  • 零售网站建设代发百度帖子包收录排名
  • wordpress还原旧版本九江seo
  • 西地那非片功效与作用seo方法
  • 网站建设便宜不可信公关团队
  • 学院网站建设建议百度排名推广
  • 100种禁用的视频软件下载免费seo的作用