类模板的定义
#include<iostream>
using namespace std;
//类模板
template<class name_type,class age_type>
class Person
{
public:
Person(name_type name,age_type age)
{
this->my_name = name;
this->my_age = age;
}
void show()
{
cout << "name: " << this->my_name << " " << "age = " << this->my_age << endl;
}
public:
name_type my_name;
age_type my_age;
};
void test01()
{
Person<string, int> p1("吴迪", 20);
p1.show();
}
int main()
{
test01();
return 0;
}
类模板与函数模板的区别
#include<iostream>
using namespace std;
//类模板的参数可以设置默认参数
template<class name_type,class age_type = int>
class Person
{
public:
Person(name_type name,age_type age)
{
this->my_name = name;
this->my_age = age;
}
void show()
{
cout << "name: " << this->my_name << " " << "age = " << this->my_age << endl;
}
public:
name_type my_name;
age_type my_age;
};
void test01()
{
Person<string, int> p1("吴迪", 20);
p1.show();
}
void test02()
{
Person <string> p2("吴迪", 20);
p2.show();
}
int main()
{
//test01();
test02();
return 0;
}
类模板中成员函数的调用时机
#include<iostream>
using namespace std;
class Person1
{
public:
void show1()
{
cout << "show person1" << endl;
}
};
class Person2
{
public:
void show2()
{
cout << "show person2" << endl;
}
};
template <class T>
class my_class
{
public:
T obj;
void func1()
{
obj.show1();
}
void func2()
{
obj.show2();
}
};
void test01()
{
my_class<Person2> m;
//m.func1();
m.func2();
}
int main()
{
test01();
return 0;
}
模板对象做函数参数
#include<iostream>
using namespace std;
template <class T1,class T2>
class Person
{
public:
Person(T1 name, T2 age)
{
this->age = age;
this->name = name;
}
void show()
{
cout << "姓名是: " << this->name << " " << "年龄是:" << this->age << endl;
}
T1 name;
T2 age;
};
//1. **指定传入的类型**-- - 直接显示对象的数据类型
void print1(Person<string, int>& p)
{
p.show();
}
void test01()
{
Person<string, int> p("吴迪",20);
print1(p);
}
//2. 参数模板化-- - 将对象中的参数变为模板进行传递
template<class T1,class T2>
void print2(Person<T1,T2>&p)
{
p.show();
cout << "T1的类型是:" << typeid(T1).name() << endl;
cout << "T2的类型是:" << typeid(T2).name() << endl;
}
void test02()
{
Person<string, int> p("杨瑞", 22);
print2(p);
}
//3. 整个类模板化-- - 将这个对象类型 模板化进行传递
template<class T>
void print3(T &p)
{
p.show();
}
void test03()
{
Person<string, int>p("阿迪王", 999);
print3(p);
}
int main()
{
//test01();
//test02();
test03();
return 0;
}
最常用的就是指定传入的类型,其他两类可以但是没有必要,谁会没事找事做呢?是你吗?
类模板与继承
#include <iostream>
using namespace std;
template<class T>
class Base
{
T m;
};
//class Son:public Base错误的继承需要指定类型
class Son :public Base<int>
{
};
void test01()
{
Son c;
}
//类模板继承类模板,可以用T2指定父类中的T类型
template<class T1,class T2>
class Son2 :public Base<T2>
{
public:
//构造函数
Son2()
{
cout << typeid(T1).name() << endl;
cout << typeid(T2).name() << endl;
}
};
void test02()
{
//测试传入的类型
Son2<int, char> child;
}
int main()
{
//test01();
test02();
return 0;
}
类模板成员函数的类外实现
#include <iostream>
using namespace std;
//类模板中的函数类外实现
template<class T1, class T2>
class Person
{
//成员函数类声明
public:
Person(T1 name, T2 age);
void show();
public:
T1 my_name;
T2 my_age;
};
//构造函数 类外实现
template<class T1,class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
this->my_name = name;
this->my_age = age;
}
//成员函数 类外实现
template<class T1,class T2>
void Person<T1, T2>::show()
{
cout << "姓名: " << this->my_name << " " << "年龄 " << this->my_age << endl;
}
void test01()
{
Person<string, int>p("吴迪", 20);
p.show();
}
int main()
{
test01();
return 0;
}