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

正文內(nèi)容

[理學(xué)]cppt第十三章(已修改)

2024-10-28 20:22 本頁(yè)面
 

【正文】 1 程序設(shè)計(jì)初步 ——C++程序設(shè)計(jì) 教 師:文艷軍 : 電 話: 13787414975 國(guó)防科大計(jì)算機(jī)學(xué)院 602室 第十三章 運(yùn)算符重載 3 NUDT, Computer School, 602, 講授內(nèi)容 1. 運(yùn)算符重載的概念 2. 運(yùn)算符成員函數(shù)與友元函數(shù) 3. 單目運(yùn)算符重載 4. 重載流插入和流提取運(yùn)算符 5. 一般雙目運(yùn)算符重載 6. 賦值運(yùn)算符重載 4 NUDT, Computer School, 602, 運(yùn)算符重載的概念 (1/2) ? 運(yùn)算符可以看作為一種特殊的函數(shù) ? 運(yùn)算符重載類似于 函數(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ù)類的定義 ifndef COMPLEX1_H define COMPLEX1_H class Complex {public: Complex(double = , double = )。 Complex operator+(const Complexamp。) const。 Complex operator(const Complexamp。) const。 Complexamp。 operator=(const Complexamp。)。 void print() const。 private: double real。 // real part double imag。 // imaginary part }。 endif 例 1 :復(fù)數(shù)的+、-、=運(yùn)算 (2/5) //文件 2: -- 復(fù)數(shù)類的成員函數(shù)定義 include include Complex::Complex(double r, double i) { real = r。 imag = i。 } Complex Complex::operator+( const Complex amp。 operand2) const { Complex sum。 = real + 。 = imag + 。 return sum。 } 7 NUDT, Computer School, 602, 例 1 :復(fù)數(shù)的+、-、=運(yùn)算 (3/5) Complex Complex::operator( const Complex amp。 operand2) const { Complex diff。 = real 。 = imag 。 return diff。 } Complexamp。 Complex::operator=( const Complex amp。 right) { real = 。 imag = 。 return *this。 } void Complex::print( ) const { cout 39。(39。 real , imag 39。)39。 } 8 NUDT, Computer School, 602, 例 1 :復(fù)數(shù)的+、-、=運(yùn)算 (4/5) //文件 3: --主函數(shù)定義 include include main() { Complex x, y(, ), z(, )。 cout x: 。 ()。 cout \ny: 。 ()。 cout \nz: 。 ()。 x = y + z。 cout \n\nx = y + z:\n。 9 NUDT, Computer School, 602, 例 1 :復(fù)數(shù)的+、-、=運(yùn)算 (5/5) ()。 cout = 。 ()。 cout + 。 ()。 x = y z。 cout \n\nx = y z:\n。 ()。 cout = 。 ()。 cout 。 ()。 cout 39。\n39。 return 0。 } x: (0, 0) y: (, ) z: (, ) x = y + z: (, ) = (, ) + (, ) x = y z: (1, ) = (, ) (, ) 10 NUDT, Computer School, 602, 運(yùn)算符重載的概念 (2/2) ? 什么情況下需要考慮運(yùn)算符重載? ? 當(dāng)需要 把自定義類的對(duì)象用作運(yùn)算符的操作數(shù)時(shí) (只有賦值運(yùn)算符 = 和地址運(yùn)算符 amp。 可直接用于對(duì)象的操作) ? 運(yùn)算符重載函數(shù)如何定義? ? 保持運(yùn)算符的原有屬性,作為類的 成員函數(shù)或 友元函數(shù) 、作為 一般函數(shù) 11 NUDT, Computer School, 602, 運(yùn)算符重載的限制 ? C++的所有運(yùn)算符都可以被重載嗎? . .* :: ?: sizeof 不能 ? 必須作用于自定義類型 ? 不能改變運(yùn)算符作用于內(nèi)部類型時(shí)的含義 ? etc 12 NUDT, Computer School, 602, 運(yùn)算符成員函數(shù)與友元函數(shù) ? 運(yùn)算符重載函數(shù)可定義為類的 成員函數(shù) ? 當(dāng)該成員函數(shù) 不帶參數(shù) 時(shí),支持以該類對(duì)象為唯一操作數(shù)的運(yùn)算(一元運(yùn)算) ? 當(dāng)該成員函數(shù) 帶一個(gè)參數(shù) 時(shí),支持以該類對(duì)象為左操作數(shù)、以所帶參數(shù)類型的數(shù)據(jù)為右操作數(shù)的運(yùn)算(二元運(yùn)算) 13 NUDT, Computer School, 602, 運(yùn)算符成員函數(shù)與友元函數(shù) ? 運(yùn)算符的重載函數(shù)定義為類的 友元函數(shù) ? 當(dāng)該函數(shù) 帶一個(gè)參數(shù) 時(shí),支持以該參數(shù)類型的對(duì)象為唯一操作數(shù)的運(yùn)算 ? 當(dāng)該函數(shù) 帶兩個(gè)參數(shù) 時(shí),支持以第一個(gè)參數(shù)類型的數(shù)據(jù)為左操作數(shù)、以第二個(gè)參數(shù)類型的數(shù)據(jù)為右操作數(shù)的運(yùn)算 14 NUDT, Computer School, 602, 例 1 :復(fù)數(shù)的+運(yùn)算(用友元函數(shù)實(shí)現(xiàn)) //文件 1: -- 復(fù)數(shù)類的定義 ifndef COMPLEX1_H define COMPLEX1_H class Comple
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
公安備案圖鄂ICP備17016276號(hào)-1