1>.h 文件
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>
#include <string>
using namespace std;
class myString
{
public:
myString();
//myString(int len);
myString(const char *str);
myString(const myString &another);
~myString();
//重载[]
char & operator[](int index);
//重载<<
friend ostream &operator<<(ostream &os, myString&s);
//重载>>
friend istream& operator>>(istream&is, myString &s);
//重载=
myString &operator=(myString &s);
myString &operator=(char* c);
//重载+
myString operator+ (myString &s );
//重载==
bool operator==(myString &s);
//重载!=
bool operator!=(myString &s);
private:
int len;
char *str;
};
2>.cpp文件
#include "myString.h"
myString::myString()
{
this->len = 0;
this->str = NULL;
}
myString::myString(const char *str)
{
if (str == NULL)
{
this->len = 0;
this->str = new char[0 + 1];
strcpy(this->str, "");
}
else
{
this->len = strlen(str);
this->str = new char[this->len + 1];
strcpy(this->str, str);
}
}
myString::myString(const myString &another)
{
this->len = another.len;
this->str = new char[this->len + 1];
strcpy(this->str, another.str);
}
myString::~myString()
{
if (this->str != NULL)
{
cout << "~mystr()....." << endl;
delete this->str;
this->str = NULL;
this->len = 0;
}
}
char & myString::operator[](int index)
{
return this->str[index];
}
ostream & operator<<(ostream &os, myString&s)
{
os << s.str;
return os;
}
istream& operator>>(istream&is, myString &s)
{
if (s.str != NULL)
{
delete[]s.str;
s.str = NULL;
s.len = 0;
}
char str_temp[4096] = { 0 };
is >> str_temp;
s.len = strlen(str_temp);
s.str = new char[s.len + 1];
strcpy(s.str, str_temp);
return is;
}
myString &myString::operator=(myString &str)
{
if (this == &str)
{
return *this;
}
//回收
if (this->str != NULL) {
delete[] this->str;
this->str = NULL;
this->len = 0;
}
// 深拷贝
this->len = str.len;
this->str = new char[this->len + 1];
strcpy(this->str, str.str);
return *this;
}
myString &myString::operator=(char* p)
{
if (this->str != NULL)
{
delete[]this->str;
this->len = 0;
}
this->str = new char[strlen(p) + 1];
strcpy(this->str, p);
return *this;
}
myString myString::operator+ (myString &another)
{
myString temp;
temp.len = this->len + another.len;
temp.str = new char[temp.len + 1];
//清空拼接区
memset(temp.str, 0, temp.len + 1);
strcat(temp.str, this->str);
strcat(temp.str, another.str);
/*
写法2
strcpy(temp.str,this->str);
strcat(temp.str,another.str);
用strcpy清除开辟区,并且搞出来/0;
*/
return temp;
}
bool myString::operator==(myString &s)
{
if (this == &s)
{
return true;
}
if (this->len == s.len && 0 == strcmp(this->str, s.str))
{
return true;
}
return false;
}
bool myString::operator!=(myString &s)
{
if (this == &s)
{
return false;
}
if (this->len != s.len ||0 != strcmp(this->str, s.str))
{
return true;
}
else
{
return false;
}
}
4>text.cpp文件
#include "myString.h"
int main()
{
cout << "~~~~~~~~~~~~~~~~~~" << endl;
myString s1("abc");
myString s3(s1);
myString s2 ="123";
cout << s1<< endl;
cout << s2 << endl;
s1[1] = 'x';
cout << s1 << endl;
s1 = s3;
cout << s1 << endl;
myString s4("abs");
cin >> s4;
cout << s4 << endl;
cout << s1 << endl;
cout << s2 << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << s1 + s2 << endl;
cout << s1 << endl;
cout << s2 << endl;
if (s1 == s2)
{
cout << "哈哈" << endl;
}
cout << "~~~~~~~~~~~~~~~~~~" << endl;
s2 = s1;
if (s1 == s2)
{
cout << "哈哈" << endl;
}
cout << "~~~~~~~~~~~~~~~~~~" << endl;
if (s1 != s2)
{
cout << "xixi" << endl;
}
cout << "~~~~~~~~~~~~~~~~~~" << endl;
s2 = "123";
if (s1 != s2)
{
cout << "xixi" << endl;
}
cout << "~~~~~~~~~~~~~~~~~~" << endl;
return 0;
}