强化字符串string类源代码
mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
using namespace std;
class mystring
{
friend ostream& operator<<(ostream &out,mystring &ob);
friend istream& operator>>(istream &in,mystring &ob);
private:
char *str;
int size;
public:
mystring();
mystring(const char *str);
mystring(const mystring &ob);
~mystring();
int getSize(void);
//重载[]运算符返回值必须是左值才能用写操作
char& operator[](int index);
mystring& operator=(const mystring &ob);
mystring& operator=(const char *str);
mystring& operator+(const mystring &ob);
mystring& operator+(const char *str);
bool operator==(const mystring &ob);
bool operator==(const char *str);
bool operator!=(const mystring &ob);
bool operator!=(const char *str);
};
#endif // MYSTRING_H
mystring.cpp
#include "mystring.h"
#include<iostream>
#include<cstring>
using namespace std;
mystring::mystring()
{
this -> str = NULL;
this -> size = 0;
cout << "无参构造" << endl;
}
mystring::mystring(const char *str)
{
cout << "char* 构造函数" << endl;
this->str = new char [strlen(str)+1];
strcpy(this->str,str);
this->size = strlen(str);
}
mystring::mystring(const mystring &ob)
{
cout << "char* 构造函数" << endl;
this->str = new char [strlen(ob.str)+1];
strcpy(this->str,ob.str);
this->size = ob.size;
}
mystring::~mystring()
{
if (this->str != NULL)
{
delete []this->str;
this->str = NULL;
this->size = 0;
}
cout << "析构函数" << endl;
}
ostream& operator<<(ostream &out,mystring &ob)
{
out << ob.str;
return out;
}
istream& operator>>(istream &in,mystring &ob)
{
if(ob.str != NULL)
{
delete [] ob.str;
ob.str = NULL;
}
char buf[1024] = "";//临时buf
in >> buf;
ob.str = new char[strlen(buf)+1];
strcpy(ob.str,buf);
ob.size = strlen(buf);
return in;
}
int mystring::getSize(void)
{
return this->size;
}
char& mystring::operator[](int index)
{
if(index >= 0 && index <= this->size)
{
return this->str[index];
}
else
{
cout << "index无效" << endl;
}
}
mystring& mystring::operator=(const mystring &ob)
{
if(this->str != NULL)
{
delete [] this->str;
this-> str = NULL;
this-> size = 0;
}
this->str = new char[ob.size+1];
strcpy(this->str,ob.str);
this->size = ob.size;
return *this;
}
mystring& mystring::operator=(const char *str)
{
if(this->str != NULL)
{
delete [] this->str;
this-> str = NULL;
this-> size = 0;
}
this->str = new char[strlen(str)+1];
strcpy(this->str,str);
this->size = strlen(str);
return*this;
}
mystring& mystring::operator+(const mystring &ob)
{
int newSize = this->size + ob.size +1;
char *tmp_str = new char [newSize];
//清空tmp_str所指向的空间
memset(tmp_str,0,newSize);
//先将this->str拷贝到tmp_str中,然后ob.str追加到tmp_str中
strcpy(tmp_str,this->str);
strcat(tmp_str,ob.str);
static mystring newString(tmp_str);
//释放tmp_str指向的临时空间
if(tmp_str != NULL)
{
delete [] tmp_str;
tmp_str = NULL;
}
return newString;
}
mystring& mystring::operator+(const char *str)
{
int newSize = this->size + strlen(str) +1;
char *tmp_str = new char [newSize];
//清空tmp_str所指向的空间
memset(tmp_str,0,newSize);
//先将this->str拷贝到tmp_str中,然后str指向的字符串追加到tmp_str中
strcpy(tmp_str,this->str);
strcat(tmp_str,str);
static mystring newString(tmp_str);
//释放tmp_str指向的临时空间
if(tmp_str != NULL)
{
delete [] tmp_str;
tmp_str = NULL;
}
return newString;
}
bool mystring::operator==(const mystring &ob)
{
if((strcmp(this->str,ob.str) == 0) && (this->size == ob.size))
{
return true;
}
return false;
}
bool mystring::operator==(const char *str)
{
if((strcmp(this->str,str) == 0) && (this->size == strlen(str)))
{
return true;
}
return false;
}
bool mystring::operator!=(const mystring &ob)
{
if((strcmp(this->str,ob.str) == 0) && (this->size == ob.size))
{
return false;
}
return true;
}
bool mystring::operator!=(const char *str)
{
if((strcmp(this->str,str) == 0) && (this->size == strlen(str)))
{
return false;
}
return true;
}
main.cpp
#include <iostream>
#include"mystring.h"
using namespace std;
int main()
{
mystring str1("Happy birthday");
mystring str2;
mystring str3;
mystring str4;
str2 = str1;
str3 = " ZY";
cout << "str1: " << str1 << endl;
cout << "str2: " <<str2 << endl;
cout << "str1 和 str2 ";
if (str1 == str2){cout << "相等" <<endl;}
else{cout << "不相等" << endl;}
cout << "将str1 的 " << str1[6] << " 改成大写:" ;
str1[6] = 'B';
cout << str1 << endl;
if (str1 != str2){cout << "不相等" <<endl;}
else{cout << "相等" << endl;}
str4 = str1 + str3;
cout << str4 << endl;
return 0;
}