模板类
#include<bits/stdc++.h>
using namespace std;
template<typename T>
class People{
public:
People(T name):name_(name){}
protected:
T name_;
};
class A:public People<string>{
public:
A(string name): People(name){}
void print(){
std::cout<<name_<<std::endl;
}
};
class B:public People<int>{
public:
B(int name):People(name){}
void print(){
std::cout<<name_<<std::endl;
}
};
int main(){
A a("aaa");
B b(100);
a.print();
b.print();
}
模板函数
#include<bits/stdc++.h>
using namespace std;
template<typename T>
void Swap(T &a,T &b){
T c=a;
a=b;
b=c;
}
int main(){
int a=1,b=2;
Swap(a,b);
cout<<a<<" "<<b<<endl;
string c="111",d="222";
Swap(c,d);
cout<<c<<" "<<d<<endl;
}