欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中類模板的應(yīng)用你了解多少

 更新時(shí)間:2022年02月21日 15:23:27   作者:是小明同學(xué)啊  
這篇文章主要為大家詳細(xì)介紹了C++中類模板的應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

類模板應(yīng)用

數(shù)組類的封裝

屬性:

1,T *pAddress 指向堆區(qū)數(shù)組的指針。
2,int m_Capacity 數(shù)組容量
3,int m_Size 數(shù)組大小

行為:

1,myArray(int capacity) 構(gòu)造函數(shù)
2,myArray(const MyArray&arr) 拷貝構(gòu)造函數(shù)
3,operator= 重載賦值操作符=
4,operator[] 重載中括號(hào)[]
5,~myArray()  析構(gòu)函數(shù)
6,getCapacity 獲取容量
7,getSize     獲取大小
8,pushback   尾插

將頭文件與實(shí)現(xiàn)文件寫到一起,后綴是.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() {};//默認(rèn)構(gòu)造
	MyArray(int capacity)//有參構(gòu)造
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
	}
	MyArray(const MyArray& arr)//拷貝構(gòu)造
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[this->m_Capacity];//這個(gè)不能直接拷貝,需要自己重新創(chuàng)建
		for (int i = 0; i < arr.m_Size; i++)//然后將數(shù)組的元素一個(gè)個(gè)的賦值過來
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}
	MyArray& operator=(const MyArray &arr)//重載賦值操作符=(返回自身的引用)
	{
		if (this->pAddress)//如果原先有數(shù)據(jù)了,那么就刪除
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
		//然后進(jìn)行深拷貝
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[this->m_Capacity];//這個(gè)不能直接拷貝,需要自己重新創(chuàng)建
		for (int i = 0; i < arr.m_Size; i++)//然后將數(shù)組的元素一個(gè)個(gè)的賦值過來
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}
	T& operator[](int dex)//重載[] 為了訪問數(shù)組中的值,
	{
		return this->pAddress[dex];
	}

	void pushBack(const T& val)//尾插
	{
		if (this->m_Capacity <= this->m_Size)//如果已經(jīng)超過范圍了
		{
			return;
		}
		this->pAddress[this->m_Size] = val;
		this->m_Size++;
	}
	int getCapacity()//獲取數(shù)組容量
	{
		return this->m_Capacity;
	}
	int getSize()//獲取數(shù)組大小
	{
		return this->m_Size;
	}
	~MyArray()//析構(gòu)
	{
		if (this->pAddress)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
private:	 
	T* pAddress;//指向堆區(qū)真實(shí)數(shù)組指針
	int m_Capacity;//數(shù)組容量
	int m_Size;
};

int的測(cè)試文件

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

輸出結(jié)果:

100
101
102
103
104
105
106
107
108
109

以上代碼證明寫的數(shù)組類的封裝對(duì)內(nèi)置數(shù)據(jù)類型是適用的,接下來試試自定義類型Person

ps:如果識(shí)別出來了是要開辟Person類的數(shù)組的空間,需要調(diào)用Person的默認(rèn)構(gòu)造(有參構(gòu)造不行),所以必須在Person類中加一個(gè)默認(rèn)構(gòu)造。

Person類的.hpp文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<string>

template<class T>
class MyArray
{
public:
	MyArray() {};//默認(rèn)構(gòu)造
	MyArray(int capacity)//有參構(gòu)造
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
	}
	MyArray(const MyArray& arr)//拷貝構(gòu)造
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[this->m_Capacity];//這個(gè)不能直接拷貝,需要自己重新創(chuàng)建
		for (int i = 0; i < arr.m_Size; i++)//然后將數(shù)組的元素一個(gè)個(gè)的賦值過來
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}
	MyArray& operator=(const MyArray &arr)//重載賦值操作(返回自身的引用)
	{
		if (this->pAddress)//如果原先有數(shù)據(jù)了,那么就刪除
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
		//然后進(jìn)行深拷貝
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[this->m_Capacity];//這個(gè)不能直接拷貝,需要自己重新創(chuàng)建
		for (int i = 0; i < arr.m_Size; i++)//然后將數(shù)組的元素一個(gè)個(gè)的賦值過來
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}
	T& operator[](int dex)//重載[] 為了訪問數(shù)組中的值,
	{
		return this->pAddress[dex];
	}

	void pushBack(const T& val)//尾插
	{
		if (this->m_Capacity <= this->m_Size)//如果已經(jīng)超過范圍了
		{
			return;
		}
		this->pAddress[this->m_Size] = val;
		this->m_Size++;
	}
	int getCapacity()//獲取數(shù)組容量
	{
		return this->m_Capacity;
	}
	int getSize()//獲取數(shù)組大小
	{
		return this->m_Size;
	}
	~MyArray()//析構(gòu)
	{
		if (this->pAddress)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}
private:	 
	T* pAddress;//指向堆區(qū)真實(shí)數(shù)組指針
	int m_Capacity;//數(shù)組容量
	int m_Size;
};

Person類的測(cè)試文件

#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("小應(yīng)", 18);
	myPersonArray.pushBack(p1);
	myPersonArray.pushBack(p2);
	myPersonArray.pushBack(p3);
	myPersonArray.pushBack(p4);
	myPrintPerson(myPersonArray);
	cout << "數(shù)組容量:"<<myPersonArray.getCapacity()<< endl;//100
	cout << "數(shù)組大小:" << myPersonArray.getSize() << endl;//4
	return 0;
}

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容! 

相關(guān)文章

  • 從匯編看c++函數(shù)的默認(rèn)參數(shù)的使用說明

    從匯編看c++函數(shù)的默認(rèn)參數(shù)的使用說明

    本篇文章介紹了,在c++中函數(shù)的默認(rèn)參數(shù)的使用說明分析。需要的朋友參考下
    2013-05-05
  • c++讀取和寫入TXT文件的整理方法

    c++讀取和寫入TXT文件的整理方法

    今天小編就為大家分享一篇c++讀取和寫入TXT文件的整理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • C語言利用鏈表實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)

    C語言利用鏈表實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言如何利用鏈表實(shí)現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-11-11
  • C++控制臺(tái)實(shí)現(xiàn)掃雷游戲

    C++控制臺(tái)實(shí)現(xiàn)掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了C++控制臺(tái)實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 判斷兩顆二叉樹是否相似的兩種方法

    判斷兩顆二叉樹是否相似的兩種方法

    今天小編就為大家分享一篇關(guān)于判斷兩顆二叉樹是否相似的兩種方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • C++中的單例模式介紹

    C++中的單例模式介紹

    單例模式也稱為單件模式、單子模式,可能是使用最廣泛的設(shè)計(jì)模式。其意圖是保證一個(gè)類僅有一個(gè)實(shí)例,并提供一個(gè)訪問它的全局訪問點(diǎn),該實(shí)例被所有程序模塊共享
    2013-03-03
  • C語言for循環(huán)嵌套for循環(huán)在實(shí)踐題目中應(yīng)用詳解

    C語言for循環(huán)嵌套for循環(huán)在實(shí)踐題目中應(yīng)用詳解

    初學(xué)C語言,常常遇到for循環(huán)中嵌套個(gè)for循環(huán),初學(xué)者對(duì)于這種形式總是一知半解,這次我就整理了常見的for循環(huán)嵌套for循環(huán)的題目,我們一起爭取一舉拿下這類題。學(xué)廢他們,以后再見到就不怕啦!每天都要學(xué)一點(diǎn)呀。加油,奮斗的我們
    2022-05-05
  • C語言實(shí)現(xiàn)生日賀卡

    C語言實(shí)現(xiàn)生日賀卡

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)生日賀卡的具體方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 深入分析C++中幾個(gè)最不常用的關(guān)鍵字

    深入分析C++中幾個(gè)最不常用的關(guān)鍵字

    本篇文章是對(duì)C++中幾個(gè)最不常用的關(guān)鍵字進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言的指針類型詳細(xì)解析

    C語言的指針類型詳細(xì)解析

    C語言的指針類型包括兩方面的信息:一是地址,存放在指針變量中;二是類型信息,關(guān)乎于讀寫的長度,沒有存儲(chǔ)在指針變量中,位于用該指針讀寫時(shí)的mov指令中,不同的讀寫長度對(duì)應(yīng)的mov指令不同
    2013-09-09

最新評(píng)論