C/C++教程

C++ //模板案例-数组封装类

本文主要是介绍C++ //模板案例-数组封装类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  1 //模板案例-数组封装类
  2 //描述
  3 //实现一个通用的数组类
  4 //1.可以对内置数据类型以及自定义数据类型的数据进行储存
  5 //2.将数组很脏的数据储存到堆区
  6 //3.构造函数中可以传入数组的容量
  7 //4.提供对应的拷贝构造函数以及operator=防止浅拷贝问题
  8 //5,提供尾插法和尾删法对数组中的数据进行增加和删除
  9 //6.可以通过下标的方式访问数组的元素
 10 //7.可以获取数组在当前元素个数和数组的容量
 11 
 12 #include <iostream>
 13 #include <string>
 14 #include<fstream>
 15 #include"MyArray.hpp"
 16 using namespace std;
 17 
 18 void printfIntArray(MyArray <int>&arr)
 19 {
 20     for (int i = 0; i < arr.getSize(); i++)
 21     {
 22         cout << arr[i] << endl;
 23     }
 24 }
 25 
 26 
 27 void test01()
 28 {
 29     MyArray <int>arr1(5);
 30 
 31     for (int i = 0; i < 5; i++)
 32     {
 33         //利用尾插法向数组中插入数据
 34         arr1.Push_Back(i);
 35     }
 36     cout << "arry1的打印输出为: " << endl;
 37     printfIntArray(arr1);
 38 
 39     cout << "arr1的容量为: " << arr1.getCapacity() << endl;
 40     cout << "arr1的大小为: " << arr1.getSize() << endl;
 41 
 42     
 43     cout << "arry2的打印输出为: " << endl;
 44     MyArray<int>arr2(arr1);
 45     printfIntArray(arr2);
 46 
 47     //尾删
 48     arr2.Pop_Back();
 49     cout << "arr2尾删后: " << endl;
 50 
 51     cout << "arr2的容量为: " << arr2.getCapacity() << endl;
 52     cout << "arr2的大小为: " << arr2.getSize() << endl;
 53     //MyArray<int>arr3(100);
 54     //arr3 = arr1;
 55 }
 56 
 57 //测试自定义数据类型
 58 class Person
 59 {
 60 public:
 61 
 62     Person()
 63     {
 64 
 65     }
 66     Person(string name, int age)
 67     {
 68         this->m_Name = name;
 69         this->m_Age = age;
 70     }
 71 
 72 
 73     string m_Name;
 74     int m_Age;
 75 };
 76 void printPersonArray(MyArray<Person>& arr)
 77 {
 78     for (int i = 0; i < arr.getSize(); i++)
 79     {
 80         cout << "姓名: " << arr[i].m_Name << " 年龄:" << arr[i].m_Age << endl;
 81 
 82 
 83     }
 84 }
 85 void test02()
 86 {
 87     MyArray<Person>arr(10);
 88 
 89     Person p1("张三", 200);
 90     Person p2("李四",20);
 91     Person p3("王五", 30);
 92     Person p4("赵六",99);
 93     Person p5("孙七", 48);
 94 
 95     //将数据插入到数组中
 96     arr.Push_Back(p1);
 97     arr.Push_Back(p2);
 98     arr.Push_Back(p3);
 99     arr.Push_Back(p4);
100     arr.Push_Back(p5);
101 
102     //打印数组
103     printPersonArray(arr);
104 
105     //打印容量
106     cout << "arr容量为:" << arr.getCapacity() << endl;
107 
108     //打印大小
109     cout << "arr容量为:" << arr.getSize() << endl;
110 
111 }
112 int main()
113 {
114 
115     test01();
116     test02();
117 
118     system("pause");
119 
120     return 0;
121 
122 }
  1 MyArray.hpp
  2 
  3 #pragma once
  4 //自己的通用数组类
  5 #include<iostream>
  6 using namespace std;
  7 
  8 template<class T>
  9 class MyArray
 10 {
 11 public:
 12 
 13     //有参构造  参数 容量
 14     MyArray(int capacity)
 15     {
 16         //cout << "Myarray有参构造调用"<<endl;
 17         this->m_Capacity = capacity;
 18         this->m_Size = 0;
 19         this->pAddress = new T[this->m_Capacity];
 20     }
 21     //拷贝构造
 22     MyArray(const MyArray& arr)
 23     {
 24         //cout << "Myarray有拷贝造调用" << endl;
 25         this->m_Capacity = arr.m_Capacity;
 26         this->m_Size = arr.m_Size;
 27         //this->pAddress = arr.pAddress;
 28 
 29 
 30         //深拷贝
 31         this->pAddress  = new T[arr.m_Capacity];
 32 
 33         //将arr中的数据都拷贝过来
 34         for (int i = 0; i < this->m_Size; i++)
 35         {
 36             this->pAddress[i] = arr.pAddress[i];
 37         }
 38 
 39 
 40     }
 41     //operator =防止浅拷贝的问题 a = b =c
 42     MyArray& operator = (const MyArray& arr)
 43     {
 44         //cout << "Myarray的 operator= 调用" << endl;
 45         //先判断原来堆区是否有数据 如果有先释放
 46         if (this->pAddress != NULL)
 47         {
 48             delete[]this->pAddress;
 49             this->pAddress = NULL;
 50             this->m_Capacity = 0;
 51             this->m_Size = 0;
 52         }
 53 
 54         //深拷贝
 55         this->m_Capacity = arr.m_Capacity;
 56         this->m_Size = arr.m_Size;
 57         this->pAddress = new T[arr.m_Capacity];
 58         for (int i = 0; i < this->m_Size; i++)
 59         {
 60             this->pAddress[i] = arr.pAddress[i];
 61         }
 62         return *this;
 63     }
 64 
 65     //尾插法
 66     void Push_Back(const T &val)
 67     {
 68         //判断容量是否等于大小
 69         if (this->m_Capacity == this->m_Size)
 70         {
 71             return;
 72         }
 73         //在数组末尾插入数据
 74         this->pAddress[this->m_Size] = val;
 75         this->m_Size++;  //更新数组大小
 76     }
 77     //尾删法
 78     void Pop_Back()
 79     {
 80         //让用户访问不到最后一个元素,即为尾删,逻辑删除
 81         if (this->m_Size == 0)
 82         {
 83             return;
 84         }
 85         this->m_Size--;
 86     }
 87 
 88     //通过下标的方式访问数组中的元素
 89     T& operator[](int index)
 90     {
 91         return this->pAddress[index];
 92     }
 93 
 94     //返回数组的容量
 95     int getCapacity()
 96     {
 97         return this->m_Capacity;
 98     }
 99     //返回数组大小
100     int getSize()
101     {
102         return this->m_Size;
103     }
104 
105 
106 
107 
108 
109     //析构函数
110     ~MyArray()
111     {
112         if (this->pAddress != NULL)
113         {
114             //cout << "Myarray析构函数调用" << endl;
115             delete[] this->pAddress;
116             this->pAddress = NULL;
117         }
118     }
119 private:
120 
121     T* pAddress;  //指针指向堆区开辟的真时数组
122 
123     int m_Capacity; //数组容量
124 
125     int m_Size;     //数组大小
126 };

 

这篇关于C++ //模板案例-数组封装类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!