C++模板類複習
//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;
}
相關文章
- C/C++複習C++
- C++關於DLL匯出模板類和模板函式C++函式
- C++複習考點C++
- C++複習筆記C++筆記
- 【C++複習】棧-上篇C++
- 7.6-----複習C++C++
- c++函式模板和類别範本C++函式
- c++模板類的使用,編譯的問題C++編譯
- C++——模板C++
- C++ 模板C++
- 【C++ 泛型程式設計01:模板】函式模板與類别範本C++泛型程式設計函式
- C++學習 2.5 string類C++
- C++類將函式模板宣告為友元 例項C++函式
- [複習] 種類並查集並查集
- C++複習整理---i++和++iC++
- C++期末複習資料 備考C++
- C++學習筆記 — STL標準模板庫C++筆記
- C++學習 類定義(一)C++
- JavaScript 複習之各類事件(一)JavaScript事件
- JavaScript 複習之各類事件(二)JavaScript事件
- c++ 模板模板引數("Template Template Parameters")C++
- c++函式模板C++函式
- C++模板沉思錄C++
- C++ Primer plus 第12章類和動態記憶體分配複習題參考答案C++記憶體
- C/C++期末考試複習---知識點+習題C++
- C++模板沉思錄(上)C++
- C++開發:template,模板C++
- C++泛型一:模板C++泛型
- C++函式模板案例C++函式
- 【學習記錄】IDEA編輯器 - 類、方法模板配置Idea
- 模板方法模式(c++實現)模式C++
- c++可變模板引數C++
- C++基礎回顧5——類的拷貝、複製和銷燬C++
- C++ 禁用類的複製建構函式和賦值運算子C++函式賦值
- C++學習筆記-----類和建構函式C++筆記函式
- C++ 類 & 物件C++物件
- C++分類C++
- c/c++ 模板 型別推斷C++型別
- C++ 函式過載和模板C++函式