在My_string类的基础上,完成运算符重载
算术运算符:+
赋值运算符:+=
下标运算符:[]
关系运算符:>、=、
插入提取运算符:>
要求:注意数据的保护(const)
#include <iostream>
#include <cstring>
using namespace std;
class My_string
{
private:
char *data;
int size;
public:
//无参构造默认长度为15
My_string():size(15)
{
data = new char[size];
data[0] = '\0';
cout<<"My_string::无参构造"<<endl;
}
//有参构造
My_string(const char *str)
{
size=strlen(str);
data = new char[size+1];
//data[0] = '\0';
strcpy(data,str);
cout<<"My_string::有参构造"<<endl;
}
My_string(int n, char ch)
{
data = new char[n+1];
size=n;
// data[0] = '\0';
for(int i=0;i<n;i++)
{
data[i]=ch;
}
data[n-1]='\0';
cout<<"My_string::有参构造1"<<endl;
}
//析构函数
~My_string()
{
delete []data;
data=nullptr;
cout<<"My_string::析构"<<endl;
}
//拷贝构造函数
My_string(const My_string &other):data(new char[other.size+1]),size(other.size)
{
strcpy(data,other.data);
cout<<"My_string::拷贝构造函数"<<endl;
}
//拷贝赋值函数
My_string & operator=(const My_string &other)
{
delete []data;
data=new char(other.size);
strcpy(data,other.data);
size=other.size;
cout<<"My_string::拷贝赋值函数"<<endl;
return *this;
}
//c_str函数
char* c_str()
{
return this->data;
}
//size函数
int my_size()
{
return size;
}
//empty函数
bool empty()
{
return size==0?1:0;
}
//at函数
char &my_at(int num)
{
if(num<0||num>size)
{
cout<<"数组越界"<<endl;
}
else
return data[num-1];
}
//show
void my_show()
{
cout<<"data= "<<this->data<<endl;
}
//实现+运算符重载 字符串相加 s3=s1+s2
const My_string operator+(const My_string &other)const
{
My_string temp; //s3
delete []temp.data;
temp.size=size+other.size;
temp.data=new char(temp.size+1);
strcpy(temp.data,data);
temp.data=strcat(temp.data,other.data); //拼接
return temp;
}
//实现+=运算符的重载: s1+=s2
My_string &operator+=(const My_string &other)
{
My_string temp; //定义一个中间类
delete []temp.data;
temp.data=new char(size+1);
strcpy(temp.data,data); //将类data赋予中间data
temp.size=this->size;
delete []data; //delete data
this->data=new char(temp.size+other.size+1); //重新申请data空间
strcpy(data,temp.data); //将中间data重新赋值回data
strcat(data,other.data); //将other.data拼接回data
size=strlen(data);
return *this; //为了级联使用,连等于
}
//实现[]运算符重载:s1[]
char &operator[](int n)
{
if(n<0||n>size)
{
cout<<"数组越界"<<endl;
}
else
return data[n-1];
}
//实现>运算符重载函数: strcmp()
bool operator>(const My_string &other)const
{
return strcmp(data,other.data)>0;
}
//实现>运算符重载函数: strcmp()
bool operator<(const My_string &other)const
{
return strcmp(data,other.data)<0;
}
//实现==运算符重载函数: strcmp()
bool operator==(const My_string &other)const
{
return strcmp(data,other.data)==0;
}
//实现!=运算符重载函数: strcmp()
bool operator!=(const My_string &other)const
{
return strcmp(data,other.data)!=0;
}
//实现>=运算符重载函数: strcmp()
bool operator>=(const My_string &other)const
{
return strcmp(data,other.data)>=0;
}
//实现<=运算符重载函数: strcmp()
bool operator<=(const My_string &other)const
{
return strcmp(data,other.data)<=0;
}
//实现插入运算符的重载: <<
friend ostream &operator<<(ostream &L, const My_string &other);
//实现提取运算符的重载: >>
friend ostream &operator>>(ostream &L, const My_string &other);
};
//实现插入运算符的重载: <<
ostream &operator<<(ostream &L, const My_string &other)
{
L << other.data<<endl;
return L;
}
//实现提取运算符的重载: >>
ostream &operator>>(ostream &L, const My_string &other)
{
L << other.data<<endl;
return L;
}
int main()
{
My_string s1("weixinyu");
//有参构造 show函数
My_string s2(s1); //有参构造
cout <<"s1";
s1.my_show();
//构造拷贝函数
My_string s3("drp"); //拷贝构造函数
My_string s4(s2);
cout <<"s3";
s3.my_show();
//c_str函数
cout <<"s3= "<<s3.c_str() <<endl;
//size函数
cout <<"s1-size= "<<s1.my_size() <<endl;
//empty函数
if(s1.empty())
{
cout << "s1为空 "<<endl;
}
else
{
cout << "s1为非空 "<<endl;
}
//at函数
cout << "s1第四位 " <<s1.my_at(4) <<endl;
//加
My_string s5;
s5=s1+s3;
cout<<s5;
//+=
My_string s6("love");
s6+=s3;
cout<<s6;
//[]
cout<<"s6[4]=" <<s6[4]<<endl;
//= != > < >= <=
if(s1==s3)
{
cout<<"s1==s3"<<endl;
}
else if(s1>s3)
{
cout<<"s1>s3"<<endl;
}
else if(s1<s3)
{
cout<<"s1<s3"<<endl;
}
else if(s1>=s3)
{
cout<<"s1>=s3"<<endl;
}
else if(s1<=s3)
{
cout<<"s1<=s3"<<endl;
}
if(s1!=s3)
{
cout<<"s1!=s3"<<endl;
}
return 0;
}
实现结果