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

正文內(nèi)容

清華c語言程序設(shè)計:第13章運算符重載-資料下載頁

2025-10-09 17:14本頁面
  

【正文】 )。 private: char *str。 }。 endif 例 6:定義類 String, 重載運算符 +和 =(2/6) //文件 , 類 String的實現(xiàn) include include String::String(char *m) { str = new char[strlen(m)+1]。 strcpy(str,m)。 } String::~String() { delete [] str。 } //定義運算符 重載函數(shù) ostream amp。 operator(ostream amp。output, String amp。s) { output。 return output。 } 例 6:定義類 String, 重載運算符 +和 =(3/6) //續(xù)文件 //定義運算符 重載函數(shù) istream amp。 operator(istream amp。input, String amp。s) { char temp[1000]。 cintemp。 delete [] 。 = new char[strlen(temp)+1]。 strcpy(,temp)。 return input。 } 例 6:定義類 String, 重載運算符 +和 =(4/6) String amp。String::operator=(String amp。s) { if (amp。s == this) //檢查是否自我賦值 , 非常重要 return *this。 delete []str。 //釋放當前對象的數(shù)據(jù)空間 str = new char[strlen()+1]。 //重新分配適當大小的空間 strcpy(str, )。 return *this。 } 例 6:定義類 String, 重載運算符 +和 =(5/6) String amp。String::operator+(String amp。s) { static String res。 //為什么要聲明 res為靜態(tài)對象 char temp[4096]。 strcpy(temp,str)。 strcat(temp,)。 delete [] 。 = new char[strlen(temp)+1]。 strcpy(,temp)。 return res。 } 例 6:定義類 String, 重載運算符 +和 =(6/6) //文件 , 測試重載的運算符 include include main() { String s1,s2。 cout Please input two strings: endl。 cin s1 s2。 cout Output is: endl。 cout s1 s1 endl。 cout s2 s 2 endl。 s1 = s1 + s2。 cout after s1 = s1 + s2。 endl。 cout s1 s1 endl。 return 0。 } 程序執(zhí)行結(jié)果: Please input two strings: Wuhan Changsha Output is: s1 Wuhan s2 Changsha after s1 = s1 + s2。 s1 WuhanChangsha 類型之間的轉(zhuǎn)換 (1/4) ? 自定義類的對象和其它類型之間也可以實現(xiàn)隱式或顯式的轉(zhuǎn)換 ? 類似提升和類型強制轉(zhuǎn)換 ? 兩種類型轉(zhuǎn)換方式 ? 其它類型的數(shù)據(jù)到自定義類的對象的轉(zhuǎn)換 ? 通過轉(zhuǎn)換構(gòu)造函數(shù) ? 自定義類的對象到其它類型的數(shù)據(jù)的轉(zhuǎn)換 ? 通過類型轉(zhuǎn)換運算符函數(shù) 類型之間的轉(zhuǎn)換 (2/4) ? 類型轉(zhuǎn)換運算符函數(shù) ? 必須是類的非靜態(tài)成員函數(shù),不能是友元函數(shù) ? 沒有參數(shù)(操作數(shù)是什么?) ? 不能指定返回類型(其實已經(jīng)指定了) 類型之間的轉(zhuǎn)換 (3/4) ? 類型轉(zhuǎn)換運算符函數(shù)名: operator + 類型名 ? 例子: operator int ( ) const。 將類的對象轉(zhuǎn)換成一個 int類型的數(shù)據(jù) ? 如果 obj是該自定義類的一個對象,則: (int)obj。 等價于: int( )。 該函數(shù)根據(jù)對象 obj生成一個 int類型的數(shù)據(jù) ? 需要的時候,編譯器會自動調(diào)用類型轉(zhuǎn)換運算符函數(shù)來建立一個臨時對象 ? 就像對內(nèi)部類型使用提升規(guī)則一樣 類型之間的轉(zhuǎn)換 (4/4) ? 轉(zhuǎn)換構(gòu)造函數(shù) ? 只有一個參數(shù),就是其它類型的對象 ? 根據(jù)該對象初始化自定義類型的對象 ? 轉(zhuǎn)換構(gòu)造函數(shù)例子 String(const char *m=)。 String(const int len)。 學習目的檢測 ? 了解運算符重載的概念和意義 ? 掌握運算符重載的限制 ? 掌握流插入和流提取運算符的重載方法 ? 掌握以成員函數(shù)的方式重載運算符的方法 ? 掌握以友元函數(shù)的方式重載運算符的方法 ? 了解運算符成員函數(shù)和友元函數(shù)的區(qū)別 ? 掌握賦值運算符重載的方法 ? 了解類型轉(zhuǎn)換運算符的重載方法 作業(yè)
點擊復制文檔內(nèi)容
教學課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1