[C++提高编程](二):模板--类模板

目录

类模板语法

类模板与函数模板的区别

类模板中的成员函数创建时机

类模板对象做函数参数

类模板与继承

类模板成员函数类外实现

类模板份文件编写

类模板与友元

类模板使用示例


类模板的作用

建立一个通用类,类中的成员,数据类型可以不具体制定,用一个虚拟类型来代表

类模板语法

cpp 复制代码
template<typename T>
类

类模板与函数模板的区别

  • 类模板没有自动类型推导的使用方式
  • 类模板在模板参数列表中可以有默认参数

类模板中的成员函数创建时机

  • 普通类中的成员函数一开始声明就可以创建
  • 类模板中的成员函数在调用时才创建

类模板对象做函数参数

  1. 指定传入的类型-----直接显示对象的数据类型(广泛使用)

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

  3. 整个类模板化-----将这个对象类型模板化进行传递

类模板与继承

  • 当子类继承的父类是一个类模板时,子类在声明的时候要指定出父类中T的类型
  • 如果不指定,编译器无法给子类分配内存
  • 如果想灵活指定出父类中T的类型,子类也需要变为类模板
cpp 复制代码
#include <iostream>
#include <string>

using namespace std;

template <class T>
class Base
{
public:
	T m;
};

//继承,父类为类模板, 1.指定类型
class Son1 :public Base<int>
{

};
//继承,父类为类模板, 2.灵活继承
template<class S1, class NameType>
class Son2 :public Base<NameType>
{
public:
	S1 m_obj;
	Son2(S1 obj, NameType name)
	{
		this->m_obj = obj;
		this->m = name;
	}
	void show_info()
	{
		cout << "son m_obj:" << this->m_obj << endl;
		cout << "son m:" << this->m << endl;
	}
};

void test_Son(void)
{
	Son2<string, int> m_son("son", 12);
	m_son.show_info();
}

int main()
{
	test_Son();
	return 0;
}

类模板成员函数类外实现

类模板中成员函数类外实现时,需要加上模板参数列表

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

using namespace std;

template<class NameType, class AgeType>  //默认参数
class Person
{
public:
	Person(NameType name, AgeType age);
	void show_Info();

	NameType m_Name;
	AgeType m_Age;
};

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

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

void test(void)
{
	Person<string, int> person("张三", 19); //必须显示指定类型
	person.show_Info();
}

int main()
{
	test();
	return 0;
}

类模板份文件编写

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

解决方案:

  • 1.直接包含.cpp源文件
cpp 复制代码
//修改头文件#include "person.h"改为
#include "person.hpp"
  • 2.将声明和实现写到同一个文件中,并修改后缀名为.hpp, hpp是约定名称,非强制。
cpp 复制代码
//声明和实现写一起命名为.hpp文件,然后包含hpp文件
#include "person.hpp"

类模板与友元

  • 全局函数类内实现---直接在类内声明友元即可(建议使用)
  • 全局函数类外实现---需要提前让编译器知道全局函数的存在
cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

//提前让编译器知道Person存在
template<class NameType, class AgeType> class Person;

//全局函数类外实现
template<class NameType, class AgeType>
void show_Info1(Person<NameType, AgeType>p)
{
	cout << "姓名:" << p.m_Name << ",年龄:" << p.m_Age << endl;
}

template<class NameType, class AgeType>  //默认参数
class Person
{
public:
	Person(NameType name, AgeType age);
	//全局函数配合友元类内实现
	friend void show_Info(Person<NameType, AgeType>p)
	{
		cout << "姓名:" << p.m_Name << ",年龄:" << p.m_Age << endl;
	}
	//全局函数类外实现  ;加空模板参数列表 ;编译器提前知道这个函数的存在
	friend void show_Info1<>(Person<NameType, AgeType>p);
private:
	NameType m_Name;
	AgeType m_Age;
};

template<class NameType, class AgeType>
Person<NameType, AgeType>::Person(NameType name, AgeType age)
{
	this->m_Name = name;
	this->m_Age = age;
}

void test(void)
{
	Person<string, int> person("张三", 19); //必须显示指定类型
	show_Info(person);
	show_Info1(person);
}

int main()
{
	test();
	return 0;
}

类模板使用示例

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

using namespace std;

template<class NameType, class AgeType = int>  //默认参数
class Person
{
public:
	Person(NameType name, AgeType age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	void show_Info()
	{
		cout << "姓名:" << this->m_Name << ",年龄:" << this->m_Age << endl;
	}

	NameType m_Name;
	AgeType m_Age;
};

void test(void)
{
	Person<string, int> person("张三", 19); //必须显示指定类型

	Person<string> person_1("李四", 19);

	person.show_Info();
	person_1.show_Info();
}
//指定传入类型
void printPerson(Person<string, int> &person)
{
	person.show_Info();
}
//将参数模板化
template<class NameType, class AgeType>
void printPerson1(Person<NameType, AgeType>& person)
{
	person.show_Info();
	cout << "NameType数据类型为:" << typeid(NameType).name() << endl;
	cout << "AgeType数据类型为:" << typeid(AgeType).name() << endl;
} 

//整个类模板化
template<class T>
void printPerson1(T & person)
{
	person.show_Info();
	cout << "T数据类型为:" << typeid(T).name() << endl;
}

void test_parm(void)
{
	//指定传入类型
	Person<string, int> person("张三", 199);
	printPerson(person);

	//将参数模板化
	printPerson1(person);

	//整个类模板化
	printPerson1(person);
}

int main()
{
	test();
	test_parm();
	return 0;
}

推荐文章:[C++提高编程](一):模板----函数模板

相关推荐
程序员-King.4 小时前
day158—回溯—全排列(LeetCode-46)
算法·leetcode·深度优先·回溯·递归
优雅的潮叭4 小时前
c++ 学习笔记之 chrono库
c++·笔记·学习
星火开发设计4 小时前
C++ 数组:一维数组的定义、遍历与常见操作
java·开发语言·数据结构·c++·学习·数组·知识
月挽清风5 小时前
代码随想录第七天:
数据结构·c++·算法
TTGGGFF5 小时前
控制系统建模仿真(一):掌握控制系统设计的 MAD 流程与 MATLAB 基础运算
开发语言·matlab
小O的算法实验室5 小时前
2026年AEI SCI1区TOP,基于改进 IRRT*-D* 算法的森林火灾救援场景下直升机轨迹规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
2501_944424125 小时前
Flutter for OpenHarmony游戏集合App实战之贪吃蛇食物生成
android·开发语言·flutter·游戏·harmonyos
小郭团队5 小时前
2_1_七段式SVPWM (经典算法)算法理论与 MATLAB 实现详解
嵌入式硬件·算法·硬件架构·arm·dsp开发
充值修改昵称5 小时前
数据结构基础:从二叉树到多叉树数据结构进阶
数据结构·python·算法
Deepoch6 小时前
Deepoc数学大模型:发动机行业的算法引擎
人工智能·算法·机器人·发动机·deepoc·发动机行业