//C++模板类复习
#include<iostream>
using namespace std;
template <class T1, class T2 = string>
class test
{
private:
T1 temp1;
T2 temp2;
public:
test(){}
test(T1 data1, T2 data2):temp1(data1),temp2(data2){}
void show();
};
template <class T1, class T2> //定义模板类成员
void test<T1, T2>::show()
{
cout << "原始类" << endl;
cout << temp1 << endl << temp2 << endl;
}
template <> //显示具体化
class test<int, int>
{
private:
int temp1;
int temp2;
public:
test(){}
test(int one, int two):temp1(one), temp2(two){}
void show()
{
cout << "具体化" << endl;
cout << temp1 << endl << temp2 << endl;
}
};
template <class T>
class test<T, double>
{
private:
T temp1;
double temp2;
public:
test(){}
test(T data1, double data2):temp1(data1), temp2(data2){}
void show()
{
cout << "部分具体化" << endl;
cout << temp1 << endl << temp2 << endl;
}
};
template class test<double, double>; //模板类的显示具体化和显示实例化不会冲突,因为显示具体化不会创建类声明
//将模板类作为内置成员
template <class T1>
class test1
{
private:
template <class T2>
class test2
{
private:
T2 data2;
public:
test2(){}
test2(T2 d):data2(d){}
void show2()
{
cout << data2 << endl;
}
};
test2<T1> data1;
public:
test1(T1 d):data1(d){}
void show1()
{
data1.show2();
}
};
template <template <class T1, class T2> class te>
class test3
{
private:
te<int, int> a;
public:
test3(te<int, int> d):a(d){}
void show1()
{
a.show();
}
};
int main()
{
//test<int> a(1, "linukey");//第二个参数将用默认参数string
//a.show();
//test<double, double> a(5, 6);//隐式实例化
//a.show();
//test<int,int> a(5, 6);//因为有了int,int 的显示具体化,所以此次使用具体化类
//a.show();
//test<int, double> a(1, 2.1);//部分具体化
//a.show();
//test1<int> a(1); //模板类作为类内置成员
//a.show1();
//test<int, int> a(1, 2); //模板类作为参数
//test3<test> b(a);
//b.show1();
return 0;
}
#include<iostream>
using namespace std;
template <class T1, class T2 = string>
class test
{
private:
T1 temp1;
T2 temp2;
public:
test(){}
test(T1 data1, T2 data2):temp1(data1),temp2(data2){}
void show();
};
template <class T1, class T2> //定义模板类成员
void test<T1, T2>::show()
{
cout << "原始类" << endl;
cout << temp1 << endl << temp2 << endl;
}
template <> //显示具体化
class test<int, int>
{
private:
int temp1;
int temp2;
public:
test(){}
test(int one, int two):temp1(one), temp2(two){}
void show()
{
cout << "具体化" << endl;
cout << temp1 << endl << temp2 << endl;
}
};
template <class T>
class test<T, double>
{
private:
T temp1;
double temp2;
public:
test(){}
test(T data1, double data2):temp1(data1), temp2(data2){}
void show()
{
cout << "部分具体化" << endl;
cout << temp1 << endl << temp2 << endl;
}
};
template class test<double, double>; //模板类的显示具体化和显示实例化不会冲突,因为显示具体化不会创建类声明
//将模板类作为内置成员
template <class T1>
class test1
{
private:
template <class T2>
class test2
{
private:
T2 data2;
public:
test2(){}
test2(T2 d):data2(d){}
void show2()
{
cout << data2 << endl;
}
};
test2<T1> data1;
public:
test1(T1 d):data1(d){}
void show1()
{
data1.show2();
}
};
template <template <class T1, class T2> class te>
class test3
{
private:
te<int, int> a;
public:
test3(te<int, int> d):a(d){}
void show1()
{
a.show();
}
};
int main()
{
//test<int> a(1, "linukey");//第二个参数将用默认参数string
//a.show();
//test<double, double> a(5, 6);//隐式实例化
//a.show();
//test<int,int> a(5, 6);//因为有了int,int 的显示具体化,所以此次使用具体化类
//a.show();
//test<int, double> a(1, 2.1);//部分具体化
//a.show();
//test1<int> a(1); //模板类作为类内置成员
//a.show1();
//test<int, int> a(1, 2); //模板类作为参数
//test3<test> b(a);
//b.show1();
return 0;
}