C++中类模板的应用你了解多少

 更新时间:2022年02月21日 15:23:27   作者:是小明同学啊  
这篇文章主要为大家详细介绍了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;
}

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容! 

相关文章

  • C++实现简单遗传算法

    C++实现简单遗传算法

    这篇文章主要介绍了C++实现简单遗传算法,以实例形式较为详细的分析了遗传算法的C++实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-05-05
  • C++深入探究类与对象之友元与运算符重载

    C++深入探究类与对象之友元与运算符重载

    友元就是让一个函数或者类,访问另一个类中的私有成员;打个比方,这相当于是说:朋友是值得信任的,所以可以对他们公开一些自己的隐私,运算符重载的实质就是函数重载或函数多态,运算符重载是一种形式的C++多态,目的在于让人能够用同名的函数来完成不同的基本操作
    2022-04-04
  • 详解C语言中const关键字的用法

    详解C语言中const关键字的用法

    这篇文章主要对C语言中const关键字的用法进行了详细的分析介绍,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-08-08
  • 简单讲解哈希表

    简单讲解哈希表

    本文主要介绍了哈希表简单知识及C语言实现哈希表实例,文中利用图片以及代码简单讲解了相关知识,感兴趣的小伙伴可以多多学习这篇文章
    2021-09-09
  • OpenCV图像轮廓提取的实现

    OpenCV图像轮廓提取的实现

    本文主要介绍了OpenCV图像轮廓提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • 基于C++编写一个简单的服务器

    基于C++编写一个简单的服务器

    这篇文章主要为大家详细介绍了如何基于C++编写一个简单的服务器,文中的示例代码讲解详细,具有一定的参考价值,感兴趣的小伙伴可以了解一下
    2023-03-03
  • C++中的ilst使用以及模拟实现

    C++中的ilst使用以及模拟实现

    list是一个类模板,加<类型>实例化才是具体的类,可以在任意位置进行插入和删除的序列式容器,本文将通过代码示例给大家介绍一下C++中的ilst使用以及模拟实现,需要的朋友可以参考下
    2023-08-08
  • C++使用jsoncpp解析json的方法示例

    C++使用jsoncpp解析json的方法示例

    这篇文章主要介绍了C++使用jsoncpp解析json的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • C++深入讲解类与封装的概念与使用

    C++深入讲解类与封装的概念与使用

    我们都知道C++有三大特性:封装、继承、多态,现在我们来总结一下封装的相关知识与类的概念,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-04-04
  • 构建mfc窗体的简单示例

    构建mfc窗体的简单示例

    这篇文章主要介绍了构建mfc窗体的简单示例,需要的朋友可以参考下
    2014-04-04

最新评论