#include <iostream>
#include <string>
using namespace std;
template<typename NameT, class AgeT>
class Person
{
public:
Person(NameT name, AgeT age)
{
this->name = name;
this->age = age;
}
NameT name;
AgeT age;
void showPerson()
{
cout << "name :" << this->name << endl;
}
};
void test01()
{
Person<string, int> p1("Tom", 10);
p1.showPerson();
}
template<class NameType, class AgeType=int>
class Person2
{
public:
NameType name;
AgeType age;
Person2(NameType name, AgeType age)
{
this->name = name;
this->age = age;
}
void showPerson()
{
cout << "age :" << this->age << endl;
}
};
void test02()
{
Person2<string, int> p3("Tom", 10);
Person2<string> P4("猪八戒", 999);
}
int main(int argc, char** argv)
{
test02();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Person1
{
public:
void showPerson1()
{
cout << "Person1 show" << endl;
}
};
class Person2
{
public:
void showPerson2()
{
cout << "Person2 show" << endl;
}
};
template<class T>
class Myclass
{
public:
T obj;
void func1()
{
obj.showPerson1();
}
void func2()
{
obj.showPerson2();
}
};
void test(){
Myclass<Person1> m;
m.func1();
}
template<class T1, class T2>
class Person01{
public:
Person01(T1 name, T2 age)
{
this ->age = age;
this ->name = name;
}
T1 name;
T2 age;
void showPerson()
{
cout << "姓名:" << this ->name << endl;
}
};
void PrintPerson01(Person01<string, int> &p)
{
p.showPerson();
}
void test02(){
Person01<string, int> p1("suiwuh", 100);
PrintPerson01(p1);
}
template<class T1, class T2>
void PrintPerson2(Person01<T1, T2>p)
{
p.showPerson();
}
void test03(){
Person01<string, int> p1("zhubajie", 99);
PrintPerson01(p1);
}
template<class T>
void PrintPerson3( T &p )
{
p.showPerson();
}
void test04()
{
Person01<string, int> p2("tangseng", 8888);
PrintPerson3(p2);
}
int main()
{
test04();
return 0;
}
#include <string>
#include <iostream>
using namespace std;
template<class T>
class Base
{
T m;
};
class son :public Base<int>
{
};
void test(){
son s1;
}
template<class T , class T1>
class Son2 :public Base<T>
{
public:
Son2()
{
cout << typeid(T1).name() << endl;
cout << typeid(T).name() << endl;
}
T1 obj;
};
void test01(){
Son2<char ,int> s2;
}
template<class T1, class T2>
class Person01{
public:
Person01(T1 name, T2 age);
void showPerson();
T1 name;
T2 age;
};
template<class T1, class T2>
Person01<T1 ,T2>::Person01(T1 name, T2 age)
{
this->name = name;
this->age = age;
}
template<class T1, class T2>
void Person01<T1, T2>::showPerson()
{
cout << "xingming" << this->name << endl;
}
void test02(){
Person01<string, int > P2("dfdf" , 333);
P2.showPerson();
}
int main()
{
test02();
return 0;
}