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

相关推荐
CoderCodingNo19 小时前
【GESP】C++五级真题(贪心和剪枝思想) luogu-B3930 [GESP202312 五级] 烹饪问题
开发语言·c++·剪枝
柯慕灵19 小时前
7大推荐系统/算法框架对比
算法·推荐算法
adam-liu19 小时前
Fun Audio Chat 论文+项目调研
算法·语音端到端·fun-audio-chat
kylezhao201919 小时前
第1章:第一节 开发环境搭建(工控场景最优配置)
开发语言·c#
啃火龙果的兔子19 小时前
JavaScript 中的 Symbol 特性详解
开发语言·javascript·ecmascript
栀秋66619 小时前
你会先找行还是直接拍平?两种二分策略你Pick哪个?
前端·javascript·算法
如果你想拥有什么先让自己配得上拥有20 小时前
数学思想和数学思维分别都有什么?
线性代数·算法·机器学习
热爱专研AI的学妹20 小时前
数眼搜索API与博查技术特性深度对比:实时性与数据完整性的核心差异
大数据·开发语言·数据库·人工智能·python
Mr_Chenph20 小时前
Miniconda3在Windows11上和本地Python共生
开发语言·python·miniconda3
阿狸远翔20 小时前
Protobuf 和 protoc-gen-go 详解
开发语言·后端·golang