編寫類String的建構函式 析構函式和賦值函式

2021-08-30 01:31:45 字數 698 閱讀 6792

這個在面試或筆試的時候常問到或考到。

已知類string的原型為:

class string

;

請編寫string的上述4個函式。

//普通建構函式

string::string(const char *str)

else

} // string的析構函式

string::~string(void)

//拷貝建構函式

string::string(const string &other)    // 得分點:輸入引數為const型

//賦值函式

string & string::operate =(const string &other) // 得分點:輸入引數為const

[b]剖析:[/b]

能夠準確無誤地編寫出string類的建構函式、拷貝建構函式、賦值函式和析構函式的面試者至少已經具備了c++基本功的60%以上!在這個類中包括了指標類成員變數m_data,當類中包括指標類成員變數時,一定要過載其拷貝建構函式、賦值函式和析構函式,這既是對c++程式設計師的基本要求,也是《effective c++》中特別強調的條款。仔細學習這個類,特別注意加注釋的得分點和加分點的意義,這樣就具備了60%以上的c++基本功!

編寫類String的建構函式 析構函式和賦值函式

編寫類string的建構函式 析構函式和賦值函式,已知類string的原型為 class string public string const char str null 普通建構函式 string const string other 拷貝建構函式 string void 析構函式 string ...

編寫類String的建構函式 析構函式和賦值函式

已知類string的原型為 classstring 請編寫string的上述4個函式 普通建構函式 string string const char str else string的析構函式 string string void 拷貝建構函式 string string conststring ot...

編寫類String的建構函式 析構函式和賦值函式

01.include 02.include 03.04.05.06.char strcopy char pdest,const char psrc 07.18.pdest null 19.return ptmp 20.21.22.int strlength const char str 23.30....