[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++提高编程(一):模板----函数模板

相关推荐
tianyu23443 分钟前
Java 正则表达式终极指南:从 Pattern 到 Matcher,从双重转义到性能优化,一篇全搞定
java·开发语言·正则表达式·group·find·pattern·matcher
caimouse2 小时前
ReactOS 窗口系统分析(23):键盘 — keyboard.c + kbdlayout.c
c语言·开发语言·计算机外设
Emily156853598703 小时前
Emerson 1C31129G03 Analog Input Module
java·开发语言·前端·plc·emerson·1c31129g03·input module
就叫_这个吧8 小时前
Java递归方法实现面包屑导航
java·开发语言
小O的算法实验室8 小时前
IEEE TASE,基于MPC的多无人机协同搜索竞争群体优化方法
算法
fīɡЙtīиɡ ℡8 小时前
AI 应用系统设计
java·开发语言·人工智能
码匠许师傅8 小时前
【C++ 面试真题】26. 聊聊 C++ 的智能指针
java·c++·面试
Tbisnic9 小时前
BGE-M3 算法详解:从模型架构到三种检索方式的数学原理
算法·自然语言处理·大模型·bert·transformer·注意力机制
Brilliantwxx9 小时前
【算法从零到千】【55-58】哈希位图+常见数学运算 接口
算法
今天AI了吗9 小时前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·数据库·人工智能·python·sql·深度学习·机器学习