模板函数
#include<iostream>
using namespace std;
#include<string>
//普通函数调用的时候可以发生隐式类型转换
// 一个typename的模板函数不行
// 调用的时候指定类型可以发生隐式类型转换
//函数模板 用typename和class关键字都行
template<typename T1,typename T2>
bool compare(T1& p1, T2& p2) {
if (p1==p2)
{
return true;
}
else
{
return false;
}
}
template<typename T1, typename T2>
T1 add(T1& p1, T2& p2) {
return p1 + p2;
}
template<typename T>
T add01(T &p1, T& p2) {
return p1 + p2;
}
int add02(int a, int b) {
return a + b;
}
class Person
{
public:
Person(int age, string name) {
this->age = age;
this->name = name;
};
int age;
string name;
};
//模板重载函数 用模板去重载函数你如果类要用模板去比较就用这个方法
template<> bool compare(Person& p1, Person& p2) {
if (p1.age==p2.age&&p1.name==p2.name)
{
return true;
}
else
{
return false;
}
}
void test() {
char db='c';
int a = 20;
if (compare(a, db)) {
cout << "a=db" << endl;
}
else
{
cout << "a!=db" << endl;
}
}
void test01() {
char db = 'c';
int a = 20;
cout << "普通函数调用" << add02(a, db);
cout <<"定义两个typename" << add(a, db)<<endl;
//cout << add01(a, db);//不会发生隐式类型转换
cout <<"指定类型" << add<int>(a, db)<< endl;//指定类型可以发生隐式类型转换
}
int main() {
Person p1(10, "abc");
Person p2(10, "abc");
if (compare(p1, p2)) {
cout << "p1p2相等" << endl;
};
Person p3(11, "abc");
Person p4(10, "abc");
compare(p3, p4);
if (!compare(p3, p4)) {
cout << "p3p4不相等" << endl;
};
test();
test01();
return 0;
}
模板类
#include<iostream>
#include<string>
using namespace std;
//模板类就是类里面的成员函数的类型不确定相当于给一个虚拟的类型
//
// 类模板与函数模板的区别
//
//模板类没有自动类型推导
//类模板可以使用默认参数类型
//类模板中的成员函数的创建时机
//普通的类的成员函数一开始就可以创建
//类模板中的成员函数在调用的时候才创建
//因为类模板中的成员函数一开始不知道是什么类型只有指定了类型之后才会创建
template<class T1, class T2=int>//直接给他指定为int
class Person
{
public:
Person(T1 name, T2 age)
{
this->age = age;
this->name = name;
};
T1 name;
T2 age;
private:
};
//如何使用类模板 分别创建Person1 Person2类
class Person1
{
public:
void showPerson1() {
cout << "showPerson1" << endl;
}
};
class Person2
{
public:
void showPerson2() {
cout << "showPerson2" << endl;
}
};
template<class T>
class man {
public:
T obj;//创建一个模板对象
void func1(){
obj.showPerson1();
}
void func2() {
obj.showPerson2();
}
};
int main() {
//Person<string,int> p1("adc", 18);
Person<string> p1("adc", 18);
cout << p1.age << p1.name;
man<Person1> man1;
man1.func1();
//man1.func2();
return 0;
}
模板实例化出的对象如何当作参数传递
#include<iostream>
#include<string>
using namespace std;
//模板实例化出的对象,向函数传参的方式
//1指定传入的类型 ---直接显示对象的数据类型
//2参数模板化 ---将对象中的参数变为模板进行传递
//3整个类模板花 ---将这个对象类型 模板化进行传递
template<class T1,class T2>
class Person1 {
public:
Person1 (T1 age,T2 name) {
this->Age = age;
this->Name = name;
}
void ShowPerson1() {
cout << "姓名:"<<this->Name<<"年龄:"<<this->Age << endl;
cout << "Person1 show" << endl;
}
T1 Age;
T2 Name;
};
class Person2 {
public:
void showPerson2() {
cout << "Person2 show" << endl;
}
};
//1指定传入类型 这个最常用
//
//直接吧模板传进去 告诉函数传的是那个模板的对象
void printPerson1(Person1<string, int>&p) {
p.ShowPerson1();
}
void test01() {
Person1<string, int>p("孙悟空", 100);
printPerson1(p);
}
//2参数模板化
template<typename T1, typename T2>
void printPerson2(Person1<T1, T2>& p){
p.ShowPerson1();
}
void test02() {
Person1<string, int>p("猪八戒", 100);
printPerson2(p);
}
//整个类模板化
template<class T>
void printPerson3(T& p) {
p.ShowPerson1();
}
void test03() {
Person1<string, int>p("唐僧", 88);
printPerson3(p);
}
int main() {
test01();
test02();
test03();
system("pause");
return 0;
}
类模板与继承
#include<iostream>
#include<string>
using namespace std;
//必须知道父类中的T类型才能继承给子类
template<class T>
class Base
{
public:
T obj;
};
class son1:public Base<int>//必须知道父类中的T类型才能继承给子类
{
public:
son1() {
cout << "继承类型1" << endl;
}
};
void test01() {
son1 p1;
}
template<class T1,class T2>
class son2 :public Base<T2>//灵活指定父类中的T类型
{
public:
T1 obj;
};
void test02() {
son2<int,char> p2;//int传给T1 char传给T2 而且这个T2是给父类的
}
int main() {
test01();
test02();
return 0;
}
模板类的成员函数类外声明
#include<iostream>
#include<string>
using namespace std;
template<class T1,class T2>
class people
{
public:
people(T1 name,T2 age);
void showPeople(T1 name, T2 age);
private:
};
//向类外的函数说明用的模板
template<class T1, class T2>
people<T1,T2>::people(T1 name, T2 age)
{
}
//注意返回值类型
template<class T1, class T2>
void people<T1, T2>::showPeople(T1 name, T2 age)
{
}
int main() {
return 0;
}