c++String類的運算符重載---21


原創博文,轉載請標明出處--周學偉 http://www.cnblogs.com/zxouxuewei/

 一,創建測試程序包

測試代碼如下:

/* Date: 2017-5-4
 * Description: String operator overload test program
 * Filename: string_operator.h
 * Author: Myron Zhou
 */

#ifndef __STRING_OPERATOR_h_
#define __STRING_OPERATOR_h_  
  
#include <iostream>    
#include <string.h>    
  
using namespace std;  
  
class MyString  
{  
public:  
    //三個重載的構造函數    
    MyString();  
    MyString(const char* str);  
    MyString(const MyString& str);  
    //析構函數    
    ~MyString();  
  
    //重載運算符    
    MyString& operator = (const MyString& str);  
    char&  operator[](int index);                                                   //訪問下標  
    friend ostream& operator << (ostream& out, const MyString& str);                //重載輸出操作符    
    friend istream& operator >> (istream& in, MyString& str);                       //重載輸入操作符    
    friend MyString operator + (const MyString& str1, const MyString& str2);        //重載加號操作符,注意返回引用不行  
    friend MyString operator += (MyString& str1, const MyString& str2);             //重載+=操作符    
    friend bool operator == (const MyString& str1, const MyString& str2);           //重載相等操作符    
    friend bool operator != (const MyString& str1, const MyString& str2);           //重載不相等操作符    
  
private:  
    char* p;  
    int len;  
};


#endif  
/* Date: 2017-5-4
 * Description: String operator overload test program
 * Filename: string_operator.cpp
 * Author: Myron Zhou
 */
#include "string_operator.h" 
using namespace std; 
  
//默認構造函數,初始化為空串    
MyString::MyString()  
{  
    len = 0;  
    p = new char[len + 1];  
    p[0] = '\0';  
}  
//構造函數,用一個字符串初始化    
MyString::MyString(const char* str)  
{  
    len = strlen(str);  
    p = new char[strlen(str) + 1];  
    strncpy(p, str,len+1);  
}  
//拷貝構造函數,用另外一個string初始化    
MyString::MyString(const MyString& str)  
{  
    len = str.len;  
    p = new char[strlen(str.p) + 4];  
    strncpy(p,  str.p, strlen(str.p) + 1);  
}  
//析構函數    
MyString::~MyString()  
{  
    delete[] p;  
}  
//重載賦值操作符( = )    
MyString& MyString::operator = (const MyString& str)  
{  
    if (this->p == str.p)  
    {  
        return *this;  
    }  
    delete[] p;  
    len = str.len;  
    p = new char[len + 1];  
    strncpy(p,  str.p, len + 1);  
    return *this;  
}  
//重載輸出操作符( << )    
ostream& operator << (ostream& out, const MyString& str)  
{  
    out << str.p;  
    return out;  
}  
//重載輸入操作符( >> )    
istream& operator >> (istream& in, MyString& str)  
{  
    in >> str.p;  
    return in;  
  
}  
//重載加號操作符( + )    
MyString operator + (const MyString& str1, const MyString& str2)  
{  
    MyString str;  
    delete[] str.p;  
    str.len = str1.len + str2.len;  
    str.p = new char[str.len + 1];  
    strncpy(str.p,  str1.p, str.len + 1);  
    strncat(str.p,  str2.p, str.len + 1);  
    return str;  
}  
//重載相加賦值操作符( += )    
MyString operator += (MyString& str1, const MyString& str2)  
{  
    str1 = str1 + str2;  
    return str1;  
}  
//重載相等操作符    
bool operator == (const MyString& str1, const MyString& str2)  
{  
    if (strcmp(str1.p, str2.p) == 0)  
    {  
        return true;  
    }  
    return false;  
}  
//重載不相等操作符    
bool operator != (const MyString& str1, const MyString& str2)  
{  
    if (strcmp(str1.p, str2.p) == 0)  
    {  
        return false;  
    }  
    return true;  
}  
  
//重載下標([])  
char&  MyString::operator[](int index)  
{  
    return p[index];  
} 
/* Date: 2017-5-4
 * Description: String operator overload test program
 * Filename: main.cpp
 * Author: Myron Zhou
 */

#include "string_operator.h"  
  
int main()  
{  
    MyString mystr("this is test program!");            //測試構造函數,用一個字符串初始化    
    cout << mystr[2] << endl;  
    mystr[4] = 'd';  
    cout << mystr <<endl;  
    MyString mystr2(mystr);                 //用另外一個string初始化     
    cout << mystr2 << endl;  
    MyString mystr3;   
    mystr3 = mystr + mystr2;                //測試加號運算符,測試賦值運算符      
    cout << mystr + mystr2 << endl;  
    mystr3 += mystr;                        //測試+=運算符    
    cout << mystr3 << endl;  
    cout << (mystr == mystr2) << endl;      //測試 ==    
    cout << (mystr != mystr3) << endl;      //測試 !=    
    MyString mystr4;  
    cout << "good luck!" << endl;  
    cin >> mystr4;                          //測試重載的輸入符號( >> )    
    cout << mystr4 << endl;  
  
    return 0;  
}  

二,編譯運行代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM