1.类模板的概念
类模板的作用:
建立一个通用的类,类中的成员和数据类型也不具体指定,用一个虚拟的类型
来表示。
语法如下:
template<class T> //T可以使用其它的代替
类
#include
#include
using namespace std;
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 = " << m_Name << endl;
cout << "Age = " << m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
void test()
{
Person<string, int>p("孙悟空", 1000);
p.showPerson();
}
int main()
{
test();
system("pause");
return 0;
}
2.类模板与函数模板的区别
类模板和函数模板主要有两点区别:
//类模板中模板参数中可以有默认参数
#include
#include
using namespace std;
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 << "Name = " << m_Name << endl;
cout << "Age = " << m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
void test()
{
Person<string>p("孙悟空", 1000);
p.showPerson();
}
int main()
{
test();
system("pause");
return 0;
}
3.类模板中成员函数创建时机
类模板中成员函数与普通类中的成员函数创建时机不同:
//类模板中成员函数的创建时机
#include
using namespace std;
class Person1
{
public:
void showPerson1()
{
cout << "showPerson1" << endl;
}
};
class Person2
{
public:
void showPerson2()
{
cout << "showPerson2" << endl;
}
};
template<class T>
class MyClass
{
public:
T obj;
void func1()
{
obj.showPerson1();
}
void func2()
{
obj.showPerson2();
}
};
void test()
{
MyClass<Person1>p;//类模板只有在调用时才会创建,指定为Person1就只能调用Person1下的函数
p.func1();
}
int main()
{
test();
system("pause");
return 0;
}
4.类模板对象做函数参数
类模板实例化出的对象,向函数传参的方式:
4.1 指定传入类型
//指定传入类型
#include
#include
using namespace std;
template<class T1, class T2>
class Person
{
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
T1 m_Name;
T2 m_Age;
void showPerson()
{
cout << "name = " << m_Name << endl;
cout << "age = " << m_Age << endl;
}
};
void printPerson(Person<string, int>& p)
{
p.showPerson();
}
void test()
{
Person<string, int>p("孙悟空", 18);
printPerson(p);
}
int main()
{
test();
system("pause");
return 0;
}
4.2 参数模板化
//参数模板化
#include
#include
using namespace std;
template<class T1, class T2>
class Person
{
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
T1 m_Name;
T2 m_Age;
void showPerson()
{
cout << "name = " << m_Name << endl;
cout << "age = " << m_Age << endl;
}
};
template<class T1,class T2>
void printPerson(Person<T1, T2>& p)
{
p.showPerson();
}
void test()
{
Person<string, int>p("孙悟空", 188);
printPerson(p);
}
int main()
{
test();
system("pause");
return 0;
}
4.3 整个类模板化
//整个类进行模板化
#include
#include
using namespace std;
template<class T1, class T2>
class Person
{
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
T1 m_Name;
T2 m_Age;
void showPerson()
{
cout << "name = " << m_Name << endl;
cout << "age = " << m_Age << endl;
}
};
template<class T>
void printPerson(T& p)
{
p.showPerson();
}
void test()
{
Person<string, int>p("孙悟空", 518);
printPerson(p);
}
int main()
{
test();
system("pause");
return 0;
}
5.类模板与继承
当类模板遇到继承时,需要注意以下几点:
//类模板和继承
#include
#include
using namespace std;
template<class T>
class Base
{
public:
T m;
};
class Son:public Base //错误,必须指明父类中T的类型比如:class Son: public Base
{
};
int main()
{
system("pause");
return 0;
}
以上代码无法运行,存在错误,作为例子进行说明…………
6.类模板成员函数类外实现
类模板成员函数类外实现:需要加上模板参数列表
//类模板成员函数类外实现
#include
#include
using namespace std;
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 << "姓名: " << m_Name << endl;
cout << endl;
cout << "年龄:" << m_Age << endl;
}
void test()
{
Person<string, int>p("Jack", 18);
p.showPerson();
}
int main()
{
test();
system("pause");
return 0;
}
7.类模板与友元
全局函数类内实现------直接在类内声明友元即可;
全局函数类外实现------需提前让编译器知道全局函数的存在(加上空模板参数列表)
7.1全局函数类内实现
#include
#include
using namespace std;
template<class T1,class T2>
class Person
{
//全局函数类内实现
friend void printPerson(Person<T1, T2> &p)
{
cout << "姓名: " << p.m_name << endl;
cout << endl;
cout << "年龄: " << p.m_age << endl;
}
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>p("jack", 66);
printPerson(p);
}
int main()
{
test();
system("pause");
return 0;
}
7.2全局函数类外实现
//1.全局函数类外实现
#include
#include
using namespace std;
//提前让编译器知道Person类的存在
template<class T1, class T2>
class Person;
//类外实现
template<class T1, class T2>
void printPerson(Person<T1, T2> p)
{
cout << "姓名: " << p.m_name << endl;
cout << endl;
cout << "年龄: " << p.m_age << endl;
}
template<class T1,class T2>
class Person
{
friend void printPerson<>(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>p("Jack", 99);
printPerson(p);
}
int main()
{
test();
system("pause");
return 0;
}