C++中类模板的应用你了解多少
目录
- 类模板应用
- 数组类的封装
- Int的.hpp文件
- int的测试文件
- Person类的.hpp文件
- Person类的测试文件
- 总结
类模板应用
数组类的封装
属性:
1,T *pAddress 指向堆区数组的指针。 2,int m_Capacity 数组容量 3,int m_Size 数组大小
行为:
1,myArray(int capacity) 构造函数 2,myArray(const MyArray&arr) 拷贝构造函数 3,operator= 重载赋值操作符= 4,operator[] 重载中括号[] 5,~myArray() 析构函数 6,getCapacity 获取容量 7,getSize 获取大小 8,pushback 尾插
将头文件与实现文件写到一起,后缀是.hpp
Int的.hpp文件
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; #include<string> template<class T> class MyArray { public: MyArray() {};//默认构造 MyArray(int capacity)//有参构造 { this->m_Capacity = capacity; this->m_Size = 0; this->pAddress = new T[this->m_Capacity]; } MyArray(const MyArray& arr)//拷贝构造 { this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; this->pAddress = new T[this->m_Capacity];//这个不能直接拷贝,需要自己重新创建 for (int i = 0; i < arr.m_Size; i++)//然后将数组的元素一个个的赋值过来 { this->pAddress[i] = arr.pAddress[i]; } } MyArray& operator=(const MyArray &arr)//重载赋值操作符=(返回自身的引用) { if (this->pAddress)//如果原先有数据了,那么就删除 { delete[] this->pAddress; this->pAddress = NULL; } //然后进行深拷贝 this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; this->pAddress = new T[this->m_Capacity];//这个不能直接拷贝,需要自己重新创建 for (int i = 0; i < arr.m_Size; i++)//然后将数组的元素一个个的赋值过来 { this->pAddress[i] = arr.pAddress[i]; } return *this; } T& operator[](int dex)//重载[] 为了访问数组中的值, { return this->pAddress[dex]; } void pushBack(const T& val)//尾插 { if (this->m_Capacity <= this->m_Size)//如果已经超过范围了 { return; } this->pAddress[this->m_Size] = val; this->m_Size++; } int getCapacity()//获取数组容量 { return this->m_Capacity; } int getSize()//获取数组大小 { return this->m_Size; } ~MyArray()//析构 { if (this->pAddress) { delete[] this->pAddress; this->pAddress = NULL; } } private: T* pAddress;//指向堆区真实数组指针 int m_Capacity;//数组容量 int m_Size; };
int的测试文件
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; #include<string> #include"myArray.hpp" void myPrint(MyArray<int> &myIntArray) { for (int i = 0; i < myIntArray.getSize(); i++) { cout << myIntArray[i] << endl; } } int main() { MyArray<int> myIntArray(100); for (int i = 0; i < 10; i++) { myIntArray.pushBack(i + 100); } myPrint(myIntArray); return 0; }
输出结果:
100
101
102
103
104
105
106
107
108
109
以上代码证明写的数组类的封装对内置数据类型是适用的,接下来试试自定义类型Person
ps:如果识别出来了是要开辟Person类的数组的空间,需要调用Person的默认构造(有参构造不行),所以必须在Person类中加一个默认构造。
Person类的.hpp文件
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; #include<string> template<class T> class MyArray { public: MyArray() {};//默认构造 MyArray(int capacity)//有参构造 { this->m_Capacity = capacity; this->m_Size = 0; this->pAddress = new T[this->m_Capacity]; } MyArray(const MyArray& arr)//拷贝构造 { this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; this->pAddress = new T[this->m_Capacity];//这个不能直接拷贝,需要自己重新创建 for (int i = 0; i < arr.m_Size; i++)//然后将数组的元素一个个的赋值过来 { this->pAddress[i] = arr.pAddress[i]; } } MyArray& operator=(const MyArray &arr)//重载赋值操作(返回自身的引用) { if (this->pAddress)//如果原先有数据了,那么就删除 { delete[] this->pAddress; this->pAddress = NULL; } //然后进行深拷贝 this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; this->pAddress = new T[this->m_Capacity];//这个不能直接拷贝,需要自己重新创建 for (int i = 0; i < arr.m_Size; i++)//然后将数组的元素一个个的赋值过来 { this->pAddress[i] = arr.pAddress[i]; } return *this; } T& operator[](int dex)//重载[] 为了访问数组中的值, { return this->pAddress[dex]; } void pushBack(const T& val)//尾插 { if (this->m_Capacity <= this->m_Size)//如果已经超过范围了 { return; } this->pAddress[this->m_Size] = val; this->m_Size++; } int getCapacity()//获取数组容量 { return this->m_Capacity; } int getSize()//获取数组大小 { return this->m_Size; } ~MyArray()//析构 { if (this->pAddress) { delete[] this->pAddress; this->pAddress = NULL; } } private: T* pAddress;//指向堆区真实数组指针 int m_Capacity;//数组容量 int m_Size; };
Person类的测试文件
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; #include<string> #include"myArray.hpp" class Person { public: Person() {}; string m_name; int m_age; Person(string name, int age) { this->m_age = age; this->m_name = name; } }; void myPrintInt(MyArray<int> &myIntArray)//int的 { for (int i = 0; i < myIntArray.getSize(); i++) { cout << myIntArray[i] << endl; } } void myPrintPerson(MyArray<Person>& myPersonArray)//Person的 { for (int i = 0; i < myPersonArray.getSize(); i++) { cout << myPersonArray[i].m_name << " " << myPersonArray[i].m_age << endl; } } int main() { /*MyArray<int> myIntArray(100); for (int i = 0; i < 10; i++) { myIntArray.pushBack(i + 100); } myPrintInt(myIntArray);*/ MyArray<Person>myPersonArray(100); Person p1("小明", 18); Person p2("小宏", 18); Person p3("小量", 19); Person p4("小应", 18); myPersonArray.pushBack(p1); myPersonArray.pushBack(p2); myPersonArray.pushBack(p3); myPersonArray.pushBack(p4); myPrintPerson(myPersonArray); cout << "数组容量:"<<myPersonArray.getCapacity()<< endl;//100 cout << "数组大小:" << myPersonArray.getSize() << endl;//4 return 0; }
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我们的更多内容!
赞 (0)