mustring.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "mystring.h"
#include <string.h>
#include <iostream>
using namespace std;
mystring::mystring(const char* s)
{
if (s == nullptr)
{
_str = new char[1];
*_str = '\0';
}
else
{
_str = new char[strlen(s) + 1];
strcpy(_str, s);
}
}
mystring::~mystring()
{
delete[]_str;
}
mystring & mystring::operator = (const mystring & another)
{
if (this == &another)
{
return *this;
}
delete[]this->_str;
_str = new char[strlen(another._str) + 1];
strcpy(_str, another._str);
return *this;
}
mystring::mystring(const mystring & another)
{
_str = new char[strlen(another._str) + 1];
strcpy(_str, another._str);
}
void mystring::dump()
{
cout << _str << endl;
}
bool mystring::operator ==(const mystring & another)
{
return strcmp(this->_str,another._str) == 0;
}
bool mystring::operator > (const mystring & another)
{
return strcmp(this->_str, another._str) > 0;
}
bool mystring::operator <(const mystring & another)
{
return strcmp(this->_str, another._str) < 0;
}
bool mystring::operator >=(const mystring & another)
{
return strcmp(this->_str, another._str) >= 0;
}
bool mystring::operator <=(const mystring & another)
{
return strcmp(this->_str, another._str) <= 0;
}
mystring mystring::operator +(const mystring & another)
{
int len = strlen(this->_str) + strlen(another._str);
mystring ms;
delete []ms._str;
ms._str = new char[len + 1]{0};
strcat(strcat(ms._str, this->_str), another._str);
return ms;
}
mystring& mystring::operator += (const mystring& another)
{
int catLen = strlen(this->_str);
int srcLen = strlen(another._str);
int len = catLen + srcLen;
this->_str = static_cast<char*>(realloc(this->_str, len + 1));
memset(this->_str + catLen, 0, srcLen + 1);
strcat(this->_str, another._str);
return *this;
}
char mystring:: at(int n)
{
if (n > strlen(_str))
exit(0);
return _str[n];
}
char mystring::operator[](int n)
{
return _str[n];
}
char* mystring::c_str()
{
return _str;
}
mustring.h
#pragma once
class mystring
{
public:
mystring(const char* s = nullptr);
void dump();
~mystring();
mystring & operator = (const mystring & another);
mystring(const mystring & another);
bool operator == (const mystring & another);
bool operator > (const mystring & another);
bool operator < (const mystring & another);
bool operator >= (const mystring& another);
bool operator <= (const mystring& another);
mystring operator + (const mystring& another);
mystring& operator += (const mystring& another);
char at(int n);
char operator[](int n);
char *c_str();
private:
char * _str;
};
main.cpp
#include "mystring.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "string 实现:" << endl;
string s;
string s1("china");
cout << s << endl;
cout << s1 << endl;
string s3(s1);
cout << s3 << endl;
string s4("china");
string s5("victory");
if (s4 == s5)
cout << "==" << endl;
if (s4 >= s5)
cout << ">=" << endl;
if (s4 <= s5)
cout << "<=" << endl;
string s8 = s4 + s5;
cout << s8 << endl;
(s4 += s5) = "world";
cout << s4 << endl;
cout << s4.at(3) << endl;
cout << s4[3] << endl;
cout << s4.operator[](3)<< endl;
cout << s4.c_str() << endl;
cout << "==============================" << endl;
cout << "mystring 实现:" << endl;
mystring ms;
mystring ms1("china");
ms.dump();
ms1.dump();
mystring ms3(ms1);
ms3.dump();
mystring s6("china");
mystring s7("victory");
if (s6 == s7)
cout << "==" << endl;
if (s6 >= s7)
cout << ">=" << endl;
if (s6 <= s7)
cout << "<=" << endl;
mystring s9 = s6 + s7;
s9.dump();
cout << "===================================" << endl;
s6 += s7;
s6.dump();
(s6 += s7) = "abc";
s6.dump();
cout << ms1.at(3) << endl;
cout << ms1.operator[](3) << endl;
cout << ms1.c_str() << endl;
return 0;
}
测试结果为: