作业要求://在My_string类的基础上,完成运算符重载//算术运算符:+//赋值运算符:+=//下标运算符:[]//关系运算符:>、<、==、!=、>=、<=//插入提取运算符:<<、>>//
实现过程:
#include <iostream>
#include <cstring>
//在My_string类的基础上,完成运算符重载
//算术运算符:+
//赋值运算符:+=
//下标运算符:[]
//关系运算符:>、<、==、!=、>=、<=
//插入提取运算符:<<、>>
//要求:注意数据的保护(const)
using namespace std;
class My_string
{
private:
char *data;
int size;
public:
//无参构造默认长度为15
My_string();
//有参构造
My_string(const char *str);
//有参构造
My_string(int n, char ch);
//析构函数
~My_string();
//拷贝构造函数
My_string(const My_string &other);
//拷贝赋值函数
My_string & operator=(const char *other);
//实现+运算符重载
const My_string operator+(const My_string &R)const;
//实现+=运算符重载
My_string & operator+=(const My_string &R);
//实现>运算符重载函数
bool operator>(const My_string &R)const;
char operator[](int i)const;
//c_str函数
char * my_c_str();
//sizeof函数
int my_sizeof();
//my_strlen函数
int my_strlen();
//empty函数
My_string empty();
//at函数
char my_at(int i);
//友元函数声明
friend ostream &operator<<(ostream &L,const My_string &R);
friend istream &operator>>(istream &L,const My_string &R);
};
//全局函数
ostream &operator<<(ostream &L,const My_string &R); //插入函数
istream &operator>>(istream &L,const My_string &R); //提取函数
int main()
{
My_string str1("hello world!wwwwww"); //有参构造
int size=str1.my_sizeof();
cout<<"str1(size)="<<size<<endl;
str1="hello world!wwwwwwwwwwwwww";
size=str1.my_sizeof(); //二倍扩容
cout<<"str1(size)="<<size<<endl;
My_string str2(10,'C'); //有参构造
My_string str3; //无参构造
str3=str1; //拷贝赋值构造
My_string str4(str2); //拷贝构造
My_string str5 = str1; //拷贝构造
//字符串输出
cout<<"str1 = "<<str1<<endl; //插入函数
cout<<"str2 = "<<str2<<endl;
printf("str3 = %s\n",str3.my_c_str()); //C字符串输出
cout<<"str4 = "<<str4<<endl;
cout<<"str5 = "<<str5<<endl;
//判断字符串是否为空
My_string str6;
cout<<"str1 = "<<str1.empty()<<endl;
cout<<"str6 = "<<str6.empty()<<endl;
My_string str7;
cout<<"输入str7>";
cin>>str7; //提取函数
cout<<"str7 = "<<str7<<endl;
My_string str8;
str8=str1+str2; //+运算符重载
cout<<"str8 = "<<str8<<endl;
str8+=str1; //+运算符重载
cout<<"str8 = "<<str8<<endl;
if(str1>str8)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
cout<<"str1[1]="<<str1[1]<<endl;
cout<<"结束"<<endl;
return 0;
}
//无参构造默认长度为15
My_string::My_string():size(15)
{
data = new char[size];
data[0] = '\0';
}
//有参构造
My_string::My_string(const char *str):size(15)
{
int len=strlen(str)+1;
if(len>size)
{
size=len;
}
data = new char[size];
data[0] = '\0';
strcpy(data,str);
}
//有参构造
My_string::My_string(int n, char ch):size(15)
{
if(n>size)
{
size=n;
}
data = new char[size];
data[0] = '\0';
for(int i=0;i<n;i++)
{
data[i]=ch;
}
}
//析构函数
My_string::~My_string()
{
delete []data;
}
//拷贝构造函数
My_string::My_string(const My_string &other):size(15)
{
int len=strlen(other.data)+1;
while(len>size)
{
size=len;
}
data = new char[size];
data[0] = '\0';
strcpy(this->data,other.data);
}
//拷贝赋值函数
My_string & My_string::operator=(const char *other)
{
delete []data;
int len=strlen(other)+1;
if(len>size)
{
size=size*2;
}
data = new char[size];
data[0] = '\0';
strcpy(data,other);
return *this;
}
//c_str函数
char * My_string::my_c_str()
{
return data;
}
//sizeof函数
int My_string::my_sizeof()
{
return size;
}
//my_strlen函数
int My_string::my_strlen()
{
return strlen(data);
}
//empty函数
My_string My_string::empty()
{
if(0==my_strlen())
{
return "空字符串";
}
else
{
return "非空字符串";
}
}
//at函数
char My_string::my_at(int i)
{
return data[i];
}
//实现+运算符重载
const My_string My_string::operator+(const My_string &R)const
{
My_string temp; //定义一个临时的类对象
temp.size=15;
delete []temp.data;
int len=strlen(this->data)+strlen(R.data)+1;
if(len>temp.size)
{
temp.size=len;
}
temp.data=new char[temp.size];
strcpy(temp.data,this->data);
strcat(temp.data,R.data);
return temp;
}
//实现+=运算符重载
My_string & My_string::operator+=(const My_string &R)
{
My_string temp(this->data); //定义一个临时的类对象存原值
delete []data;
int len=strlen(temp.data)+strlen(R.data)+1;
if(len>this->size)
{
this->size=this->size*2;
}
this->data=new char[this->size];
strcpy(this->data,temp.data);
strcat(this->data,R.data);
return *this;
}
//实现>运算符重载函数
bool My_string::operator>(const My_string &R)const
{
return strcmp(this->data,R.data)>0;
}
char My_string::operator[](int i)const
{
return data[i];
}
//全局函数
ostream &operator<<(ostream &L,const My_string &R) //插入函数
{
L<<R.data;
return L;
}
istream &operator>>(istream &L,const My_string &R) //提取函数
{
L>>R.data;
return L;
}
实现效果: