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

正文內(nèi)容

c語(yǔ)言程序設(shè)計(jì)(下)ppt-展示頁(yè)

2024-10-26 00:38本頁(yè)面
  

【正文】 。 str = new char[strlen(temp)+1]。 strcat(temp,)。s) { char temp[4096]。 } 例 5:定義類(lèi) String, 重載運(yùn)算符 +=, x+=y將 y中的字符串連接到 x的后面 (4/5) //定義運(yùn)算符 +=重載函數(shù) String amp。 strcpy(,temp)。 delete [] 。s) { char temp[1000]。 operator(istream amp。 return output。output, String amp。 } //定義運(yùn)算符 重載函數(shù) ostream amp。 strcpy(str,m)。 }。s)。 String amp。 public: String(char *m=)。input, String amp。 friend istream amp。output, String amp。 } 程序執(zhí)行結(jié)果: Please input two strings: Wuhan Changsha Output is: s1 Wuhan s2 Changsha 雙目運(yùn)算符重載 (1/2) ? 雙目運(yùn)算符的重載形式,考慮 ? 運(yùn)算符對(duì)左操作數(shù)是否有要求 ? 是否要保留運(yùn)算符的可交換性(只針對(duì)具有可交換性的運(yùn)算符而言) ? 雙目運(yùn)算符重載為非靜態(tài)成員函數(shù) ? 帶有一個(gè)參數(shù) ? 左操作數(shù)必須為該類(lèi)的對(duì)象或?qū)ο蟮囊? ? 雙目運(yùn)算符重載為帶有兩個(gè)參數(shù)的非成員函數(shù) ? 參數(shù)之一必須是類(lèi)的對(duì)象或?qū)ο蟮囊? 例 5:定義類(lèi) String, 重載運(yùn)算符 +=, x+=y將 y中的字符串連接到 x的后面 (1/5) //文件 if !defined __STRING__H__ define __STRING__H__ include class String { friend ostream amp。 cout s1 s1 endl s2 s2 endl。 cin s1 s2。 } 例 4:定義字符串類(lèi) String, 并重載流插入運(yùn)算符 和流提取運(yùn)算符 ,實(shí)現(xiàn)用 cout和 cin直接輸出和輸入類(lèi) String的對(duì)象 (4/4) //文件 include include main() { String s1,s2。 strcpy(,temp)。 delete [] 。s) { char temp[1000]。 operator(istream amp。 return output。output, String amp。 } 例 4:定義字符串類(lèi) String, 并重載流插入運(yùn)算符 和流提取運(yùn)算符 ,實(shí)現(xiàn)用 cout和 cin直接輸出和輸入類(lèi) String的對(duì)象 (3/4) //定義運(yùn)算符 重載函數(shù) ostream amp。 strcpy(str,m)。 }。 ~String()。s)。 operator(istream amp。s)。 operator(ostream amp。 cout s endl。 b++轉(zhuǎn)換為函數(shù)調(diào)用 operator++(b, 0) 重載流插入和流提取運(yùn)算符 (1/2) ? cout + 可以輸出預(yù)定義類(lèi)型的對(duì)象 如: cout string。 operator++(C amp。 operator++(int)。)。 ? 也可為 C的友元函數(shù),原型為 C amp。 } 程序執(zhí)行結(jié)果: s1 is NULL! s2 is not NULL! 單目運(yùn)算符重載 (2/3) ? 特殊的單目運(yùn)算符 ++、 以 ++ 為例( 同理 ) , 設(shè) b為類(lèi) C的對(duì)象 ? 前自增,如: ++b ? 重載函數(shù)可為 C的成員函數(shù),原型為 C amp。 else couts2 is not NULL!endl。 else couts1 is not NULL!endl。 } 例 3:定義字符串類(lèi) String, 并以友元函數(shù)的方式重載運(yùn)算符 ! 以判斷對(duì)象中的字符串是否為空串 (3/3) //文件 include include main() { String s1, s2(some string)。s)//實(shí)現(xiàn)運(yùn)算符重載友元函數(shù) { if (strlen() == 0) return true。 } String::~String() { delete [] str。 endif 例 3:定義字符串類(lèi) String, 并以友元函數(shù)的方式重載運(yùn)算符 ! 以判斷對(duì)象中的字符串是否為空串 (2/3) //文件 include include String::String(char *m) { str = new char[strlen(m)+1]。 private: char *str。 public: String(char *m=)。 } 程序執(zhí)行結(jié)果: s1 is NULL! s2 is not NULL! 例 3:定義字符串類(lèi) String, 并以友元函數(shù)的方式重載運(yùn)算符 ! 以判斷對(duì)象中的字符串是否為空串 (1/3) //文件 if !defined __STRING__H__ define __STRING__H__ include class String { //定義運(yùn)算符重載友元函數(shù)原型 friend bool operator !(String amp。 else couts2 is not NULL!endl。 else couts1 is not NULL!endl。 } 例 2:定義字符串類(lèi) String, 并以成員函數(shù)的方式重載運(yùn)算符 ! 以判斷對(duì)象中的字符串是否為空串 (3/3) //文件 include include main() { String s1, s2(some string)。 } bool String::operator !() //實(shí)現(xiàn)運(yùn)算符重載函數(shù) { if (strlen(str) == 0) return true。 strcpy(str,m)。 }。 //定義運(yùn)算符重載成員函數(shù)原型 bool operator !()。 可直接用于對(duì)象 ? 運(yùn)算符重載函數(shù)如何定義? ? 作為類(lèi)的成員函數(shù)或友元函數(shù)、作為一般函數(shù)(很少用) ? ( ) [ ] = 的重載函數(shù)必須是類(lèi)的成員函數(shù) 運(yùn)算符成員函數(shù)與友元函數(shù) (1/2) ? 以成員函數(shù)的方式重載運(yùn)算符 ? 單目運(yùn)算符:不帶參數(shù),該類(lèi)對(duì)象為唯一操作數(shù) ? 雙目運(yùn)算符:帶一個(gè)參數(shù),該類(lèi)對(duì)象為左操作數(shù)、參數(shù)為右操作數(shù) ? 以友元函數(shù)的方式重載運(yùn)算符 ? 單目運(yùn)算符:帶一個(gè)參數(shù),該參數(shù)為唯一操作數(shù),是自定義類(lèi)的對(duì)象 ? 雙目運(yùn)算符:帶兩個(gè)參數(shù),第一個(gè)參數(shù)為左操作數(shù)、第二個(gè)參數(shù)為右操作數(shù),至少有一個(gè)參數(shù)為自定義類(lèi)的對(duì)象 運(yùn)算符成員函數(shù)與友元函數(shù) (2/2) ? 如果雙目運(yùn)算符的左操作數(shù)不是自定義類(lèi)的對(duì)象,重載函數(shù)不能定義為成員函數(shù) ? 流插入運(yùn)算符 的重載 ? 運(yùn)算符可交換性的實(shí)現(xiàn)(兩個(gè)不同類(lèi)型的操作數(shù)) ? 運(yùn)算符重載函數(shù)的定義要保證運(yùn)算無(wú)二義性 單目運(yùn)算符重載 (1/3) ? 操作數(shù)是自定義類(lèi)的對(duì)象或?qū)ο蟮囊? ? 作為成員函數(shù)重載 ? 沒(méi)有參數(shù) ? 作為友元函數(shù)重載 ? 參數(shù)為自定義類(lèi)的對(duì)象或?qū)ο蟮囊? 例 2:定義字符串類(lèi) String, 并以成員函數(shù)的方式重載運(yùn)算符 ! 以判斷對(duì)象中的字符串是否為空串 (1/3) //文件 if !defined __STRING__H__ define __STRING__H__ include class String { public: String(char *m=)。 return 0。 cout 39。 cout 。 cout = 。 cout \n\nx = y z:\n。 ()。 ()。 例 1 :復(fù)數(shù)的+、-、=運(yùn)算 (5/5) ()。 x = y + z。 cout \nz: 。 cout \ny: 。 cout x: 。)39。(39。 return *this。right) { real = 。 } Complexamp。 =imaginary 。operand2) const { Complex diff。 return sum。 = real + 。 } Complex Complex::operator+(const Complex amp。 endif 例 1 :復(fù)數(shù)的+、-、=運(yùn)算 (2/5) //文件 2: -- 復(fù)數(shù)類(lèi)的成員函數(shù)定義 include include Complex::Complex(double r, double i) { real = r。 // real part double imaginary。 void print() const。 operator=(const Complexamp。) const。) const。C++語(yǔ)言程序設(shè)計(jì) (下 ) 第 1章 計(jì)算機(jī)與程序設(shè)計(jì) 第 2章 數(shù)據(jù)類(lèi)型、運(yùn)算符與表達(dá)式 第 3章 輸入和輸出 第 04章 控制結(jié)構(gòu) 第 5章 函數(shù) 第 6章 數(shù)組 第 7章 指針 第 9章 鏈表 第 10章 面向?qū)ο蟪绦蛟O(shè)計(jì)基本概念 第 11章 類(lèi)與對(duì)象 第 12章 類(lèi)和對(duì)象的使用 第 13章 運(yùn)算符重載 第 14章 繼承 第 15章 多態(tài)性 第 16章 文件和流 第 17章 異常 第 18章 模板 第 13章 運(yùn)算符重載 講授內(nèi)容 ? 運(yùn)算符重載的概念 ? 以成員函數(shù)的方式重載運(yùn)算符 ? 以友元函數(shù)的方式重載運(yùn)算符 ? 流插入和流提取運(yùn)算符的重載 ? 一般單目和雙目運(yùn)算符的重載 ? 賦值運(yùn)算符重載 ? 類(lèi)型轉(zhuǎn)換運(yùn)算符重載 運(yùn)算符重載的概念 (1/2) ? 類(lèi)似于函數(shù)重載 ? 把傳統(tǒng)的運(yùn)算符用于用戶自定義的對(duì)象 ? 直觀自然,可以提高程序的可讀性 ? 體現(xiàn)了 C++的可擴(kuò)充性 ? 通過(guò)定義名為 operator 運(yùn)算符 的函數(shù)來(lái)實(shí)現(xiàn)運(yùn)算符重載 例 1 :復(fù)數(shù)的+、-、=運(yùn)算 (1/5) // 文件 1: -- 復(fù)數(shù)類(lèi)的定義 ifndef COMPLEX1_H define COMPLEX1_H class Complex { public: Complex(double = , double = )。 Complex operator+(const Complexamp。 Complex operator(const Complexamp。 Complexamp。)。 private: double real。 // imaginary part }。 imaginary = i。operand2) const { Complex sum。 =imaginary + 。 } 例 1 :復(fù)數(shù)的+、-、=運(yùn)算 (3/5) Complex Complex::operator(const Complex amp。 = real 。 return diff。 Complex::operator=(const Complex amp。 imaginary = 。 // enables concatenation } void Complex::print() const { cout39。real , imaginary 39。 } 例 1 :復(fù)數(shù)的+、-、=運(yùn)算 (4/5) //文件 3: -- 主函數(shù)定義 include include main() { Complex x, y(, ), z(, )。 ()。 ()。 ()。 cout \n\nx = y +
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1