C++字串過載運算子函式(劍指offer面試題1)
阿新 • • 發佈:2019-02-18
//劍指offer面試題1:過載運算子函式
//題目:如下為型別CMyString的宣告,請為該型別新增多種運算子函式。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
class CMyString
{
friend ostream& operator<<(ostream &out, const CMyString &str);//輸出流過載函式
friend istream& operator>>(istream &in, CMyString &str);//輸入流過載函式
public:
CMyString(char* pData = nullptr); //有參建構函式
CMyString(const CMyString& str); //拷貝建構函式
~CMyString(void); //解構函式
CMyString& operator = (const CMyString &str);//過載賦值運算子函式
CMyString& operator += (const CMyString &str);//過載+=運算子函式
CMyString& operator + (const CMyString &str);//過載+運算子函式
void show();
private:
char* m_pData;
};
ostream &operator<<(ostream &out, const CMyString &str)
{
out << str.m_pData << endl;
return out;
}
istream &operator>>(istream &in, CMyString &str)
{
in >> str.m_pData;
return in;
}
CMyString::CMyString(char *pData)
{
if (pData == nullptr)
{
m_pData = new char[1];
m_pData[0] = '\0'; //'\0'表示空字元,作為字串結束符使用
}
else
{
m_pData = new char[strlen(pData) + 1];
strcpy(m_pData, pData);
}
}
CMyString::CMyString(const CMyString &str)
{
m_pData = new char[strlen(str.m_pData) + 1];
m_pData = str.m_pData;
}
CMyString::~CMyString()
{
delete[] m_pData;
}
CMyString& CMyString::operator = (const CMyString &str)
{
//判斷傳入的引數和當前的例項(*this)是不是同一個例項
if (this == &str)
return *this;
//分配新記憶體之前釋放自身已有的記憶體,防止程式出現記憶體洩漏
delete[]m_pData;
m_pData = nullptr;
m_pData = new char[strlen(str.m_pData) + 1];
strcpy(m_pData, str.m_pData);
return *this;
}
CMyString& CMyString::operator += (const CMyString &str)
{
char *str1; //臨時的字串存放指標
if (str.m_pData == nullptr)
return *this;
else
{
str1 = new char[strlen(m_pData) + strlen(str.m_pData) + 1];
strcpy(str1, m_pData);
strcat(str1, str.m_pData);
}
delete[]m_pData;
m_pData = str1;
return *this;
}
CMyString& CMyString::operator + (const CMyString &str)
{
char *temp;//臨時的字串存放指標
if (str.m_pData == nullptr)
return *this;
else
{
temp = new char[strlen(m_pData) + strlen(str.m_pData) + 1];
strcpy(temp, m_pData);
strcat(temp, str.m_pData);
}
delete[]m_pData;
m_pData = temp;
return *this;
}
//===============測試程式碼========================//
void CMyString::show()
{
cout << m_pData << endl;
}
//賦值過載函式測試
void Test1()
{
cout << "******************Test1(賦值)*****************" << endl;
char *text = "Hello world";
CMyString str1(text);
CMyString str2;
str2 = str1;
cout << "The expected result is:" << text << endl;
cout << "The actual result is:";
str2.show();
cout << endl;
}
//加法過載函式測試
void Test2()
{
cout << "*********************Test2(加法)******************" << endl;
char *text1 = "shuang";
char *text2 = "is a beautiful girl!";
CMyString str1(text1);
CMyString str2(text2);
CMyString str;
cout << "The expected result is:" << text1 << " " << text2 << endl;
str1 += str2;
cout << "str1 += str2:";
str1.show();
CMyString str3 = "hai";
CMyString str4 = "Merry";
str = str3 + str4;
cout << "str3 + str4:" << str << endl;
//str3.show();
}
//輸入輸出流測試
void Test3()
{
CMyString str1;
CMyString str2;
cin >> str1 >> str2;
cout << "str1:" << str1 << endl;
cout << "str2:" << str2 << endl;
}
int main(int argc, char *argv[])
{
Test1();
Test2();
Test3();
return 0;
}