C++中類模板的應用你瞭解多少

類模板應用

數組類的封裝

屬性:

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;
}

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容! 

推薦閱讀: