程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++模板類復習

C++模板類復習

編輯:關於C++
//C++模板類復習
#include
using namespace std;


template
class test
{
private:
T1 temp1;
T2 temp2;
public:
test(){}
test(T1 data1, T2 data2):temp1(data1),temp2(data2){}
void show();
};


template //定義模板類成員
void test::show()
{
cout << "原始類" << endl;
cout << temp1 << endl << temp2 << endl;
}


template <> //顯示具體化
class test
{
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 test
{
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; //模板類的顯示具體化和顯示實例化不會沖突,因為顯示具體化不會創建類聲明


//將模板類作為內置成員
template
class test1
{
private:
template
class test2
{
private:
T2 data2;
public:
test2(){}
test2(T2 d):data2(d){}
void show2()
{
cout << data2 << endl;
}
};
test2 data1;
public:
test1(T1 d):data1(d){}
void show1()
{
data1.show2();
}
};


template
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved