仿照string类,完成myString 类
代码:
头文件
#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
public:
myString(); //无参构造
myString(const char *str); //有参构造
~myString(); //析构
myString(const myString &other);//拷贝构造
myString &operator=(const myString &other);//拷贝赋值
bool empty(); //判空
int size_(); //size函数
const char *c_str_();//c_str函数
char &at_(int pos);//at函数
const myString operator+(const myString &R)const;//加号重载
myString &operator+=(const myString &R);//加等号重载
char & operator[](int i);//中括号重载
bool operator>(const myString &other)const;//关系重载
bool operator<(const myString &other)const;//关系重载
bool operator==(const myString &other)const;//关系重载
void show();//输出
private:
char *str; //存储字符串
int size; //存储字符串长度
};
#endif // TEST_H
源代码
#include "test.h"
myString::myString():size(30)//无参构造
{
str = new char[size]; //构造出一个长度为30的字符串
strcpy(str,""); //赋值为空串
}
myString::myString(const char *s) //有参构造
{
size = strlen(s);
str = new char[size+1];
strcpy(str,s);
}
myString::~myString() //析构
{
delete[]str;
}
myString::myString(const myString &other):str(new char[other.size+1]),size(other.size)//拷贝构造
{
strcpy(str,other.str);
}
myString & myString::operator=(const myString &other)//拷贝赋值
{
if(this != &other)
{
this->size = other.size;
if(this->str!=NULL) //判断原来的指向是否为空
{
delete []this->str;
}
this->str = new char[size+1]; //重新在堆区申请空间
strcpy(this->str,other.str); //拷贝字符串
}
return *this;
}
bool myString::empty() //判空
{
return strlen(str) == 0;
}
int myString::size_() //size函数
{
return size;
}
const char *myString::c_str_()//c_str函数
{
return str;
}
char &myString::at_(int pos)//at函数
{
return str[pos];
}
const myString myString::operator+(const myString &R)const//加号重载
{
myString temp;
delete []temp.str;
temp.str = new char[size+R.size];
strcpy(temp.str,this->str);//将自身给到新的地址中
strcat(temp.str,R.str); //将后面要追加的追加到新的地址中
temp.size = this->size + R.size;
return temp;
}
myString &myString::operator+=(const myString &R)//加等号重载
{
strcat(this->str,R.str); //将后面要追加的追加到新的地址中
this->size += R.size;
return *this;
}
char &myString::operator[](int i)//中括号重载
{
if(i >= 0 && i< size)
{
return this->str[i];
}
else
{
cout<<"数组越界"<<endl;
}
}
void myString::show()//输出
{
cout<<str<<endl;
}
bool myString::operator>(const myString &other)const//关系重载
{
if(strcmp(str,other.str)>0)
{
return true;
}
return false;
}
bool myString::operator<(const myString &other)const//关系重载
{
if(strcmp(str,other.str)<0)
{
return true;
}
return false;
}
bool myString::operator==(const myString &other)const//关系重载
{
if(strcmp(str,other.str)==0)
{
return true;
}
return false;
}
测试函数
#include "test.h"
int main()
{
char arr[10]="hello";
myString s1(arr); //有参构造
myString s2=s1; //拷贝构造
myString s3; //无参构造
s3=s2; //拷贝赋值函数
cout << "s1:" << s1.size_() << endl;
cout << "s2:" << s2.size_() << endl; //size
cout << "s3:" << s3.size_() << endl;
cout << "改变前";
s1.show();
s1.at_(1) = 'i'; //at()函数
cout << "改变后";
s1.show();
myString s4 = s1 + "world"; //加法运算符重载
s4.show();
myString s5("pig"); //有参构造
s4 += s5; //+=运算符重载
s4.show();
s4.size_();
if(s5>s1)
{
cout<<"s5 > s1"<<endl;
}else
{
cout<<"s5 <= s1"<<endl;
}
cout << "s4[6] = " << s4[6] << endl;
return 0;
}
运行结果
思维导图