【c++】模板2—类模板


类模板语法

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

语法:

template<typename T>

解释:
template—声明创建模板
typename—表明其后面的符号是一种数据类型,可以用class代替
T—通用的数据类型,名称可以替换,通常为大写字母

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

template<class nameT,class ageT>
class person
{
public:
	person(nameT name, ageT age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	void showperson()
	{
		cout << "name:" << this->m_name << endl;
		cout << "age:" << this->m_age << endl;
	}
	nameT m_name;
	ageT m_age;
};

void test()
{
	//通过类模板来实例化对象
	//指定nametype为string类型,agetype为int类型
	person<string, int> p1("张三", 18);
	p1.showperson();
}

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

类模板与函数模板区别

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

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

template<class nameT,class ageT=int>
class person
{
public:
	person(nameT name, ageT age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	void showperson()
	{
		cout << "name:" << this->m_name << endl;
		cout << "age:" << this->m_age << endl;
	}
	nameT m_name;
	ageT m_age;
};

//1、类模板没有自动类型推导的使用方式
void test1()
{
	//person p("张三", 18);//错误!类模板使用时,不可以用自动类型推导
	person<string, int>p("张三", 18);//必须使用显示指定类型的方式,使用类模板
	p.showperson();
}

//2、类模板在模板参数列表中可以有默认参数
void test2()
{
	person<string>p("李四",10);//类模板中的模板参数列表可以指定默认参数
	p.showperson();
}

int main()
{
	test1();
	test2();
	system("pause");
	return 0;
}

类模板中成员函数常见时机

类模板中成员函数和普通类中成员函数创建时机是有区别的:
1、普通类中成员函数一开始就可以创建;
2、类模板中成员函数在调用时才创建。

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

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 test()
{
	myclass<person1>m;
	m.func1();
	//m.func2();//编译会出错,说明函数调用才会去创建成员函数,这行myclass<person1>m;的person1改成person2就可以编译成功
}

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

类模板对象做函数参数

类模板实例化出的对象,可以通过三种方式向函数中进行传参:
1、指定传入的类型——直接显示对象的数据类型;
2、参数模板化——将对象中的参数变为模板进行传参;
3、整个类模板化——将这个对象类型模板化进行传递。

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

template<class T1,class T2>
class person
{
public:
	person(T1 name, T2 age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	void showperson()
	{
		cout << "name:" << this->m_name << endl;
		cout << "age:" << this->m_age << endl;
	}

	T1 m_name;
	T2 m_age;
};

//1、指定传入类型
void printperson1(person<string, int>& p)
{
	p.showperson();
}
void test1()
{
	person<string, int>p("张三", 18);
	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 test2()
{
	person<string, int>p("李四", 19);
	printperson2(p);
}

//3、整个类模板化
template<class T>
void printperson3(T& p)
{
	p.showperson();
	cout << "T的类型:" << typeid(T).name() << endl;
}
void test3()
{
	person<string, int>p("王五", 20);
	printperson3(p);
}

int main()
{
	test1();
	test2();
	test3();
	system("pause");
	return 0;
}

类模板与继承

当类模板碰到继承时,需要注意以下几点:
1、当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型;
2、如果不指定,编译器无法给子类分配内存;
3、如果想灵活指定出父类中T1的类型,子类也需编程类模板。

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

template<class T>
class dad
{
	T m;
};

//class son1 :public dad//错误!必须要知道父类中的T类型,才能继承给子类
class son1 :public dad<int>//必须指定一个类型
{
};

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

void test()
{
	son1 s1;
	son2<int, char>s2;
}

int main()
{
	test();

	system("pause");
	return 0;
}

类模板成员函数类外实现

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

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

//类内实现
template<class T1, class T2>
class person1
{
public:
	person1(T1 name, T2 age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	void showperson1()
	{
		cout << "姓名:" << this->m_name << endl;
		cout << "年龄:" << this->m_age << endl;
	}
	T1 m_name;
	T2 m_age;
};

//类外实现
template<class T1, class T2>
class person2
{
public:
	person2(T1 name, T2 age);

	void showperson2();

	T1 m_name;
	T2 m_age;
};

//构造函数类外实现
template<class T1, class T2>
person2<T1, T2>::person2(T1 name, T2 age)
{
	this->m_name = name;
	this->m_age = age;
}

//成员函数类外实现
template<class T1, class T2>
void person2<T1, T2>::showperson2()
{
	cout << "姓名:" << this->m_name << endl;
	cout << "年龄:" << this->m_age << endl;
}

void test()
{
	person2<string, int>p("张三", 18);
	p.showperson2();
}

int main()
{
	test();

	system("pause");
	return 0;
}

类模板分文件编写

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

解决:
解决方式1:直接包含.cpp源文件;
解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制的。

第一种解决方式:

main.cpp文件

#include<iostream>
using namespace std;

//第一种解决方式:直接包含源文件
#include"person.cpp"

void test()
{
	person<string, int>p("张三", 18);
	p.showperson();
}

int main()
{
	test();

	system("pause");
	return 0;
}

person.h文件

#pragma once
#include<iostream>
using namespace std;

#include<string>
template<class T1, class T2>
class person
{
public:
	person(T1 name, T2 age);
	void showperson();

	T1 m_name;
	T2 m_age;
};

person.cpp文件

#include"person.h"

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 << "name:" << this->m_name << endl;
	cout << "age:" << this->m_age << endl;
}

第二种解决方式:

main.cpp文件

#include<iostream>
using namespace std;

//第二种解决方式:将.h和.cpp的内容写在一起,将后缀名改为.hpp文件
#include"person.hpp"

void test()
{
	person<string, int>p("张三", 18);
	p.showperson();
}

int main()
{
	test();

	system("pause");
	return 0;
}

person.hpp文件

#pragma once
#include<iostream>
using namespace std;

#include<string>

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 << "name:" << this->m_name << endl;
	cout << "age:" << this->m_age << endl;
}

类模板与友元

全局函数类内实现——直接在类内声明友元即可;
全局函数类外实现——需要提前让编译器知道全局函数的存在。

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

//类外实现
//全局函数配合友元 类外实现——先做函数声明,下方再做模板定义,再做友元
template<class T1, class T2>class person;

//如果声明了函数模板,可以将实现写在后面,否则将实现体写在类的前面让编译器提前看到
//template<class T1, class T2>void printperson2(person<T1, T2> p);

template<class T1, class T2>
void printperson2(person<T1, T2> p)
{
	cout << "类外实现:" << endl;
	cout << "姓名:" << p.m_name << endl;
	cout << "年龄:" << p.m_age << endl;
}

//通过全局函数 打印person信息
template<class T1, class T2>
class person
{
	//全局函数 类内实现
	friend void printperson1(person<T1, T2> p)
	{
		cout << "类内实现:" << endl;
		cout << "姓名:" << p.m_name << endl;
		cout << "年龄:" << p.m_age << endl;
	}

	//全局函数 类外实现
	friend void printperson2<>(person<T1, T2> p);//加空模板参数列表

public:
	person(T1 name, T2 age)
	{
		this->m_name = name;
		this->m_age = age;
	}
private:
	T1 m_name;
	T2 m_age;
};

void test()
{
	person<string, int>p1("张三", 18);
	printperson1(p1);

	person<string, int>p2("李四", 20);
	printperson1(p2);
}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值