Mystring.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class MyString {
public:
MyString();
//MyString(int len);
MyString(const char *str);
MyString(const MyString& other);//拷貝構造
~MyString();
//重載操作符[]
char & operator[](int index);
//重載操作符==
bool operator==(const MyString& other);
//重載操作符!=
bool operator!=(const MyString& other);
//重載操作符>
bool operator>(MyString& other);
//重載操作符<
bool operator<(MyString& other);
//重載操作符=
MyString& operator=(const MyString &other);
//重載操作符+
MyString operator+(MyString& other);
//重載操作符<<
friend ostream& operator<<(ostream& os, MyString& s);
//重載操作符>>
friend istream& operator>>(istream& is, MyString& s);
private:
int len;
char *str;
};
Mystring.cpp 具體實現
#include "MyString.h"
MyString::MyString()
{
this->len = 0;
this->str = NULL;
}
//MyString(int len);
MyString::MyString(const char *str)
{
if (str == NULL) {
this->len = 0;
this->str = new char[0 + 1];
strcpy(this->str, "");
}
else {
int length = strlen(str);
this->len = length;
this->str = new char[length + 1];
strcpy(this->str, str);
}
}
//拷貝構造在對象初始化賦值時調用,所有沒有垃圾需要清理
MyString::MyString(const MyString& other)
{
this->len = other.len;
this->str = new char[other.len + 1];
strcpy(this->str, other.str);
}
MyString::~MyString()
{
if (this->str != NULL) {
cout << this->str << "執行了析構函數" << endl;
delete this->str;
this->str = NULL;
this->len = 0;
}
}
//重載操作符[]
char& MyString::operator[](int index)
{
return this->str[index];
}
//重載操作符==
bool MyString::operator==(const MyString& other)
{
if (strcmp(this->str, other.str) == 0) {
return true;
}
else {
return false;
}
}
//重載操作符!=
bool MyString::operator!=(const MyString& other)
{
return !(*this == other);
}
//重載>
bool MyString::operator>(MyString& other)
{
int ret = strcmp(this->str, other.str);
if (ret > 0) {
return true;
}
else {
return false;
}
}
//重載>
bool MyString::operator<(MyString& other)
{
int ret = strcmp(this->str, other.str);
if (ret < 0) {
return true;
}
else {
return false;
}
}
//重載操作符=
MyString& MyString::operator=(const MyString &other)
{
//1 如果是自身
if (*this == other)
return *this;
//2 自身垃圾回收
if (this->str != NULL) {
delete[] this->str;
this->str = NULL;
}
//3 賦值
this->len = other.len;
this->str = new char[this->len + 1];
strcpy(this->str, other.str);
return *this;
}
//重載操作符+
//返回匿名對象就可以進行連續+操作
MyString MyString::operator+(MyString& other)
{
MyString temp;
int len = this->len + other.len;
temp.len = len;
temp.str = new char[len + 1];
memset(temp.str, 0, len + 1);
strcat(temp.str, this->str);
strcat(temp.str, other.str);
return temp;
}
/*全局函數*/
//重載操作符<<
ostream& operator<<(ostream& os, MyString& s)
{
os << s.str;
return os;
}
//重載操作符>>
istream& operator>>(istream& is, MyString& s)
{
//1 將s之前的字符串釋放掉
if (s.str != NULL) {
delete[] s.str;
s.str = NULL;
s.len = 0;
}
//2 通過cin添加新的字符串
char temp_str[4096] = { 0 };//設定鍵盤輸入長度,用臨時變量來存儲輸入
cin >> temp_str;
int len = strlen(temp_str);
s.str = new char[len + 1];
strcpy(s.str, temp_str);
s.len = len;
return is;
}
main.cpp
#include "MyString.h"
int main()
{
MyString s1("abc");
MyString s2(s1);
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
s1[0] = 'X';
cout << "s1: " << s1 << endl;
cout << "*******重載=******" << endl;
MyString s3 = "123456";
s1 = s3;
cout << "s1: " << s1 << endl;
cout << "*******重載cin>>******" << endl;
cin >> s3;
cout << "*******重載+******" << endl;
cout << s1;
s3 = s1 + s2;
//cout << s3 + s1;
return 0;
}