简易版:
定义:
class String
{
public:
String(const char *str = NULL); // 通用构造函数
String(const String &another); // 拷贝构造函数
~ String(); // 析构函数
String & operater =(const String &rhs); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
实现:
String::String(const char *str)
{
if ( str == NULL ) //strlen 在参数为 NULL 时会抛异常才会有这步判断
{
m_data = new char[1] ;
m_data[0] = ‘\0’ ;
}
else
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}
}
String::String(const String &another)
{
m_data = new char[strlen(another.m_data) + 1];
strcpy(m_data,other.m_data);
}
String& String::operator =(const String &rhs)
{
if ( this == &rhs)
return *this ;
delete []m_data; //删除原来的数据,新开一块内存
m_data = new char[strlen(rhs.m_data) + 1];
strcpy(m_data,rhs.m_data);
return this ;
}
String::~String()
{
delete []m_data ;
}
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;
}