目录
作业
在昨天my_string的基础上,将能重载的运算符全部重载掉
关系运算符:>、=、
加号运算符:+
取成员运算符:[]
赋值运算符: =
代码
#include <iostream>
#include <cstring>
using namespace std;
class my_string
{
private:
char *str;
int len;
public:
// 无参构造
my_string(){}
// 有参构造
my_string(char *s)
{
len=strlen(s);
//str=new char[len];
str=new char[len];
// memset(str,0,sizeof(str));
//strcpy(str,s);
str=s;
}
// 拷贝构造
my_string(my_string& O)
{
len=O.len;
str=O.str;
}
// 拷贝赋值
my_string& operator =(const my_string &R)//拷贝赋值
{
if (this != &R)
{
this->str=R.str;
this->len=R.len;
}
return *this;
}
bool my_empty()
{
if(len==0)
return true;
else
return false;
}
int my_size()
{
return len;
}
char *my_str()
{
return str;
}
void display()
{
//cout<<my_str()<<'\t'<<len<<endl;
cout<<my_str()<<'\t'<<"len:"<<len<<'\t';
}
friend bool operator>(my_string &str1,my_string& str2);
friend bool operator<(my_string &str1,my_string& str2);
friend bool operator>=(my_string &str1,my_string& str2);
friend bool operator<=(my_string &str1,my_string& str2);
friend bool operator==(my_string &str1,my_string& str2);
friend bool operator!=(my_string &str1,my_string& str2);
friend const my_string operator+(my_string &str1,const my_string &str2);
char operator[](int x)
{
return this->str[x];
}
};
bool operator>(my_string &str1,my_string& str2)
{
if(str1.str>str2.str)
return true;
else
return false;
}
bool operator>=(my_string &str1,my_string& str2)
{
if(str1.str>=str2.str)
return true;
else
return false;
}
bool operator<(my_string &str1,my_string& str2)
{
if(str1.str<str2.str)
return true;
else
return false;
}
bool operator<=(my_string &str1,my_string& str2)
{
if(str1.str<=str2.str)
return true;
else
return false;
}
bool operator==(my_string &str1,my_string& str2)
{
if(str1.str==str2.str)
return true;
else
return false;
}
bool operator!=(my_string &str1,my_string& str2)
{
if(str1.str!=str2.str)
return true;
else
return false;
}
const my_string operator+(my_string &str1,const my_string &str2)
{
my_string res;
strcpy(res.str,str1.str);
strcat(res.str,str2.str);
res.len=strlen(res.str);
return res;
}
int main()
{
//char s="das";
my_string s1("wangyulin");
my_string s2(s1);
my_string s3("lisi");
s3=s2;
s1.display();
if(s1.my_empty())
cout<<"empty"<<endl;
else
cout<<"not empty"<<endl;
s2.display();
if(s2.my_empty())
cout<<"empty"<<endl;
else
cout<<"not empty"<<endl;
s3.display();
if(s3.my_empty())
cout<<"empty"<<endl;
else
cout<<"not empty"<<endl;
my_string s4("");
s4.display();
if(s4.my_empty())
cout<<"empty"<<endl;
else
cout<<"not empty"<<endl;
my_string s5("995");
s5.display();
if(s5.my_empty())
cout<<"empty"<<endl;
else
cout<<"not empty"<<endl;
cout<<"********************************"<<endl;
cout<<"s5 > s1-->"<<(s5>s1?"yes":"no")<<endl;
cout<<"s5 < s1-->"<<(s5<s1?"yes":"no")<<endl;
cout<<"s5 >= s1-->"<<(s5>=s1?"yes":"no")<<endl;
cout<<"s5 <= s1-->"<<(s5<=s1?"yes":"no")<<endl;
cout<<"s5 == s1-->"<<(s5==s1?"yes":"no")<<endl;
cout<<"s5 != s1-->"<<(s5!=s1?"yes":"no")<<endl;
cout<<"********************************"<<endl;
my_string s6;
s6=s5+s1;
s6.display();cout<<endl;
cout<<"********************************"<<endl;
cout<<s1[2]<<endl;
return 0;
}
运行结果