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

正文內(nèi)容

[理學]cppt第十三章-展示頁

2024-10-25 20:22本頁面
  

【正文】 計算機學院 602室 第十三章 運算符重載 3 NUDT, Computer School, 602, 講授內(nèi)容 1. 運算符重載的概念 2. 運算符成員函數(shù)與友元函數(shù) 3. 單目運算符重載 4. 重載流插入和流提取運算符 5. 一般雙目運算符重載 6. 賦值運算符重載 4 NUDT, Computer School, 602, 運算符重載的概念 (1/2) ? 運算符可以看作為一種特殊的函數(shù) ? 運算符重載類似于 函數(shù)重載 ? 把傳統(tǒng)的運算符用于用戶自定義的對象 ? 直觀自然,可以提高程序的可讀性 ? 體現(xiàn)了 C++的可擴充性 ? 通過定義名為 operator 運算符 的函數(shù)來實現(xiàn)運算符重載 例 1 :復數(shù)的+、-、=運算 (1/5) //文件 1: -- 復數(shù)類的定義 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 }。 imag = i。 operand2) const { Complex sum。 = imag + 。 } 7 NUDT, Computer School, 602, 例 1 :復數(shù)的+、-、=運算 (3/5) Complex Complex::operator( const Complex amp。 = real 。 return diff。 Complex::operator=( const Complex amp。 imag = 。 } void Complex::print( ) const { cout 39。 real , imag 39。 } 8 NUDT, Computer School, 602, 例 1 :復數(shù)的+、-、=運算 (4/5) //文件 3: --主函數(shù)定義 include include main() { Complex x, y(, ), z(, )。 ()。 ()。 ()。 cout \n\nx = y + z:\n。 cout = 。 cout + 。 x = y z。 ()。 ()。 ()。\n39。 } x: (0, 0) y: (, ) z: (, ) x = y + z: (, ) = (, ) + (, ) x = y z: (1, ) = (, ) (, ) 10 NUDT, Computer School, 602, 運算符重載的概念 (2/2) ? 什么情況下需要考慮運算符重載? ? 當需要 把自定義類的對象用作運算符的操作數(shù)時 (只有賦值運算符 = 和地址運算符 amp。 friend Complex operator+(const Complexamp。) const。) const。 operator=(const Complexamp。 void print() const。 // real part double imag。 endif 15 NUDT, Computer School, 602, 例 1 :復數(shù)的+運算(用友元函數(shù)實現(xiàn)) //文件 2: -- 復數(shù)類的成員函數(shù)定義 include include Complex::Complex(double r, double i) { real = r。 } friend Complex operator+( const Complexamp。 oprd2) { Complex sum。 = + 。 } 16 NUDT, Computer School, 602, 使用成員函數(shù)還是友元函數(shù)? ? 無論使用哪種重載函數(shù),在表達式中使用該運算符的 格式是一樣的 ? 運算符重載函數(shù)的定義要保證運算 無二義性 ? 盡量使用成員函數(shù)進行運算符重載,這樣不會破壞類的封裝和信息的隱藏 17 NUDT, Computer School, 602, 使用成員函數(shù)還是友元函數(shù)? ? 有時必須使用友元函數(shù)來重載: 如果操作符的最左操作數(shù)所屬的類不能修改 ? 最左操作數(shù)屬于 系統(tǒng)預定義類 cout myStack ? 最左操作數(shù)屬于 基本數(shù)據(jù)類型 1 + myStack 18 NUDT, Computer School, 602, 單目運算符重載 (1/3) ? 操作數(shù)是 自定義類的對象 或 對象的引用 ? 作為成員函數(shù)重載 ? 沒有參數(shù) ? 作為友元函數(shù)重載 ? 參數(shù)為自定義類的對象或?qū)ο蟮囊? 19 NUDT, Computer School, 602, 例 2:定義字符串類 String, 并以成員函數(shù)的方式重載運算符 ! 以判斷對象中的字符串是否為空串 (1/3) //文件 if !defined __STRING__H__ define __STRING__H__ include class String {public: String(char *m=)。 //定義運算符重載成員函數(shù)原型 bool operator !( )。 }。 strcpy(str,m)。 } bool String::operator !() //實現(xiàn)重載 { if (strlen(str) == 0) return true。 } 21 NUDT, Computer School, 602, 例 2:定義字符串類 String, 并以成員函數(shù)的方式重載運算符 ! 以判斷對象中的字符串是否為空串 (3/3) //文件 include include main( ) { String s1, s2(some string)。 else
點擊復制文檔內(nèi)容
教學課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1