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

目录

类模板语法

类模板与函数模板的区别

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

类模板对象做函数参数

类模板与继承

类模板成员函数类外实现

类模板份文件编写

类模板与友元

类模板使用示例


类模板的作用

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

类模板语法

template<typename T>
类

类模板与函数模板的区别

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

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

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

类模板对象做函数参数

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

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

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

类模板与继承

  • 当子类继承的父类是一个类模板时,子类在声明的时候要指定出父类中T的类型
  • 如果不指定,编译器无法给子类分配内存
  • 如果想灵活指定出父类中T的类型,子类也需要变为类模板
#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;
}

类模板成员函数类外实现

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

#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源文件
//修改头文件#include "person.h"改为
#include "person.hpp"
  • 2.将声明和实现写到同一个文件中,并修改后缀名为.hpp, hpp是约定名称,非强制。
//声明和实现写一起命名为.hpp文件,然后包含hpp文件
#include "person.hpp"

类模板与友元

  • 全局函数类内实现---直接在类内声明友元即可(建议使用)
  • 全局函数类外实现---需要提前让编译器知道全局函数的存在
#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;
}

类模板使用示例

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

相关推荐

  1. [C++提高编程]():模板--模板

    2024-03-22 20:36:02       22 阅读
  2. C++提高编程——模板

    2024-03-22 20:36:02       22 阅读
  3. C++提高编程—01.函数模板

    2024-03-22 20:36:02       33 阅读
  4. C++模板编程—学习C++库的编程基础

    2024-03-22 20:36:02       11 阅读
  5. C++模板分文件编写

    2024-03-22 20:36:02       35 阅读
  6. C/C++ - 模板

    2024-03-22 20:36:02       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-22 20:36:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-22 20:36:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-22 20:36:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-22 20:36:02       20 阅读

热门阅读

  1. CCF软件能力认证202312-1——仓库规划

    2024-03-22 20:36:02       20 阅读
  2. 5.3、【AI技术新纪元:Spring AI解码】图像生成API

    2024-03-22 20:36:02       18 阅读
  3. 树形el-select封装

    2024-03-22 20:36:02       19 阅读
  4. Element Plus 文本域设置固定行数

    2024-03-22 20:36:02       18 阅读
  5. trpc-go 博客系统

    2024-03-22 20:36:02       18 阅读
  6. 使用docker搭建docker-osx

    2024-03-22 20:36:02       19 阅读
  7. 蓝桥杯 蓝肽子序列

    2024-03-22 20:36:02       21 阅读
  8. echarts数据下钻如何配置

    2024-03-22 20:36:02       21 阅读