詳解C++ 編寫String 的構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)
詳解C++ 編寫String 的構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)
編寫類String 的構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù),已知類String 的原型為:
class String { public: String(const char *str = NULL); // 普通構(gòu)造函數(shù) String(const String &other); // 拷貝構(gòu)造函數(shù) ~ String(void); // 析構(gòu)函數(shù) String & operate =(const String &other); // 賦值函數(shù) private: char *m_data; // 用于保存字符串 };
#include <iostream> class String { public: String(const char *str=NULL);//普通構(gòu)造函數(shù) String(const String &str);//拷貝構(gòu)造函數(shù) String & operator =(const String &str);//賦值函數(shù) ~String();//析構(gòu)函數(shù) protected: private: char* m_data;//用于保存字符串 }; //普通構(gòu)造函數(shù) String::String(const char *str) { if (str==NULL) { m_data=new char[1]; //對空字符串自動申請存放結(jié)束標(biāo)志'\0'的空間 if (m_data==NULL) {//內(nèi)存是否申請成功 std::cout<<"申請內(nèi)存失敗!"<<std::endl; exit(1); } m_data[0]='\0'; } else { int length=strlen(str); m_data=new char[length+1]; if (m_data==NULL) {//內(nèi)存是否申請成功 std::cout<<"申請內(nèi)存失??!"<<std::endl; exit(1); } strcpy(m_data,str); } } //拷貝構(gòu)造函數(shù) String::String(const String &other) { //輸入?yún)?shù)為const型 int length=strlen(other.m_data); m_data=new char[length+1]; if (m_data==NULL) {//內(nèi)存是否申請成功 std::cout<<"申請內(nèi)存失??!"<<std::endl; exit(1); } strcpy(m_data,other.m_data); } //賦值函數(shù) String& String::operator =(const String &other) {//輸入?yún)?shù)為const型 if (this == &other) //檢查自賦值 { return *this; } delete [] m_data;//釋放原來的內(nèi)存資源 int length=strlen(other.m_data); m_data= new char[length+1]; if (m_data==NULL) {//內(nèi)存是否申請成功 std::cout<<"申請內(nèi)存失??!"<<std::endl; exit(1); } strcpy(m_data,other.m_data); return *this;//返回本對象的引用 } //析構(gòu)函數(shù) String::~String() { delete [] m_data; } void main() { String a; String b("abc"); system("pause"); }
以上就是C++ 編寫String 的構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)的實(shí)例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:C語言數(shù)據(jù)結(jié)構(gòu)之使用鏈表模擬棧的實(shí)例
欄 目:C語言
下一篇:C++ 中String 替換指定字符串的實(shí)例詳解
本文標(biāo)題:詳解C++ 編寫String 的構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1225.html
您可能感興趣的文章
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 01-10求子數(shù)組最大和的解決方法詳解
- 01-10深入二叉樹兩個結(jié)點(diǎn)的最低共同父結(jié)點(diǎn)的詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)- 解析最少換車次數(shù)的問題詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10HDOJ 1443 約瑟夫環(huán)的最新應(yīng)用分析詳解
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10如何查看進(jìn)程實(shí)際的內(nèi)存占用情況詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置