首頁 > 軟體

C++中類別範本的應用你瞭解多少

2022-02-21 16:00:03

類別範本應用

陣列類的封裝

屬性:

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

總結

本篇文章就到這裡了,希望能夠給你帶來幫助,也希望您能夠多多關注it145.com的更多內容! 


IT145.com E-mail:sddin#qq.com