1.3.2类模板和函数模板的区别
#include
using namespace std;
#include
//类模板没有自动类型推导的使用方式
//类模板在模板参数列表中可以使用默认参数
template<class nametype ,class agetype=int>
class person
{
public:
person(nametype name, agetype age)
{
this->m_name = name;
this->m_age = age;
}
void showperson()
{
cout << this->m_name << endl;
}
nametype m_name;
agetype m_age;
};
//类模板没有自动类型推导的使用方式
void test01()
{
//person p;
person<string,int>p1(“ssss”, 1000);
//只能使用显示指定类型
p1.showperson();
}
//类模板在模板参数列表中可以使用默认参数
void test02()
{
personp2(“zzzz”, 1000);
}
int main()
{
test01();
system("pause");
return 0;
}
1.3.1类模板
#include
using namespace std;
#include
template<class nametype,class agetype>
class person
{
public:
person(nametype name, agetype age)
{
this->m_name=name;
this->m_age = age;
}
void showperson()
{
cout << “name:” << this->m_name << endl;
}
nametype m_name;
agetype m_age;
};
void test01()
{
person<string, int>p1(“ssss”, 999);
p1.showperson();
}
int main()
{
test01();
system("pause");
return 0;
}