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

相关推荐
R-G-B2 小时前
【25】MFC入门到精通——MFC静态文本框 中字符串 连续输出 不覆盖先前的文本 换行输出
c++·mfc·mfc静态文本框输出字符串·mfc静态文本框连续输出字符串·mfc静态文本框换行输出字符串
我搞slam2 小时前
快乐数--leetcode
算法·leetcode·哈希算法
WWZZ20253 小时前
快速上手大模型:机器学习3(多元线性回归及梯度、向量化、正规方程)
人工智能·算法·机器学习·机器人·slam·具身感知
应用市场4 小时前
构建自定义命令行工具 - 打造专属指令体
开发语言·windows·python
东方佑4 小时前
从字符串中提取重复子串的Python算法解析
windows·python·算法
西阳未落4 小时前
LeetCode——二分(进阶)
算法·leetcode·职场和发展
通信小呆呆5 小时前
以矩阵视角统一理解:外积、Kronecker 积与 Khatri–Rao 积(含MATLAB可视化)
线性代数·算法·matlab·矩阵·信号处理
FFZero15 小时前
【C++/Lua联合开发】 (二) Lua调用C++函数
c++·junit·lua
Dfreedom.5 小时前
一文掌握Python四大核心数据结构:变量、结构体、类与枚举
开发语言·数据结构·python·变量·数据类型
一半烟火以谋生5 小时前
Python + Pytest + Allure 自动化测试报告教程
开发语言·python·pytest