C++的进阶泛型编程学习(2):类模板的机制

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

上一节学到函数模板,意思就是把函数的返回类型和传递参数类型用一个抽象的模板名来代替,从而起到支持泛化数据输入的功能,提高了函数的多用性。

那么类,可以使用模板机制来提高泛用性吗?当然可以。

一、类模板

类模板(Class Template)是C++中的一种特殊的模板,用于定义通用的类。类模板可以根据不同的数据类型生成具体的类,从而实现代码的复用和泛化。

类模板的定义同样使用关键字template,后面跟着模板参数列表,其中可以包含类型参数、非类型参数和模板参数的默认值。例如:

c 复制代码
#include <iostream>
#include<string>

using namespace std;

template <class nametype, class agetype>
class person {
    // 类定义
public:
    person(nametype name, agetype age) {
        this->age = age;
        this->name = name;
    }
    nametype name;
    agetype age;
};

int main(void) {

    person<string,int> p("李明", 15);
    person<string, float> p2("小红", 18.5);
    system("pause");
    return 0;
}

代码使用了类模板来定义了一个名为person的类,该类有两个模板参数:nametype和agetype,分别表示姓名和年龄的数据类型。

在person类中,有一个构造函数,接受两个参数:name和age,并将它们分别赋值给类的成员变量name和age。

在main函数中,你使用了类模板来实例化了两个不同类型的person对象。p是一个person<string, int>对象,name的类型为string,age的类型为int。p2是一个person<string, float>对象,name的类型为string,age的类型为float。

最后,你使用了system("pause")来暂停程序的执行,以便在Windows系统下查看输出结果。

二、类模板和函数模板的区别

1.类模板没有自动类型推导的方式

即:

person<string,int> p("李明", 15);//合法的

类模板实例化时必须指定参数类型

person p3("王刚", 14);//不合法的

不指定的话是不合法的。

2.类模板在模板参数列表中可以有默认参数

代码如下(示例):

c 复制代码
#include <iostream>
#include<string>

using namespace std;

template <typename nametype, typename agetype=int>
class person {
    // 类定义
public:
    person(nametype name, agetype age) {
        this->age = age;
        this->name = name;
    }
    nametype name;
    agetype age;
};

int main(void) {

    person<string,int> p("李明", 15);
    person<string, float> p2("小红", 18.5);
    person<string> p3("王刚", 14);
    system("pause");
    return 0;
}

提前指定好类模板参数列表的int型,那么实例化时就不用指定整形了。

三、类模板对象做函数参数

学习目标:类模板实例化出的对象,向函数传参的方式

一共有三种传入方式:

指定传入的类型:直接显示对象的数据类型

参数模板化:将对象中的参数变为模板进行传递

整个类模板化:将这个对象类型模板化进行传递

c 复制代码
//类模板对象做函数参数
template<class T1,class T2>
class Person
{
public:
	Person(T1 name,T2 age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}

	void showPerson()
	{
		cout << "name: " << this->m_Name << " age:" << this->m_Age << endl;
	}

	T1 m_Name;
	T2 m_Age;
};

//1、指定传入类型
void printPerson1(Person<string, int>&p)
{
	p.showPerson();
}
void test01()
{
	Person<string, int>p("孙悟空", 199);
	printPerson1(p);
}

   
// 2、参数模板化
template<class T1,class T2>
void printPerson2(Person<T1,T2>&p)
{
	p.showPerson();
	cout << "T1的类型为:" << typeid(T1).name() << endl;
	cout << "T2的类型为:" << typeid(T2).name() << endl;
}
void test02()
{
	Person<string, int>p("猪八戒", 90);
	printPerson2(p);
}

// 3、整个类模板化
template<class T>
void printPerson3(T &p)
{
	p.showPerson();
	cout << "T的类型为:" << typeid(T).name() << endl;
}
void test03()
{
	Person<string, int>p("唐僧", 60);
	printPerson3(p);
}

int main()
{
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}

三种不同的方式来将类模板对象作为函数参数传递。

指定传入类型: 在printPerson1函数中,我们明确指定了Person<string, int>作为参数类型。然后我们创建一个Person<string, int>对象p,并将其传递给printPerson1函数。函数内部可以通过对象p调用成员函数showPerson来显示对象的信息。

参数模板化: 在printPerson2函数中,我们使用了参数模板化的方式,即Person<T1, T2>作为参数类型。然后我们创建一个Person<string, int>对象p,并将其传递给printPerson2函数。函数内部可以通过对象p调用成员函数showPerson来显示对象的信息。此外,我们还使用typeid来获取T1和T2的类型信息。

整个类模板化: 在printPerson3函数中,我们将整个类模板Person作为参数类型。然后我们创建一个Person<string, int>对象p,并将其传递给printPerson3函数。函数内部可以通过对象p调用成员函数showPerson来显示对象的信息。此外,我们还使用typeid来获取T的类型信息。

四、类模板与继承

当类模板碰到继承时,需要注意以下几点:

4.1当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型

c 复制代码
//类模板与继承
template<class T>
class Base
{
	T m;
};

//class Son: public Base //错误,必须要知道父类中的T类型,才能继承给子类
class Son :public Base<int>//合法
{

};

4.2 如果不指定,编译器无法给子类分配内存

4.3 如果想灵活指定出父类中T的类型,子类也需为类模板

c 复制代码
//如果想灵活指定父类中T的类型,子类也需要变成类模板
template<class T1,class T2>
class Son2 : public Base<T2>
{
public:
	Son2()
	{
		cout << "T1的类型为:" << typeid(T1).name() << endl;
		cout << "T2的类型为:" << typeid(T2).name() << endl;

	}
	T1 obj;
};

五、类模板成员函数类外实现

示例:

c 复制代码
//类模板成员类外实现
template<class T1, class T2>
class Person
{
public:
	Person(T1 name, T2 age);
	/*{
		this->m_Name = name;
		this->m_Age = age;
	}*/

	void showPerson();
	/*{
		cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
	}*/

	T1 m_Name;
	T2 m_Age;
};

//构造函数的类外实现
template<class T1,class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
	this->m_Name = name;
	this->m_Age = age;
}


//成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
	cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
}

void test01()
{
	Person<string, int>p("Tom", 30);
	p.showPerson();
}


int main()
{
	test01();
	
	system("pause");
	return 0;
}

六、类模板分文件编写

如果工程中需要利用多个类模板,那么将这些类模板都写在同一个文件中将会导致代码可读性变差,所以有必要对类模板进行分文件编写,但是类模板的分文件编写面临着一些问题,以下是类模板分文件编写面临的问题及解决方法。

问题:类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到

解决:

解决方式1:直接包含.cpp源文件

解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制的

示例1:(未进行分文件编写)

c 复制代码
template<class T1, class T2>
class Person
{
public:
	Person(T1 name, T2 age);
	/*{
		this->m_Name = name;
		this->m_Age = age;
	}*/

	void showPerson();
	/*{
		cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
	}*/

	T1 m_Name;
	T2 m_Age;
};

//构造函数的类外实现
template<class T1,class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
	this->m_Name = name;
	this->m_Age = age;
}


//成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
	cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
}

void test01()
{
	Person<string, int>p("Tom", 30);
	p.showPerson();
}


int main()
{
	test01();
	
	system("pause");
	return 0;
}

实例2:(进行分文件编写,利用.cpp)

1.创建头文件person.h,写一些声明

c 复制代码
#pragma once
#include <iostream>
using namespace std;
#include <string>

template<class T1, class T2>
class Person
{
public:
	Person(T1 name, T2 age);
	void showPerson();

	T1 m_Name;
	T2 m_Age;
};

2.创建person.cpp,写具体实现

c 复制代码
#include "person.h"

//构造函数的类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
	this->m_Name = name;
	this->m_Age = age;
}


//成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
	cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
}

3.main函数编写

错误代码:

c 复制代码
#include <iostream>
using namespace std;
#include <string>
#include "person.h"

void test01()
{
	Person<string, int>p("Tom", 30);
	p.showPerson();
}


int main()
{
	test01();
	
	system("pause");
	return 0;
}

注:因为如果包含person.h文件,那么编译器将会看到person.h中的代码。但是由于类模板中的成员函数一开始是不创建的,导致编译器没有看到person.cpp中的代码,所以执行test01时,无法解析其中的代码。

正确代码:(不常用)

c 复制代码
#include <iostream>
using namespace std;
#include <string>
#include "person.cpp"

void test01()
{
	Person<string, int>p("Tom", 30);
	p.showPerson();
}


int main()
{
	test01();
	
	system("pause");
	return 0;
}

注:就是将person.h文件改成了person.cpp代码。编译器首先看到了person.cpp文件,因为person.cpp文件中有person.h文件,编译器又看到了person.h文件,所以能够解析test01中的代码。但是一般很少直接包含.cpp文件的,所以这个方法不常用。

实例3:(分文件编写,利用.hpp)

将person.h和person.cpp的内容写到一起,并将后缀名改为.hpp,这是类模板分文件编写最常用的方式

1.编写person.hpp文件:

c 复制代码
#pragma once
#include <iostream>
using namespace std;
#include <string>

template<class T1, class T2>
class Person
{
public:
	Person(T1 name, T2 age);
	void showPerson();

	T1 m_Name;
	T2 m_Age;
};

//构造函数的类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
	this->m_Name = name;
	this->m_Age = age;
}


//成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
	cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << endl;
}

2.编写main函数

c 复制代码
#include <iostream>
using namespace std;
#include <string>
#include "person.hpp"

void test01()
{
	Person<string, int>p("Tom", 30);
	p.showPerson();
}


int main()
{
	test01();
	
	system("pause");
	return 0;
}

八、类模板与友元

全局函数类内实现:直接在类内声明友元即可

全局函数类外实现:需要提前让编译器知道全局函数的存在

1.全局函数的类内实现

c 复制代码
template<class T1, class T2>
class Person
{
	//全局函数类内实现
	friend void printPerson(Person<T1, T2> p)
	{
		cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
	}
public:
	Person(T1 name, T2 age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	
private:
	T1 m_Name;
	T2 m_Age;
};

void test01()
{
	Person<string, int>p("Tom", 30);
	printPerson(p);
}
int main()
{
	test01();
	
	system("pause");
	return 0;
}

2.全局函数类外实现

c 复制代码
//提前让编译器知道Person类的存在
template<class T1, class T2>
class Person;

//类外实现
template<class T1, class T2>
void printPerson(Person<T1, T2> p)
{
	cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}

template<class T1, class T2>
class Person
{
	//全局函数类外实现 
	//加空模板参数列表
	//如果全局函数是类外实现,需要让编译器提前知道这个函数的存在
	friend void printPerson<>(Person<T1, T2> p);
public:
	Person(T1 name, T2 age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	
private:
	T1 m_Name;
	T2 m_Age;
};



void test01()
{
	Person<string, int>p("Tom", 30);
	printPerson(p);
}


int main()
{
	test01();
	
	system("pause");
	return 0;
}

注:需要注意各个函数声明之间的顺序。在Person类模板中有友元的声明friend void printPerson<>(Person<T1, T2> p),因为类模板中友元的类外实现需要让编译器提前知道这个函数,所以需要将printPerson函数写在前面。而printPerson函数中又涉及Person类,所以在printPerson函数前面需要提前声明Person类模板的存在。

总结:建议全局函数做类内实现,用法简单,而且编译器可以直接识别。

九、类模板案例

案例描述:

可以对内置数据类型以及自定义数据类型的数据进行存储

将数组中的数据存储到堆区

构造函数中可以传入数组的容量

提供对应的拷贝构造函数以及operator=防止浅拷贝的问题

提供尾插法和尾删法对数组中的数据进行增加和删除

可以通过下标的方式访问数组中的元素

可以获取数组中当前元素个数和数组的容量

MyArray.hpp文件:

c 复制代码
#pragma once
#include <iostream>
using namespace std;


template<class T>
class MyArray
{
public:
	//构造函数
	MyArray(int capacity)
	{
		cout << "MyArray有参构造的调用" << endl;
		this->m_Capacity = capacity;
		this->pAdress = new T[this->m_Capacity];
		this->m_Size = 0;
	}

	//析构函数
	~MyArray()
	{
		
		if (this->pAdress != NULL)
		{
			cout << "MyArray析构的调用" << endl;
			delete[] this->pAdress;
			this->pAdress = NULL;
		}
	}

	//拷贝构造
	MyArray(const MyArray& arr)
	{
		cout << "MyArray拷贝构造的调用" << endl;
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;

		//深拷贝
		this->pAdress = new T[arr.m_Capacity];

		//将arr中的数据都拷贝过来
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAdress[i] = arr.pAdress[i];
		}
	}

	//operator= 防止浅拷贝问题
	MyArray& operator=(const MyArray& arr)
	{
		cout << "MyArray的operator=调用" << endl;
		//先判断原来堆区是否有数据,如果有先释放
		if (this->pAdress != NULL)
		{
			delete[] this->pAdress;
			this->pAdress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0; 
		}

		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;

		//深拷贝
		this->pAdress = new T[arr.m_Capacity];

		//将arr中的数据都拷贝过来
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAdress[i] = arr.pAdress[i];
		}
		return *this;
	}

	//尾插法
	void Push_Back(const T& val)
	{
		//判断容量是否等于大小
		if (this->m_Size == this->m_Capacity)
		{
			return;
		}
		this->pAdress[this->m_Size] = val; //在数组末尾插入数据
		this->m_Size++; //更新数组大小
	}

	//尾删法
	void Pop_Back()
	{
		//让用户访问不到最后一个元素即为尾删法 
		if (this->m_Size == 0)
		{
			return;
		}
		this->m_Size--;
	}

	//通过下标方式访问数组中的元素
	T& operator[](int index) //以引用作为返回值是为了能够做arr[0]=100这样的操作
	{
		return this->pAdress[index];
	}

	//返回数组容量
	int getCapacity()
	{
		return this->m_Capacity;
	}

	//返回数组大小
	int getSize()
	{
		return this->m_Size;
	}

private:
	T *pAdress; //指针指向堆区开辟的真实数组

	int m_Capacity; //数组容量

	int m_Size; //数组大小
};

main文件:

c 复制代码
#include <iostream>
using namespace std;
#include "MyArray.hpp"
#include <string>

void printIntArray(MyArray<int>& arr1)
{
	for (int i = 0; i < arr1.getSize(); i++)
	{
		cout << arr1[i] << " ";
	}
	cout << endl;
}


void test01()
{
	MyArray<int> arr1(5);
	MyArray<int> arr2(arr1);
	MyArray<int> arr3(100);
	arr3 = arr1;

	cout << "--------------------" << endl;

	MyArray<int>arr4(5);
	for (int i = 0; i < 5; i++)
	{
		//通过尾插法向数组中插入数据
		arr4.Push_Back(i);
	}

	cout << "arr4的打印输出为:" << endl;
	printIntArray(arr4);
	cout << "arr4的容量为:" << arr4.getCapacity() << endl;
	cout << "arr4的大小为:" << arr4.getSize() << endl;

	MyArray<int>arr5(arr4);
	cout << "arr5的打印输出为:" << endl;
	printIntArray(arr5);
	//尾删法
	arr5.Pop_Back();
	cout << "arr5尾删后的打印输出为:" << endl;
	printIntArray(arr5);
}

//测试自定义类型
class Person
{
public:
	Person()
	{

	}
	Person(string name,int age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
	string m_Name;
	int m_Age;
};

void printPersonArray(MyArray<Person>& arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << "Name: " << arr[i].m_Name << "  Age: " << arr[i].m_Age << endl;
	}
}

void test02()
{
	MyArray<Person> arr(10);
	Person p1("Tom", 12);
	Person p2("Jack", 15);
	Person p3("Bill", 17);

	//数据插入到数组中
	arr.Push_Back(p1);
	arr.Push_Back(p2);
	arr.Push_Back(p3);
	printPersonArray(arr);
	cout << "arr的容量为:" << arr.getCapacity() << endl;
	cout << "arr的大小为:" << arr.getSize() << endl;
}

int main()
{
	test01();
	cout << "==========" << endl;
	test02();
	system("pause");
	return 0;
}
相关推荐
sz66cm2 小时前
Python基础 -- 使用Python实现ssh终端并实现数据处理与统计功能
开发语言·python·ssh
小灰灰爱代码2 小时前
C++——求3*3矩阵对角元素之和。
数据结构·c++·算法
老K(郭云开)2 小时前
allWebPlugin中间件自定义alert、confirm及prompt使用
c++·chrome·中间件·prompt·html5·edge浏览器
liangbm33 小时前
MATLAB系列02:MATLAB基础
开发语言·数据结构·笔记·matlab·教程·工程基础·高级绘图
change95134 小时前
PHP纯离线搭建(php 8.1.7)
开发语言·php
福鸦4 小时前
详解c++:new和delete
开发语言·c++
qq_172805594 小时前
Go Testify学习与使用
开发语言·golang·go
深蓝海拓4 小时前
迭代器和生成器的学习笔记
笔记·python·学习
兩尛5 小时前
java--面向对象编程(中级部分)
java·开发语言
createcrystal5 小时前
《算法笔记》例题解析 第3章入门模拟--3图形输出(9题)2021-03-03
c++·笔记·算法