【c++】运算符重载练习

来源:互联网 发布:plotagraph同类软件 编辑:程序博客网 时间:2024/06/10 08:48

利用运算符重载实现自己的MyString

MyString.h

#pragma once#include <iostream>using namespace std;class MyString{public://构造和析构MyString();MyString(const char* p);MyString(const MyString& obj);~MyString();//普通成员函数const char* c_str() const;int length();//运算符<<  重载 友元函数friend ostream& operator<<(ostream& cout, MyString& obj);friend void operator>>(const char* str, MyString& obj);//运算符=,【】重载MyString& operator=(const MyString& obj);MyString& operator=(const char* p);char& operator[](int index);//+=  +重载MyString& operator+=(MyString& str);MyString& operator+=(const char* s);MyString operator+(MyString& obj);MyString operator+(const char* str);//==   !=重载bool operator==(const char* p);bool operator==(MyString& obj);bool operator!=(const char* p);bool operator!=(MyString& obj);private:char* pAddress;int mLength;};

MyString.cpp

#define _CRT_SECURE_NO_WARNINGS#include "MyString.h"//构造 析构MyString::MyString(){mLength = 0;pAddress = new char[1];pAddress[0] = '\0';}MyString::MyString(const char* p){mLength = strlen(p);pAddress = new char[mLength + 1];strcpy(pAddress, p);}MyString::MyString(const MyString& obj){mLength = obj.mLength;pAddress = new char[mLength + 1];strcpy(pAddress, obj.pAddress);}MyString::~MyString(){if (pAddress == NULL)delete[] pAddress;}const char* MyString::c_str() const{return this->pAddress;}int MyString::length(){return this->mLength;}//运算符<<重载 友元全局ostream& operator<<(ostream& out, MyString& obj){out << obj.pAddress;return out;}void operator>>(const char* str, MyString& obj){if (obj.pAddress != NULL){delete[] obj.pAddress;}obj.pAddress = new char[strlen(str) + 1];for (int i = 0; i < strlen(str) + 1; i++){obj.pAddress[i] = '\0';}strcpy(obj.pAddress, str);}//=,[]重载MyString& MyString::operator=(const MyString& obj){if (pAddress==NULL){delete[] pAddress;pAddress = NULL;}mLength = obj.mLength;pAddress = new char[mLength + 1];strcpy(pAddress, obj.pAddress);return *this;}char& MyString::operator[](int index){return pAddress[index];}//重载+=、+MyString& MyString::operator+=(MyString& str){//判断追加的字符串是否为空if (str.mLength == 0){return *this;}//计算两个字符串总长this->mLength = this->mLength + str.mLength;//申请两个字符串长度的空间char* pTemp = new char[this->mLength + 1];//初始化数组for (int i = 0; i < this->mLength + 1; i++){pTemp[i] = '\0';}//拷贝两个字符串到新空间中char* p = pTemp;strcat(p, this->pAddress);strcat(p, str.pAddress);//释放旧空间if (this->pAddress != NULL){delete[] this->pAddress;this->pAddress = NULL;}//更新pAddress指针this->pAddress = pTemp;return *this;}MyString& MyString::operator+=(const char* s){//判断追加的字符串是否为空if (s == NULL || strlen(s) == 0){return *this;}//计算两个字符串总长this->mLength = this->mLength + strlen(s);//申请两个字符串长度的空间char* pTemp = new char[this->mLength + 1];//初始化数组for (int i = 0; i < this->mLength + 1; i++){pTemp[0] = '\0';}//拷贝两个字符串到新空间中strcat(pTemp, this->pAddress);strcat(pTemp, s);//释放旧空间if (this->pAddress != NULL){delete[] this->pAddress;this->pAddress = NULL;}//更新指针this->pAddress = pTemp;return *this;}//+重载MyString MyString::operator+(MyString& str){if (str.mLength == 0){return *this;}MyString tempString;tempString.mLength = this->mLength + str.mLength;tempString.pAddress = new char[tempString.mLength + 1];//初始化数组for (int i = 0; i < tempString.mLength + 1; i++){tempString.pAddress[i] = '\0';}strcat(tempString.pAddress, this->pAddress);strcat(tempString.pAddress, str.pAddress);return tempString;}MyString MyString::operator+(const char* str){if (str == NULL || strlen(str) == 0){return *this;}MyString tempString;tempString.mLength = this->mLength + strlen(str);tempString.pAddress = new char[tempString.mLength + 1];for (int i = 0; i < tempString.mLength + 1; i++){tempString.pAddress[i] = '\0';}strcat(tempString.pAddress, this->pAddress);strcat(tempString.pAddress, str);return tempString;}// ==, != 重载bool MyString::operator==(const char* p){if (p == NULL){return false;}if (strcmp(this->pAddress, p) == 0){return true;}return false;}bool MyString::operator!=(const char* p){if (p == NULL){return false;}if (strcmp(this->pAddress, p) != 0){return true;}return false;}bool MyString::operator==(MyString& obj){if (strcmp(this->pAddress, obj.pAddress) == 0){return true;}return false;}bool MyString::operator!=(MyString& obj){if (strcmp(this->pAddress, obj.pAddress) != 0){return true;}return false;}

text.cpp

#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include "MyString.h"using namespace std;void test01(){MyString str1("bbb");MyString str2("aaa");str1 += str2;cout << "str1:" << str1 << endl;str1 += "hello world!";cout << "str1:" << str1 << endl;}//2. 测试+void test02(){MyString str1("bbb");MyString str2("aaa");MyString str3 = str1 + str2; //有问题cout << "str1:" << str1 << endl;cout << "str2:" << str2 << endl;cout << "str3:" << str3 << endl;cout << "-----------------" << endl;MyString str4 = str1 + "hello world!";cout << "str1:" << str1 << endl;cout << "str4:" << str4 << endl;}//3. 测试=、[]void test03(){MyString str1("bbb");MyString str2("aaa");cout << "str1:" << str1 << endl;cout << "str2:" << str2 << endl;cout << "---------------" << endl;str1 = str2;cout << "str1:" << str1 << endl;cout << "str2:" << str2 << endl;cout << "---------------" << endl;cout << "[]:";for (int i = 0; i < str1.length(); i++){cout << str1[i];}cout << endl;}//4. 测试==、!=void test04(){MyString str1("bbb");MyString str2("aaa");if (str1 != str2){cout << "不相等!" << endl;}if (str1 != "ccc"){cout << "不相等!" << endl;}str2 = str1;if (str1 == str2){cout << "相等!" << endl;}if (str1 == "bbb"){cout << "相等!" << endl;}}//5. 拷贝构造、=void test05(){MyString str1("bbb");MyString str2 = str1;MyString str3(str1);cout << "str1:" << str1 << endl;cout << "str2:" << str2 << endl;cout << "str3:" << str3 << endl;}//6. 右移运算符void test06(){MyString str;"hello world" >> str;cout << "str:" << str << endl;}int main(){test01();system("pause");return EXIT_SUCCESS;}




原创粉丝点击