C++中類模板的應(yīng)用你了解多少
類模板應(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[] 重載中括號[] 5,~myArray() 析構(gòu)函數(shù) 6,getCapacity 獲取容量 7,getSize 獲取大小 8,pushback 尾插
將頭文件與實現(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];//這個不能直接拷貝,需要自己重新創(chuàng)建
for (int i = 0; i < arr.m_Size; i++)//然后將數(shù)組的元素一個個的賦值過來
{
this->pAddress[i] = arr.pAddress[i];
}
}
MyArray& operator=(const MyArray &arr)//重載賦值操作符=(返回自身的引用)
{
if (this->pAddress)//如果原先有數(shù)據(jù)了,那么就刪除
{
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];//這個不能直接拷貝,需要自己重新創(chuàng)建
for (int i = 0; i < arr.m_Size; i++)//然后將數(shù)組的元素一個個的賦值過來
{
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ù)組指針
int m_Capacity;//數(shù)組容量
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;
}
輸出結(jié)果:
100
101
102
103
104
105
106
107
108
109
以上代碼證明寫的數(shù)組類的封裝對內(nèi)置數(shù)據(jù)類型是適用的,接下來試試自定義類型Person
ps:如果識別出來了是要開辟Person類的數(shù)組的空間,需要調(diào)用Person的默認(rèn)構(gòu)造(有參構(gòu)造不行),所以必須在Person類中加一個默認(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];//這個不能直接拷貝,需要自己重新創(chuàng)建
for (int i = 0; i < arr.m_Size; i++)//然后將數(shù)組的元素一個個的賦值過來
{
this->pAddress[i] = arr.pAddress[i];
}
}
MyArray& operator=(const MyArray &arr)//重載賦值操作(返回自身的引用)
{
if (this->pAddress)//如果原先有數(shù)據(jù)了,那么就刪除
{
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];//這個不能直接拷貝,需要自己重新創(chuàng)建
for (int i = 0; i < arr.m_Size; i++)//然后將數(shù)組的元素一個個的賦值過來
{
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ù)組指針
int m_Capacity;//數(shù)組容量
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("小應(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ù)的使用說明分析。需要的朋友參考下2013-05-05
C語言利用鏈表實現(xiàn)學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細介紹了C語言如何利用鏈表實現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-11-11
C語言for循環(huán)嵌套for循環(huán)在實踐題目中應(yīng)用詳解
初學(xué)C語言,常常遇到for循環(huán)中嵌套個for循環(huán),初學(xué)者對于這種形式總是一知半解,這次我就整理了常見的for循環(huán)嵌套for循環(huán)的題目,我們一起爭取一舉拿下這類題。學(xué)廢他們,以后再見到就不怕啦!每天都要學(xué)一點呀。加油,奮斗的我們2022-05-05

