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; }
相关文章推荐
- 剑指offer 面试题35扩展:删除字符串中所有重复出现的字符 (C++版)
- C++笔试题(剑指offer 面试题4 替换字符串中的空格)
- 剑指offer-面试题42-翻转单词顺序VS左旋转字符串
- C++找出数组中重复的数字( 剑指offer面试题3-1)
- 【剑指offer】面试题67:把字符串转换成整数
- 剑指Offer:面试题49 把字符串转换成整数
- 剑指offer 面试题14:调整数组顺序使奇数位于偶数前面(C++版)
- 剑指offer——面试题42:(二)字符串的坐旋转操作
- 剑指offer 面试题42 翻转单词顺序 VS 左旋转字符串
- 剑指offer——面试题54:表示数值的字符串
- 剑指Offer面试题:26.字符串的排列
- 剑指offer之面试题42翻转单词顺序VS左旋转字符串
- 剑指offer-第四章解决面试题思路(字符串的排序)
- 【剑指offer】解决面试题的思路 —— 顺时针打印矩阵(C++)
- C++剑指offer_面试题11_数值的整数次方(*)
- C++二维数组中的查找(剑指offer面试题4)
- 剑指offer面试题48 C++写一个不能被继承的类
- 剑指Offer面试题28(java版):字符串的排列
- (C++)剑指offer-49:把字符串转换成整数(综合)
- 【剑指Offer】面试题49:把字符串转化为整数