代码实现(附带注释)
#include<iostream>
#include<string>
using namespace std;
//定义模板类的基本格式
template<class T1,class T2>//定义两个形参类型,针对不同的问题根据需求定义个数。
//定义类模板
class T {
private:
//两个数据类型
T1 t1; T2 t2;
public:
T(T1 nt1, T2 nt2)
{
t1 = nt1; t2 = nt2;
}
void print() { cout << t1 << " " << t2 << endl; }
};
int main()
{
int age;
double score, wage;
string title;
cin >> age >> score >> wage >> title;
//模板类的实例化!!!
//实例化学生类
T<int, double>student(age, score);
//实例化老师类
T<double, string>teacher(wage, title);
student.print();
teacher.print();
}