mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <string.h>
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();
//有指针成员必须重载=运算符
MyString& operator=(MyString &ob);
MyString& operator=(const char *str);
//获取字符串的长度
int Size(void);
//重载[]运算符
char& operator[](int pos);
//重载+运算符
MyString operator+(const MyString &ob);
MyString operator+(const char *str);
//重载>运算符
bool operator>(const MyString &ob);
bool operator>(const char *str);
};
#endif // MYSTRING_H
mystring.cpp
#include "mystring.h"
MyString::MyString()
{
str=NULL;
size=0;
}
MyString::MyString(const char *str)
{
//获取str的长度
size = strlen(str);
//根据size的长度申请堆区空间
this->str=new char[size+1];
memset(this->str, 0, size+1);
//将str的字符串拷贝到this->str指向堆区空间中
strcpy(this->str, str);
}
MyString::MyString(const MyString &ob)
{
//获取ob的字符串长度
size = ob.size;
//根据size的长度申请堆区空间
str=new char[size+1];
memset(str, 0, size+1);
//将ob.str指向的字符串 拷贝到 str指向的堆区中
strcpy(str, ob.str);
}
MyString::~MyString()
{
if(str != NULL)
{
delete [] str;
str=NULL;
}
}
MyString &MyString::operator=(MyString &ob)
{
//得到大小
size = ob.size;
//如果str有指向 必须先释放str的指向空间
if(str!=NULL)
{
delete [] str;
str=NULL;
}
//重新根据size大小申请空间
str=new char[size+1];
memset(str, 0, size+1);
//将ob.str指向的字符串 拷贝到 str指向的堆区中
strcpy(str, ob.str);
return *this;//完成str3=str2=str1这样的链式操作
}
MyString &MyString::operator=(const char *str)
{
//释放this->str指向旧空间
if(this->str != NULL)
{
delete [] this->str;
this->str=NULL;
}
size = strlen(str);
//根据size的长度申请堆区空间
this->str=new char[size+1];
memset(this->str, 0, size+1);
//将str指向的字符串 拷贝到 this->str指向的堆区中
strcpy(this->str, str);
return *this;
}
int MyString::Size()
{
return size;
}
char& MyString::operator[](int pos)
{
//判断str是否为空
if(str == NULL)
{
cout<<"字符串为空无法访问"<<endl;
exit(-1);//结束进程
}
//pos位置是否合法
if(pos < 0 || pos >=size)
{
cout<<"位置pos:"<<pos<<"不合法"<<endl;
exit(-1);
}
return str[pos];
}
MyString MyString::operator+(const MyString &ob)
{
MyString tmp;
//tmp.size为两个对象的size之和
tmp.size=size+ob.size;
if(tmp.size == 0)
{
tmp.str=NULL;
return tmp;
}
//为tmp.str申请堆区空间
tmp.str = new char[tmp.size+1];
memset(tmp.str,0,tmp.size+1);
//str4+str5
//拷贝str4.str到tmp.str指向的空间中
if(str != NULL)
{
strcpy(tmp.str, str);
}
//将str5.str追加到tmp.str中
if(ob.str != NULL)
{
strcat(tmp.str, ob.str);
}
return tmp;
}
MyString MyString::operator+(const char *str)
{
MyString tmp;
//tmp.size为两个对象的size之和
tmp.size=size+strlen(str);
if(tmp.size == 0)
{
tmp.str=NULL;
return tmp;
}
//为tmp.str申请堆区空间
tmp.str = new char[tmp.size+1];
memset(tmp.str,0,tmp.size+1);
//str4+"hello"
//拷贝str4.str到tmp.str指向的空间中
if(this->str != NULL)
{
strcpy(tmp.str, this->str);
}
//将str5.str追加到tmp.str中
if(str != NULL)
{
strcat(tmp.str, str);
}
return tmp;
}
bool MyString::operator>(const MyString &ob)
{
if(str==NULL || ob.str == NULL)
{
cout<<"字符串存在空无法比较"<<endl;
exit(-1);
}
if(strcmp(str, ob.str)> 0)
return true;
return false;
}
bool MyString::operator>(const char *str)
{
if(this->str==NULL || str == NULL)
{
cout<<"字符串存在空无法比较"<<endl;
exit(-1);
}
if(strcmp(this->str, str)> 0)
return true;
return false;
}
//全局函数实现重载输出运算符 设置成MyString的友元
ostream & operator<<(ostream &out, MyString ob)
{
if(ob.str != NULL)
out<<ob.str;
return out;
}
//全局函数实现重载输入运算符 设置成MyString的友元
istream & operator>>(istream &in, MyString &ob)
{
char buf[1024]="";
in.getline(buf,sizeof(buf));
//释放ob.str指向的旧空间呢
if(ob.str != NULL)
{
delete [] ob.str;
ob.str=NULL;
}
ob.size = strlen(buf);
ob.str = new char[ob.size+1];
memset(ob.str, 0,ob.size+1);
strcpy(ob.str, buf);
return in;
}
网友评论