【C++】String類中的運算子多載,string運算子
模組化設計:
標頭檔:
<span style="font-size:18px;">
#ifndef operator_operator_h#define operator_operator_h#include <iostream> #include <string> 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;
</span>
功能函數:
<span style="font-size:18px;">
#include "operator.h"//預設建構函式,初始化為空白串 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];strcpy_s(p, strlen(str) + 1, str);}//拷貝建構函式,用另外一個string初始化 MyString::MyString(const MyString& str){len = str.len;p = new char[strlen(str.p) + 4];strcpy_s(p, strlen(str.p) + 1, str.p);}//解構函式 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];strcpy_s(p, len + 1, str.p);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];strcpy_s(str.p, str.len + 1, str1.p);strcat_s(str.p, str.len + 1, str2.p);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];}</span>
測試程式:
<span style="font-size:18px;"> #include "operator.h"int main(){MyString mystr("asfasgdhf"); //測試建構函式,用一個字串初始化 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 << "Input a series characters, end of ctrl+z." << endl;cin >> mystr4; //測試重載的輸入符號( >> ) cout << mystr4 << endl;return 0;}</span>
運行結果: