數(shù)據(jù)類型轉(zhuǎn)換和運(yùn)算符重載_第1頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、數(shù)據(jù)類型轉(zhuǎn)換和運(yùn)算符重載1.用戶自定義 基本類型 ilude ioseam using namespace std; / class distance private: const float mtf; int feet; float inches; public: distance():feet(0),inches(0.0),mtf(3.28f) distance(float meters):mtf(3.28f) float fi=meters*mtf; feet=int(fi); inches=12*(fi-feet); distance(int ft,float in):feet(ft)

2、,inches(in),mtf(3.28f) vo getdist() cout "enter the feet:"cin feet; cout "enter the inches:" cin inches; void showdist() cout feet "'" "-" inches """ endl; operator float () const float ff=inches/12; ff+=ic_cast float (feet); return ff/mtf;

3、/ int main() float meter; distance dist1=2.35f; /uses 1-arg constructor to /convert meters to distance cout "dist1= "dist1.showdist(); meter=static_cast float (dist1); /uses conversion operator(plicit) /for distance to meters cout "dist1= " meter " meters" endl; distanc

4、e dist2(5,10.25); meter=dist2; /ao uses conversion op(implicit) cout "dist2= " meter " meters" endl; /dist2=meter; /*error: = won't convert return 0; 從上面我們可以看出:基本類型 用戶自定義 用單參數(shù)的構(gòu)造函數(shù)(轉(zhuǎn)換構(gòu)造函數(shù)conversion constructor) 用戶自定義 基本類型 用轉(zhuǎn)換運(yùn)算符(conversion operator) 2.c字符串與string對(duì)象之間的轉(zhuǎn)換 inclu

5、de iostream include string.h using namespace std; / class string private: static const int sz=80; char strsz; public: string() str0='0' stry(str,""); string(char s) sty(str,s); void display() const cout str; operator char* () return str; / int main() string s1; char xstr="niha

6、o,haha!" s1=xstr; /為什么這里又能夠轉(zhuǎn)換了 s1.display(); string s2="bonne annee!" cout static_cast char* (s2); /use conversion operator(explicit) cout endl; return 0; 1)上面注釋:為什么這里能轉(zhuǎn)換,對(duì)照的是上一個(gè)程序中的dist2=meter; 兩者都是從基本類型轉(zhuǎn)化為用戶自定義類型,但是為什么一個(gè)可以一個(gè)不行以呢? 答: 2)其次句注釋的地方告知我們:轉(zhuǎn)換不僅發(fā)生在賦值語(yǔ)句中,在其他適合的場(chǎng)合也會(huì)發(fā)生,如傳遞給運(yùn)算法(如

7、 )或者函數(shù)的參數(shù)中。 3)在這里假如有一句:xstr=s2;(把用戶自定義類型 基本類型)對(duì)不對(duì)呢? 答案是不可的,由于c字符串是一個(gè)數(shù)組,s2為了傳給xstr會(huì)首先調(diào)用轉(zhuǎn)換運(yùn)算符 char*,但是他返回的是 一個(gè)數(shù)組的首地址,在這里我們會(huì)想到數(shù)組是不能以通常的方式(=)給另一個(gè)數(shù)組賦值的。 二、關(guān)鍵字explicit和mutable 1.explicit是用來(lái)防止轉(zhuǎn)換的 阻擋轉(zhuǎn)換運(yùn)算符的執(zhí)行的轉(zhuǎn)換比較簡(jiǎn)單:不定義運(yùn)算符就可以了。不過假如牽扯到構(gòu)造函數(shù),事情就不那么容易了。 例子: /*/ /* explicit.cpp /*/ include iostream using namespac

8、e std; class distance private: const float mtf; int feet; float inches; public: distance():feet(0),inches(0.0),mtf(3.28f) explicit distance(float meters):mtf(3.28f) float fi=meters*mtf; feet=int(fi); inches=12*(fi-feet); void getdist() cout "enter the feet:"cin feet; cout "enter the i

9、nches:" cin inches; void showdist() cout "the distance is:" feet "'" inches """ void functiond(distance ) dd.showdist(); int main() float meters=2.0f; distance d1; distance d2(2.3f); /distance d3=2.3f; /functiond(meters); return 0; 顯示構(gòu)造函數(shù)的副作用就是,不能用法帶有等號(hào)的對(duì)象

10、初始化形式。 在函數(shù)調(diào)用的時(shí)候,也牽扯到一個(gè)從meters到dd轉(zhuǎn)化過程,而此過程是隱式的所以編譯器也會(huì)報(bào)錯(cuò)。2)用法mutable可以轉(zhuǎn)變const對(duì)象的數(shù)據(jù) /*/ /* multab.cpp /*/ include iostream using namespace std; class scrollbar public: scrollbar(int si,string own):size(si),owner(own) void size(int siz) const size=siz; void setowner(string str) /*const*/ owner=str; int getsize() const return size; string getowner() const return owner; private: /*mutable */int size; mutable string owner; int main() const scrollbar ss(122,"windows"); ss.setsize(100); /錯(cuò)誤,const對(duì)象只能調(diào)用const成員函數(shù),由于沒有mutable不能修改 ss.setowner("linux");

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論