一、MyString.h
#pragma once
#pragma warning(disable:4996)
#include<iostream>
using namespace std;
class MyString
{
friend ostream& operator<<(ostream& out, MyString& str);
friend istream& operator>>(istream& in, MyString& str);
friend MyString operator+(const char* s1, const MyString& s2);
friend bool operator==(const char* str1, const MyString str2);
public:
MyString(const char* str);
MyString(const MyString& str);
~MyString();
int length()
{
return m_Length;
}
MyString operator+(const MyString& str);
MyString operator+(const char* str);
MyString& operator=(const char* str);
MyString& operator=(const MyString& str);
char& operator[](int pos);
bool operator==(const MyString& str);
bool operator==(const char* str);
MyString& operator+=(MyString& str);
MyString& operator+=(const char* str);
private:
char* pString; // 维护在底层的在堆区创建字符数组
int m_Length; //字符串的长度 \0
};
二、MyString.cpp
#include "MyString.h"
#include<iostream>
using namespace std;
ostream& operator<<(ostream& out, MyString& str)
{
out << str.pString;
return out;
}
istream& operator>>(istream& in, MyString& str)
{
//先判断原来是否有数据,如果有先释放
if (str.pString != nullptr)
{
delete[] str.pString;
str.pString = nullptr;
}
char buf[1024];
in >> buf;
str.pString = new char[strlen(buf) + 1];
strcpy(str.pString, buf);
str.m_Length = strlen(buf);
return in;
}
MyString::MyString(const char* str)
{
cout << "构造函数..." << endl;
m_Length = strlen(str);
pString = new char[m_Length + 1];
strcpy(pString, str);
}
MyString::MyString(const MyString& str)
{
cout << "拷贝构造函数..." << endl;
m_Length = str.m_Length;
pString = new char[m_Length + 1];
strcpy(pString, str.pString);
}
MyString::~MyString()
{
cout << "析构函数..." << endl;
delete[] pString;
pString = nullptr;
}
MyString operator+(const char* s1, const MyString& s2)
{
int newLength = strlen(s1)+s2.m_Length + 1;
//创建一个新的字符数组
char* temp = new char[newLength] {};
strcat(temp, s1);
strcat(temp, s2.pString);
MyString newStr = temp;
delete[] temp;
return newStr;
}
MyString MyString::operator+(const MyString& str)
{
int newLength = m_Length + str.m_Length+1;
//创建一个新的字符数组
char* temp = new char[newLength] {};
strcat(temp, pString);
strcat(temp, str.pString);
MyString newStr = temp;
delete[] temp;
return newStr;
}
MyString MyString::operator+(const char* str)
{
int newLength = m_Length + strlen(str) + 1;
//创建一个新的字符数组
char* temp = new char[newLength] {};
strcat(temp, pString);
strcat(temp, str);
MyString newStr = temp;
delete[] temp;
return newStr;
}
MyString& MyString::operator=(const char* str)
{
//判断原来有没有数据,如果有先释放
if (pString != nullptr) {
delete[] pString;
pString = nullptr;
}
m_Length = strlen(str);
pString = new char[m_Length + 1];
strcpy(pString, str);
return *this;
}
MyString& MyString::operator=(const MyString& str)
{
//判断原来有没有数据,如果有先释放
if (pString != nullptr) {
delete[] pString;
pString = nullptr;
}
m_Length = str.m_Length;
pString = new char[m_Length + 1];
strcpy(pString, str.pString);
return *this;
}
char& MyString::operator[](int pos)
{
return pString[pos];
}
bool MyString::operator==(const MyString& str)
{
return strcmp(pString, str.pString)==0;
}
bool MyString::operator==(const char* str)
{
return strcmp(pString, str)==0;
}
bool operator==(const char* str1, const MyString str2)
{
return strcmp(str1, str2.pString)==0;
}
MyString& MyString::operator+=(MyString& str)
{
MyString temp = *this + str;
*this = temp;
return *this;
}
MyString& MyString::operator+=(const char* str)
{
MyString temp = *this + str;
*this = temp;
return *this;
}
三、主函数
#include<iostream>
#include"MyString.h"
using namespace std;
void test1()
{
MyString str = "world";
cout << str << endl;
MyString str1 = str;
cout << str1 << endl;
cout << str1.length() << endl;
/*char buf[1024] = { 0 };
cin >> buf;
cout << buf << endl;*/
cin >> str1;
cout << str1 << endl;
}
void test2()
{
MyString s1 = "abc";
MyString s2 = "def";
MyString s3 = s1 + s2; //s1.operator+(s2);
MyString s4 = s3 + "ghi"; //s3.operator+("ghi);
cout << s3 << endl;
cout << s4 << endl;
MyString s5 = "hello" + s1; //helloabc
cout << s5 << endl;
}
void test3()
{
MyString s1 = "abc";
MyString s2 = "def";
s1 = s2;
s2 = "ghi";
cout << s1 << endl;
cout << s2 << endl;
}
void test4()
{
MyString s1 = "abcdef";
cout << s1[3] << endl;
s1[3] = 'z';
cout << s1 << endl;
}
void test5()
{
MyString s1 = "abc";
MyString s2 = "abcd";
cout << (s1 == s2) << endl;
cout << (s2 == "abcd") << endl;
cout << ("abc" == s1) << endl;
}
void test6()
{
MyString s1 = "abc";
s1 += "def";
cout << s1 << endl;
MyString s2 = "igh";
s1 += s2;
cout << s1 << endl;
cout << s2 << endl;
}
//封装字符串类
int main() {
//test1();
//test2();
//test3();
//test4();
//test5();
test6();
return 0;
}
总结
今天主要给大家介绍了运算符的重载以及c++String的实现。