freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

清華大學(xué)c課件-第六講函數(shù)模板與類模板-展示頁(yè)

2025-03-02 14:22本頁(yè)面
  

【正文】 x取得數(shù) 組 大小,并 賦值給 當(dāng)前 對(duì) 象的成 員//為對(duì) 象申 請(qǐng) 內(nèi)存并 進(jìn) 行出 錯(cuò)檢查list = new T[size]。}//拷 貝 構(gòu)造函數(shù)template class T ArrayT::Array(const ArrayT amp。 // 將元素個(gè)數(shù) 賦值給變 量 sizelist = new T [size]。動(dòng)態(tài)數(shù)組類模板程序動(dòng)態(tài)數(shù)組類模板程序動(dòng)態(tài)數(shù)組類模板template class T ArrayT::Array(int sz) //構(gòu)造函數(shù){assert(sz = 0)。 轉(zhuǎn)換 函數(shù)必 須 是成 員 函數(shù),它的聲明不能指定返回 類 型和參數(shù)表。ArrayT表示 類 型參數(shù) 實(shí) 例化之后的Array模板,相當(dāng)于類 名。 //取數(shù) 組 的大小void resize(int sz)。 //重 載 到 T*類 型的 轉(zhuǎn)換operator const T * () const。 operator [] (int i) const。 operator [] (int i)。rhs)。 //析構(gòu)函數(shù)ArrayT amp。a)。 //數(shù) 組 大小(元素個(gè)數(shù))public:Array(int sz = 50)。 模板元 編 程 簡(jiǎn) 介動(dòng)態(tài)數(shù)組類模板程序動(dòng)態(tài)數(shù)組類模板程序動(dòng)態(tài)數(shù)組類模板ifndef ARRAY_Hdefine ARRAY_Hinclude casserttemplate class T //數(shù) 組類 模板定 義class Array {private:T* list。 模板的 實(shí) 例化機(jī)制216。 隊(duì) 列 類 模板216。 鏈 表 類 模板216。 類 模板216。}等效原 則 : “Storeint”相當(dāng)于一個(gè) 類 名等效原 則 : “StoreStudent”相當(dāng)于一個(gè) 類 名等效原 則 : “Storedouble”相當(dāng)于一個(gè) 類 名函數(shù)模板與類模板216。cout () endl。Storedouble d。(g)。Student g = { 1000, 23 }。 (7)。類模板應(yīng)用舉例類模板應(yīng)用舉例類模板int main() {Storeint s1, s2。 item = x。 // 返回 item中存放的數(shù)據(jù) }template class T //存入數(shù)據(jù)函數(shù)的 實(shí)現(xiàn) void StoreT::putElem(const T amp。//使程序完全退出,返回到操作系 統(tǒng) 。StoreT::getElem() {//如 試圖 提取未初始化的數(shù)據(jù), 則終 止程序if (!haveValue) { cout No item present! endl。template class T //缺省構(gòu)造函數(shù)的 實(shí)現(xiàn) StoreT::Store(): haveValue(false) { }StoreT表示 類 型參數(shù) 實(shí) 例化之后的Store模板,相當(dāng)于類 名。 //存入數(shù)據(jù)函數(shù)}。 //提取數(shù)據(jù)函數(shù)void putElem(const T amp。// 缺省形式(無形參)的構(gòu)造函數(shù)T amp。 // item用于存放任意 類 型的數(shù)據(jù)bool haveValue。 //平均分}。// 結(jié) 構(gòu)體 Studentstruct Student { int id。 模板的特化216。 綜 合 實(shí) 例216。 棧類 模板216。 動(dòng)態(tài) 數(shù) 組類 模板216。 函數(shù)模板216。 //調(diào) 用函數(shù)模板return 0。 //調(diào) 用函數(shù)模板cout c array contains: endl。 //調(diào) 用函數(shù)模板cout b array contains: endl。//定 義 char數(shù) 組cout a array contains: endl。//定 義 int數(shù) 組double b[B_COUNT] = { , , , , , , , }。}函數(shù)模板的示例函數(shù)模板的示例函數(shù)模板int main() //主函數(shù){ const int A_COUNT = 8, B_COUNT = 8, C_COUNT = 20。 i++)cout array[i] 。template class T //定 義 函數(shù)模板void outputArray(const T *array, int count) {for (int i = 0。例如, 對(duì) 于 調(diào) 用表達(dá)式 abs(n),由于 實(shí) 參 n為 int型,所以推 導(dǎo) 出模板中 類 型參數(shù) T為 intn 當(dāng) 類 型參數(shù)的含 義 確定后, 編譯 器將以函數(shù)模板 為樣 板,生成一個(gè)函數(shù):int abs(int x) { return x 0 ? –x : x。return 0。cout abs(n) endl。}int main() {int n = 5。 模板元 編 程 簡(jiǎn) 介函數(shù)模板概述函數(shù)模板概述n 函數(shù)模板可以用來 創(chuàng) 建一個(gè)通用功能的函數(shù),以支持多種不同形參, 進(jìn) 一步 簡(jiǎn) 化重 載 函數(shù)的函數(shù)體 設(shè)計(jì)n 定 義 方法:template 模板參數(shù)表 函數(shù)定 義n 模板參數(shù)表的內(nèi)容n 類 型參數(shù): class(或 typename) 標(biāo)識(shí) 符n 常量參數(shù): 類 型 說 明符 標(biāo)識(shí) 符n 模板參數(shù): template 參數(shù)表 class 標(biāo)識(shí) 符函數(shù)模板函數(shù)模板實(shí)例函數(shù)模板實(shí)例函數(shù)模板include iostreamusing namespace std。 模板的 實(shí) 例化機(jī)制216。 隊(duì) 列 類 模板216。 鏈 表 類 模板216。 類 模板216。第六講 函數(shù)模板與類模板函數(shù)模板與類模板216。 函數(shù)模板216。 動(dòng)態(tài) 數(shù) 組類 模板216。 棧類 模板216。 綜 合 實(shí) 例216。 模板的特化216。templatetypename TT abs(T x) {return x 0? x : x。double d = 。cout abs(d) endl。} 運(yùn)行 結(jié) 果:5求絕對(duì)值函數(shù)的模板分析求絕對(duì)值函數(shù)的模板分析n 編譯 器從 調(diào) 用 abs()時(shí)實(shí) 參的 類 型,推 導(dǎo) 出函數(shù)模板的 類 型參數(shù) 。}函數(shù)模板函數(shù)模板的示例函數(shù)模板的示例函數(shù)模板include iostreamusing namespace std。 i count。cout endl。int a[A_COUNT] = { 1, 2, 3, 4, 5, 6, 7, 8 }。//定 義 double數(shù) 組char c[C_COUNT] = Wele to see you! 。outputArray(a, A_COUNT)。outputArray(b, B_COUNT)。outputArray(c, C_COUNT)。}函數(shù)模板的示例函數(shù)模板的示例函數(shù)模板運(yùn)行 結(jié) 果如下:a array contains:1 2 3 4 5 6 7 8b array contains: c array contains:W e l c o m e t o s e e y o u !函數(shù)模板與類模板216。 類 模板216。 鏈 表 類 模板216。 隊(duì) 列 類 模板216。 模板的 實(shí) 例化機(jī)制216。 模板元 編 程 簡(jiǎn) 介類模板概述類模板概述n 類 模板的作用使用 類 模板使用 戶 可以 為類 聲明一種模式,使得 類 中的某些數(shù)據(jù)成 員 、某些成 員 函數(shù)的參數(shù)、某些成 員 函數(shù)的返回 值 ,能取任意 類 型(包括基本 類 型和用 戶 自定 義類 型)類模板類模板的聲明類模板的聲明n 類 模板:template 模板參數(shù)表 class 類 名{類 成 員 聲明 }n 如果需要在 類 模板以外定 義 其成 員 函數(shù),則 要采用以下的形式:template 模板參數(shù)表 類 型名 類 名 模板參數(shù) 標(biāo)識(shí) 符列表 ::函數(shù)名(參數(shù)表)類模板與函數(shù)模板中的 “模板參數(shù)表 ”完全一 樣類模板應(yīng)用舉例類模板應(yīng)用舉例類模板include iostreaminclude cstdlibusing namespace std。 //學(xué)號(hào) float gpa。 類模板應(yīng)用舉例類模板應(yīng)用舉例類模板//類 模板: 實(shí)現(xiàn)對(duì) 任意 類 型數(shù)據(jù) 進(jìn) 行存取template class Tclass Store {private:T item。 // haveValue標(biāo)記 item是否已被存入內(nèi)容public:Store()。getElem()。x)。//以下 實(shí)現(xiàn) 各成 員 函數(shù)。類模板應(yīng)用舉例類模板應(yīng)用舉例類模板template class T //提取數(shù)據(jù)函數(shù)的 實(shí)現(xiàn)T amp。exit(1)。}return item。x) {// 將 haveValue 置 為 true,表示 item中已存入數(shù) 值 haveValue = true。 // 將 x值 存入 item}注意,函數(shù)模板中無法 對(duì) 參數(shù)化 類 型 T進(jìn) 行 “與具體 類 型相關(guān)的 ”處 理。 (3)。cout () () endl。StoreStudent s3。 cout The student id is ().id endl。cout Retrieving object D... 。//由于 d未 經(jīng) 初始化 ,在 執(zhí) 行函數(shù) ()過 程中 導(dǎo) 致程序 終 止return 0。 函數(shù)模板216。 動(dòng)態(tài) 數(shù) 組類 模板216。 棧類 模板216。 綜 合 實(shí) 例216。 模板的特化216。 //用于存放 動(dòng)態(tài) 分配的數(shù) 組 內(nèi)存首地址int size。 //構(gòu)造函數(shù)Array(const ArrayT amp。 //拷 貝 構(gòu)造函數(shù)~Array()。 operator = (const ArrayT amp。 //重 載 =“T amp。 //重 載 []”const T amp。 operator T * ()。int getSize() const。 //修改數(shù) 組 的大小}。賦值 函數(shù) (即拷貝 函數(shù) )針對(duì) 常 對(duì) 象定義 常函數(shù)類 型 轉(zhuǎn)換 重 載 : 在 類 體中通 過 指定關(guān) 鍵 字 operator,并在其后加上 轉(zhuǎn)換 的目 標(biāo)類 型后,就可以聲明 轉(zhuǎn)換 函數(shù)。針對(duì) 常 對(duì) 象定義 常函數(shù)T作 為類 型參數(shù),在模板的定 義 中可以當(dāng)作 類 名使用。//sz為 數(shù) 組 大?。ㄔ貍€(gè)數(shù)), 應(yīng) 當(dāng)非 負(fù)size = sz。 //動(dòng)態(tài) 分配 size個(gè) T類 型的元素空 間}template class T ArrayT::~Array() //析構(gòu)函數(shù){ delete [] list。a) {size = 。 // 動(dòng)態(tài) 分配 n個(gè) T類 型的元素空 間for (int i = 0。 i++) //從 對(duì) 象 X復(fù)制數(shù) 組 元素到本 對(duì) 象 list[i] = [i]。 (思考: 為 什么不是拷貝 構(gòu)造函數(shù)? )針對(duì) 每個(gè)元素都會(huì) 調(diào) 用 T的缺省構(gòu)造函數(shù)動(dòng)態(tài)數(shù)組類模板程序動(dòng)態(tài)數(shù)組類模板程序動(dòng)態(tài)數(shù)組類模板//重 載 =運(yùn)算符,將 對(duì) 象 rhs賦值給 本 對(duì) 象。ArrayT::operator = (const ArrayTamp。rhs != this) {if (size != ) {delete [] list。 //設(shè) 置本 對(duì) 象的數(shù) 組 大小list = new T[size]。 i size。}return *this。為 了 實(shí)現(xiàn)鏈 式表達(dá), 應(yīng)該 返回當(dāng)前 對(duì) 象的引用。ArrayT::operator[] (int n) {assert(n = 0 amp。 n size)。 //返回下 標(biāo)為 n的數(shù) 組 元素}template class Tconst T amp。amp。 //檢查 下 標(biāo) 是否越界return list[n]。 //返回當(dāng)前 對(duì) 象中私有數(shù) 組 的首地址}針對(duì) 常 對(duì) 象定義 常函數(shù)這 是 類 型 轉(zhuǎn)換 重 載 。動(dòng)態(tài)數(shù)組類模板程序動(dòng)態(tài)數(shù)組類模板程序動(dòng)態(tài)數(shù)組類模板template class TArrayT::operator const T * () const {return list。}// 將數(shù) 組 大小修改 為 sztemplate class Tvoid ArrayT::resize(int sz) {assert(sz = 0)。T
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1