您的位置:首页 > 编程语言 > C语言/C++

C++字符串重载运算符函数(剑指offer面试题1)

2018-03-15 20:11 267 查看
//剑指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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: