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

正文內(nèi)容

程序設(shè)計(jì)實(shí)習(xí)運(yùn)算符重載(編輯修改稿)

2024-11-22 13:32 本頁(yè)面
 

【文章內(nèi)容簡(jiǎn)介】 // string length char *sPtr。 // pointer to start of string }。 26 運(yùn)算符重載為友員函數(shù) ? 實(shí)現(xiàn)單目運(yùn)算: op operand ? operand是類(lèi) A的對(duì)象 ? op應(yīng)該重載為 A的友員函數(shù),該函數(shù)有一個(gè)參數(shù) ? friend return_type operator op(A arg) ? return_type是 op operand的類(lèi)型 ? 例如: ! string_s,等價(jià)于 operator!(string_s) class String { friend bool operator!(const String amp。)。// is String empty? public: String( const char * = )。 // conversion/default ctor ~String()。 // destructor private: int length。 // string length char *sPtr。 // pointer to start of string }。 bool operator!(const String amp。s) { return == 0。} 27 運(yùn)算符重載為友員函數(shù) ? 實(shí)現(xiàn)雙目運(yùn)算: operand_1 op operand_2 ? op被重載為 A的友員函數(shù),該函數(shù)有兩個(gè)參數(shù) ? friend return_type operator op(argT1 arg1, argT2 arg2 ) ? return_type是 operand_1 op operand_2的類(lèi)型 ? argT1是 operand_1的類(lèi)型 ? argT2是 operand_2的類(lèi)型 ? operand_1和 operand_2中 至少有一個(gè) 是類(lèi)型為 A的對(duì)象 ? 如果 operand_1是類(lèi) A的對(duì)象,則 argT1為 A ? 在函數(shù) operator op(argT1 arg1, argT2 arg2 )的函數(shù)體中,可以訪問(wèn)類(lèi)arg1的任何數(shù)據(jù)成員 ? 如果 operand_2是類(lèi) A的對(duì)象,則 argT2為 A ? 在函數(shù) operator op(argT1 arg1, argT2 arg2 )的函數(shù)體中,可以訪問(wèn)類(lèi)arg2的任何數(shù)據(jù)成員 ? 將雙目運(yùn)算符 op重載為類(lèi) A的友員函數(shù)時(shí),可以:以非類(lèi) A的對(duì)象作為 op所在表達(dá)式的左操作數(shù) 28 流輸入輸出運(yùn)算符的重載 cout 5 “this”。 為什么能夠成立? cout是什么? “ ” 為什么能用在 cout上? 29 cout 是在 iostream中定義的, ostream 類(lèi)的對(duì)象 “ ” 能用在 cout 上是因?yàn)? 在 “ ” 進(jìn)行了重載 考慮 ,怎么重載才能使得 cout 5。 和 cout “this”都能成立? 流輸入輸出運(yùn)算符的重載 30 void operator( const ostream amp。 o, int n){ Output( n)。 } 假定 Output 是一個(gè)能將整數(shù)n輸出到屏幕上的函數(shù),至于其內(nèi)部怎么實(shí)現(xiàn),不必深究 void operator( const ostream amp。 o, const char * s){ Output(s)。 } 流輸入輸出運(yùn)算符的重載 31 怎么重載才能使得 cout 5 “this” 。 能成立? 流輸入輸出運(yùn)算符的重載 32 只需要將重載運(yùn)算符的返回值設(shè)為引用 ostream amp。 operator( const ostream amp。 o, int n){ Output( n)。 return o。 } ostream amp。 operator( const ostream amp。 o, const char * s) { Output(s)。 return o。 } 流輸入輸出運(yùn)算符的重載 33 引用 ? 某個(gè)變量的引用,和這個(gè)變量是一回事,相當(dāng)于該變量的一個(gè)別名。 void swap( int amp。 a, int amp。 b) { int tmp。 tmp = a。 a = b。 b = tmp。 } int n1, n2。 swap(n1,n2) 。 // n1,n2的值被交換 34 引用 ? 函數(shù)的返回值可以是引用 ,如: include iostream using namespace std。 int n = 4。 int amp。 SetValue() { return n。 } int main() { SetValue() = 40。 cout n。 return 0。 } 該程序輸出結(jié)果是 40 35 const 引用類(lèi)型參數(shù) class Complex。 Complex Add ( Complex c1, Complex c2)。 Complex Add ( const Complex amp。 c1, const Complex amp。 c2)。 ? const 代表參數(shù)的值在函數(shù)內(nèi)部不能被修改 ? 兩種函數(shù)定義方式,后者比前者好在哪里呢? ? 后者的好處是避免了生成參數(shù)對(duì)象的過(guò)程,節(jié)省時(shí)間,空間開(kāi)銷(xiāo)。 36 對(duì)象指針和對(duì)象引用作函數(shù)的參數(shù) ? 對(duì)象作為函數(shù)的參數(shù) ? 值傳遞,但需進(jìn)行對(duì)象副本的拷貝 ? 對(duì)象指針作函數(shù)的參數(shù) ? 使用對(duì)象指針作為函數(shù)參數(shù)要比使用對(duì)象作函數(shù)參數(shù)更普遍一些,有如下兩點(diǎn)好處: (1) 實(shí)現(xiàn)傳址調(diào)用??稍诒徽{(diào)用函數(shù)中改變調(diào)用函數(shù)的參數(shù)對(duì)象的值,實(shí)現(xiàn)函數(shù)之間的信息傳遞。 (2) 使用對(duì)象指針實(shí)參僅將對(duì)象的地址值傳給形參,而不進(jìn)行副本的拷貝,這樣可以提高運(yùn)行效率,減少時(shí)空開(kāi)銷(xiāo)。 ? 當(dāng)形參是指向?qū)ο笾羔槙r(shí),調(diào)用函數(shù)的對(duì)應(yīng)實(shí)參應(yīng)該是 某個(gè) 對(duì)象的地址值 ,一般使用 amp。后加對(duì)象名 。 include iostream using namespace std。 class M{ public: M() { x=y=0。 } M(int i, int j) { x=i。 y=j。 } void copy(M *m)。 void setxy(int i, int j) { x=i。 y=j。 } void print() { coutx,yendl。 } private: int x, y。 }。 void M::copy(M *m){ x=mx。 y=my。 } void fun(M m1, M *m2)。 int main() { M p(5, 7), q。 (amp。p)。 fun(p, amp。q)。 ()。 ()。 return 0。 } void fun(M m1, M *m2){ (12, 15)。 m2setxy(22,25)。 } 輸出結(jié)果為: 5,7 22,25 38 對(duì)象指針和對(duì)象引用作函數(shù)的參數(shù) ? 對(duì)象引用作函數(shù)參數(shù) ? 在實(shí)際中,使用對(duì)象引用作函數(shù)參數(shù)要比使用對(duì)象指針作函數(shù)更普遍 ? 使用對(duì)象引用作函數(shù)參數(shù)具有用對(duì)象指針作函數(shù)參數(shù)的優(yōu)點(diǎn) ? 用對(duì)象引用作函數(shù)參數(shù)將更簡(jiǎn)單,更直接。 include iostream using namespace std。 class M { public: M() { x=y=0。 } M(int i, int j) { x=i。 y=j。 } void copy(M amp。m)。 void setxy(int i, int j) { x=i。 y=j。 } void print() {coutx,yendl。 } private: int x, y。 }。 void M::copy(M amp。m){ x=。 x=。 } void fun(M m1, M amp。m2)。 int main(){ M p(5, 7), q。 (p)。 fun(p, q)。 ()。 ()。 return 0。 } void fun(M m1, M amp。m2){ (12, 15)。 (22, 25)。 } 40 假定下面程序輸出為 5, 請(qǐng)問(wèn)該補(bǔ)寫(xiě)些什么? include ios
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
備案圖片鄂ICP備17016276號(hào)-1