类模板案例(11)

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

1.可以对内置数据类型以及自定义数据类型的数据进行存储

2.将数组中的数据存储到堆区

3.构造函数中可以传入数组的容量

4.提供对应的拷贝构造函数以及operator=防止浅拷贝问题

5.提供尾插法和删除法对数组中的数据进行增加和删除

6.可以通过下标方式访问数组中的元素

7.可以获取数组中当前元素个数和数组的容量

  1 /*MyArray*/
  2 //自己的通用的数据类
  3 #pragma once
  4 #include <iostream>
  5 using namespace std;
  6 
  7 template<class T>
  8 class MyArray
  9 {
 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     //拷贝构造
 23     MyArray(const MyArray &arr)
 24     {
 25         cout << "MyArray拷贝构造调用!" << endl;
 26         this->m_Capacity = arr.m_Capacity;
 27         this->m_Size = arr.m_Size;
 28         //this->pAddress = arr.pAddress;//因为pAddress是指向堆区的,直接赋值操作会带来浅拷贝问题
 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     //operator=防止浅拷贝问题
 41     MyArray& operator=(const MyArray &arr)
 42     {
 43         cout << "MyArray operator= 调用!" << endl;
 44         //先判断原来堆区是否有数据,如果有先释放
 45         if (this->pAddress != NULL)
 46         {
 47             cout << "MyArray析构函数调用!" << endl;
 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 
 59         for (int i = 0; i < this->m_Size; i++)
 60         {
 61             this->pAddress[i] = arr.pAddress[i];
 62         }
 63 
 64         return *this;
 65     }
 66 
 67     //尾插法
 68     void Push_Back(const T &val)
 69     {
 70         //判断容量是否等于大小
 71         if (this->m_Capacity == this->m_Size)
 72         {
 73             cout << "容量已达上限!" << endl;
 74             return;
 75         }
 76         this->pAddress[this->m_Size] = val;//在数组的末尾插入数据
 77         this->m_Size++;//更新数组大小
 78     }
 79 
 80     //尾删法
 81     void Pop_Back(void)
 82     {
 83         //让用户访问不到最后一个元素,即为尾删,逻辑删除
 84         if (this->m_Size == 0)
 85         {
 86             cout << "数组为空!" << endl;
 87             return;
 88         }
 89         this->m_Size--;
 90     }
 91 
 92     //用户可以通过下标的方式访问数组中的元素 arr[0]
 93     T& operator[](int index)
 94     {
 95         return this->pAddress[index];
 96     }
 97 
 98     //返回数组的容量
 99     int getCapacity(void)
100     {
101         return this->m_Capacity;
102     }
103 
104     //返回数组的大小
105     int getSize(void)
106     {
107         return this->m_Size;
108     }
109 
110     //析构函数
111     ~MyArray()
112     {
113         if (this->pAddress != NULL)
114         {
115             cout << "MyArray析构函数调用!" << endl;
116             delete[] this->pAddress;
117             this->pAddress = NULL;
118         }
119     }
120 
121 
122 private:
123 
124     T *pAddress;
125 
126     int m_Capacity;
127 
128     int m_Size;
129 };
  1 /*main.cpp*/
  2 #include <iostream>
  3 #include <string>
  4 #include "myArray.hpp"
  5 using namespace std;
  6 
  7 void printIntArray(MyArray<int> &arr)
  8 {
  9     for (int i = 0; i < arr.getSize(); i++)
 10     {
 11         cout << arr[i] << endl;
 12     }
 13 }
 14 
 15 void test_01(void)
 16 {
 17 /*************************************************************/
 18     MyArray<int> arr1(5);
 19 
 20     for (int i = 0; i < 5;i++)
 21     {
 22         //利用尾插法向数组中插入数据
 23         arr1.Push_Back(i);
 24     }
 25 
 26     cout << "arr1的打印输出为:" << endl;
 27     
 28     printIntArray(arr1);
 29 
 30     cout << "arr1的容量为:" << arr1.getCapacity() << endl;
 31     cout << "arr1的大小为:" << arr1.getSize() << endl;
 32 
 33 /*************************************************************/
 34     MyArray<int> arr2(arr1);
 35     
 36     cout << "arr2的打印输出为:" << endl;
 37 
 38     printIntArray(arr2);
 39 
 40     //尾删
 41     arr2.Pop_Back();
 42     cout << "arr2的容量为:" << arr2.getCapacity() << endl;
 43     cout << "arr2的大小为:" << arr2.getSize() << endl;
 44 
 45     //MyArray<int> arr2(arr1);
 46     //MyArray<int> arr3(100);
 47     //arr3 = arr1;
 48 }
 49 
 50 //测试自定义数据类型
 51 class Person
 52 {
 53 public:
 54     Person(){}
 55     Person(string name, int age)
 56     {
 57         this->m_Name = name;
 58         this->m_Age = age;
 59     }
 60 
 61     string m_Name;
 62     int m_Age;
 63 };
 64 
 65 void printPersonArray(MyArray<Person> &arr)
 66 {
 67     for (int i = 0; i < arr.getSize(); i++)
 68     {
 69         cout << "姓名:" << arr[i].m_Name << "年龄:" << arr[i].m_Age << endl;
 70     }
 71 }
 72 
 73 void test_02(void)
 74 {
 75     MyArray<Person> arr(10);
 76 
 77     Person p1("孙悟空 ", 20);
 78     Person p2("韩信 ",21);
 79     Person p3("赵云 ",22);
 80     Person p4("安琪拉 ",23);
 81     Person p5("妲己 ",24);
 82 
 83     //将数据插入到数组中
 84     arr.Push_Back(p1);
 85     arr.Push_Back(p2);
 86     arr.Push_Back(p3);
 87     arr.Push_Back(p4);
 88     arr.Push_Back(p5);
 89 
 90     //打印数组
 91     printPersonArray(arr);
 92 
 93     //输出容量
 94     cout << "arr的容量为:" << arr.getCapacity() << endl;
 95     
 96     //输出大小
 97     cout << "arr的大小为:" << arr.getSize() << endl;
 98 }
 99 
100 int main(void)
101 {
102     //test_01();
103     test_02();
104 
105     system("pause");
106     return 0;
107 }

 

posted @ 2020-04-28 16:26  坦率  阅读(194)  评论(0编辑  收藏  举报