1.3类模板

1.3.1类模板语法

类模板作用:

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

语法:

 templa<typename T>

类

解释:

template --- 声明创建模板

typename --- 表明其后面的符号是一种数据类型,class代替

T --- 通用的数据类型,名称可以替换,通常为大写字母

示例:

#include<iostream>
#include<string.h>
using namespace std;

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

	void showPerson()
	{
		cout << "name:" << this->m_Name << "age:" << this->m_Age << endl;
	}

	NameType m_Name;
	AgeType m_Age;
};

void test01()
{
	Person<string, int> p1("孙悟空", 999);
	p1.showPerson();
}

int main()
{
	test01();

	system("pause");
}

1.3.2 类模板与函数模板区别

类模板与函数模板区别主要有两点:

1.类模板没有自动类型推导的使用方式

2.类模板在模板参数列表中可以有默认参数

示例:

#include<iostream>
#include<string.h>
using namespace std;


//类模板与函数模板区别
template<class NameType,class AgeType=int>
class Person
{
public:
	Person(NameType name, AgeType age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}



	void showPerson()
	{
		cout << "name: " << this->m_Name << "age: " << this->m_Age << endl;
	}

	NameType m_Name;
	AgeType m_Age;
};

//1.类模板没有自动类型推导使用方式
void test01()
{
	//Person p("孙悟空", 100); 错误:无法用自动类型推导
	Person<string, int>p("孙", 1000);//正确,只能用显式指定类型
	p.showPerson();
}
//2.类模板在模板参数列表中可以有默认参数
void test02()
{
	Person<string>p("猪八戒", 99);
	p.showPerson();
}

int main()
{
	test01();
	test02();
}

总结:

类模板使用只能用显示指定类型方式

类模板中的模板参数列表可以有默认参数

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

类模板中成员函数和普通类中成员函数创建时机是有区别的:

普通类中成员函数一开始就可以创建

类模板中的成员函数在调用时才创建

#include<iostream>
#include<string.h>
using namespace std;

class Person1
{
public:
	void showPerson1()
	{
		cout <<"Person1 show" << endl;
	}
};

class Person2
{
public:
	void showPerson2()
	{
		cout << "Person2 show" << endl;
	}
};

template<class T>
class MyClass
{
public:
	T obj;

	//类模板中的成员函数
	void func1()
	{
		obj.showPerson1();
	}
	void func2()
	{
		obj.showPerson2();
	}
};

void test01()
{
	MyClass<Person1>m;
	m.func1();
	//m.func2();
}

int main()
{
	test01();
}

总结:类模板中的成员函数并不是一开始就创建的,在调用时才去创建

1.3.4 类模板对象做函数参数

学习目标:

类模板实例化出的对象,向函数传参的方式

一共有三种传入方式:

1.指定传入的类型 ---直接显示的对象的数据类型

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

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

#include<iostream>
#include<string.h>
using namespace std;

//类模板对象做函数参数

template<class T1,class T2>
class Person
{
public:
	Person(T1 name, T2 age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}

	void showPerson()
	{
		cout << "姓名:  " << this->m_Name << "年龄:  " << this->m_Age << endl;
	}
	T1 m_Name;
	T2 m_Age;
};
//1.指定传入类型
void printPerson1(Person<string, int>&p)
{
	p.showPerson();
}

void test01()
{
	Person<string, int>p("sunwukoong", 100);
	printPerson1(p);
}

//2.参数模板化
template<class T1,class T2>
void printPerson2(Person<T1, T2>& p)
{
	p.showPerson();
	cout << "T1 的类型为:" << typeid(T1).name() << endl;
	cout << "T2 的类型为:" << typeid(T2).name() << endl;
}

void test02()
{
	Person<string, int>p("zhubajie", 100);
	printPerson2(p);
}


//3.整个类模板化
template<class T>
void printPerson3(T& p)
{
	p.showPerson();
	cout << "T的数据类型为:" << typeid(T).name() << endl;
}

void test03()
{
	Person<string, int>p("Tangseng", 13);
	printPerson3(p);
}

int main()
{
	test01();

	test02();

	test03();
	system("pause");

	return 0;
}

总结:

通过类模板创建的对象,可以有三种方式向函数中进行传参

使用比较广泛是第一种:指定传入的类型

1.3.5类模板与继承

当类模板碰到继承时,需要注意以下几点:

当子类继承的父类是一个类模板时,子类在声明的时候,需要指定出父类中T的类型

如果不指定,编译器无法给子类分配内存

如果想灵活指定出父类中T的类型,子类也需要变为类模板

#include<iostream>
using namespace std;

//类模板与继承
template<class T>
class Base
{
	T m;
};

//class Son:public Base//错误,必须要知道父类中的T类型,才能继承子类
class Son :public Base<int>
{

};

void test01()
{
	Son s1;
}

//如果想灵活指定父类中T类型,子类也需要变类模板
template<class T1,class T2>
class Son2 :public Base<T2>
{
public:
	Son2()
	{
		cout << "T1的类型为:" << typeid(T1).name() << endl;
		cout << "T2的类型为:" << typeid(T2).name() << endl;
	}
	T1 obj;
};

void test02()
{
	Son2<int, char>S2;
}

int main()
{
	test02();


	system("pause");
	
	return 0;
}

总结:如果父类是类模板,子类需要指定出父类中T的数据类型

1.3.6 类模板成员函数类外实现

学习目标:能够掌握类模板中的成员函数类外实现

示例:

#include<iostream>
using namespace std;

template<class T1,class T2>
class Person
{
public:
	Person(T1 name, T2 age);

	void showPerson();

	T1 m_Name;
	T2 m_Age;
};

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

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


void test01()
{
	Person<string, int> P("Tom", 20);
	P.showPerson();
}

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

1.3.7 类模板分文件编写

学习目标:

掌握类模板成员函数份文件编写产生的问题以及解决方式

问题:

类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到

解决:

解决方式1:直接包含cpp源文件

解决方式2:将声明和实现写到同一个文件夹中,并更改后缀名为hpp,hpp是 约定的名称并不是强制

示例:

person.hpp中代码

#include<iostream>
using namespace std;
//第一种解决方式,直接包含源文件
//#include "person.cpp"

//第二种解决方式,将.h和.cpp中的内容写到一起,将后缀名改为.hpp文件
#include "person.hpp"
//template<class T1,class T2>
//class Person
//{
//public:
//	Person(T1 name, T2 age);
//	void showPerson();
//
//	T1 m_Name;
//	T2 m_Age;
//};
//
//template<class T1,class T2>
//Person<T1, T2>::Person(T1 name, T2 age)
//{
//	this->m_Name = name;
//	this->m_Age = age;
//}
//
//template<class T1, class T2>
//void Person<T1, T2>::showPerson()
//{
//	cout << "姓名:  " << this->m_Name << "年龄 :  " << this->m_Age << endl;
//}

void test01()
{
	Person <string, int>p("Jerry", 18);
	p.showPerson();
}

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

总结:主流的解决方式是第二种,将类模板成员函数写到一起,并将后缀名改为.hpp

1.3.8 类模板与友元

学习目标:掌握类模板配合友元函数的类内和类外实现

全局函数类内实现-直接在类内声明友元即可

全局函数类外实现-需要提前让编译器知道全局函数的存在

示例:

#include<iostream>
using namespace std;

template<class T1,class T2>
class Person;

template<class T1, class T2>
void printPerson2(Person<T1, T2>p)
{
	cout << "姓名:" << p.m_Name << "年龄:" << p.m_Age << endl;
}

//通过全局函数 打印Person信息

template<class T1,class T2>
class Person
{
	//1.全局函数在类内实现
	friend void printPerson(Person<T1, T2>p)
	{
		cout << "姓名:" << p.m_Name << "年龄:" << p.m_Age << endl;
	}

	//全局函数类外实现
	//加空模板参数列表
	//如果全局函数是类外实现,需要让编译器提前知道这个函数存在
	friend void printPerson2<>(Person<T1, T2>p);


public:
	Person(T1 name, T2 age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}

private:
	T1 m_Name;
	T2 m_Age;
};



void test02()
{	
	Person<string, int>p("Jerry", 20);
	printPerson2(p);
}

void test01()
{
	Person<string, int>p("tom", 20);

	printPerson(p);
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

总结:建议全局函数做类内实现,类外实现一般没必要

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值