MyString.h
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>
using namespace std;
class MyString
{
friend ostream& operator<< (ostream & out, MyString& str);
friend istream& operator>>(istream& in, MyString& str);
public:
MyString(const char *);
MyString(const MyString&);
~MyString();
char& operator[](int index);
MyString& operator=(const char * str);
MyString& operator=(const MyString& str);
MyString operator+(const char * str );
MyString operator+(const MyString& str);
bool operator== (const char * str);
bool operator== (const MyString& str);
private:
char * pString;
int m_Size;
};
MyString.cpp
#include "MyString.h"
ostream& operator<< (ostream & out, MyString& str)
{
out << str.pString;
return out;
}
istream& operator>>(istream& in, MyString& str)
{
if (str.pString != NULL)
{
delete[] str.pString;
str.pString = NULL;
}
char buf[1024];
in >> buf;
str.pString = new char[strlen(buf) + 1];
strcpy(str.pString, buf);
str.m_Size = strlen(buf);
return in;
}
MyString::MyString(const char * str)
{
this->pString = new char[strlen(str) + 1];
strcpy(this->pString, str);
this->m_Size = strlen(str);
}
MyString::MyString(const MyString& str)
{
this->pString = new char[strlen(str.pString) + 1];
strcpy(this->pString, str.pString);
this->m_Size = str.m_Size;
}
MyString::~MyString()
{
if (this->pString!=NULL)
{
delete[]this->pString;
this->pString = NULL;
}
}
char& MyString::operator[](int index)
{
return this->pString[index];
}
MyString& MyString::operator=(const char * str)
{
if (this->pString != NULL){
delete[] this->pString;
this->pString = NULL;
}
this->pString = new char[strlen(str) + 1];
strcpy(this->pString, str);
this->m_Size = strlen(str);
return *this;
}
MyString& MyString::operator=(const MyString& str)
{
if (this->pString != NULL){
delete[] this->pString;
this->pString = NULL;
}
this->pString = new char[strlen(str.pString) + 1];
strcpy(this->pString, str.pString);
this->m_Size = str.m_Size;
return *this;
}
MyString MyString::operator+(const char * str)
{
int newsize = this->m_Size + strlen(str) + 1;
char *temp = new char[newsize];
memset(temp, 0, newsize);
strcat(temp, this->pString);
strcat(temp, str);
MyString newstring(temp);
delete[] temp;
return newstring;
}
MyString MyString::operator+(const MyString& str)
{
int newsize = this->m_Size + str.m_Size + 1;
char *temp = new char[newsize];
memset(temp, 0, newsize);
strcat(temp, this->pString);
strcat(temp, str.pString);
MyString newstring(temp);
delete[] temp;
return newstring;
}
bool MyString::operator==(const char * str)
{
if (strcmp(this->pString, str) == 0 && strlen(str) == this->m_Size){
return true;
}
return false;
}
bool MyString::operator==(const MyString& str)
{
if (strcmp(this->pString, str.pString) == 0 && str.m_Size == this->m_Size){
return true;
}
return false;
}
TestMyString.cpp
void test01()
{
MyString str("hello World");
cout << str << endl;
cout << "MyString的第一个字符为:" << str[0] << endl;
MyString str2 = "^_^";
MyString str3 = "";
str3 = "aaaa";
str3 = str2;
cout << "str2 = " << str2 << endl;
cout << "str3 = " << str3 << endl;
MyString str4 = "我爱";
MyString str5 = "北京";
MyString str6 = str4 + str5;
MyString str7 = str6 + "天安门";
cout << str7 << endl;
if (str6 == str7)
{
cout << "s6 与 s7相等" << endl;
}
else
{
cout << "s6 与 s7不相等" << endl;
}
}