作业内容
在昨天my_string的基础上,将能重载的运算符全部重载掉
关系运算符:>、<、==、>=、<=、!=
加号运算符:+
取成员运算符:[]
赋值运算符: =
代码部分
#include <iostream>
#include <cstring>
using namespace std;
class my_string
{
private:
char *str;
int len;
public:
friend const bool operator >(const my_string&, const my_string&);
friend const bool operator <(const my_string&, const my_string&);
friend const bool operator ==(const my_string&, const my_string&);
friend const bool operator >=(const my_string&, const my_string&);
friend const bool operator <=(const my_string&, const my_string&);
friend const bool operator !=(const my_string&, const my_string&);
//friend const my_string& operator +(my_string&, const my_string&);
//无参构造
my_string()
{
str = new char[128];
memset(str, 0, 128);
len = strlen(str);
//cout << "无参构造函数" << endl;
}
//有参构造
my_string(char *str)
{
this->str = new char[128];
strcpy(this->str, str);
len = strlen(str);
//cout << "有参构造函数" << endl;
}
//拷贝构造
my_string(const my_string &other)
{
str = new char[128];
memset(str, 0, 128);
strcpy(str, other.str);
len = other.len;
//cout << "拷贝构造函数" << endl;
}
//拷贝赋值
my_string& operator =(const my_string &other)
{
str=new char[128];
memset(str, 0, 128);
strcpy(str,other.str);
len=other.len;
}
~my_string()
{
delete str;
}
//显示函数
void display()
{
cout << str << '\t' << len << endl;
}
//判空
bool my_empty()
{
return strlen(str)==0?true:false;
}
//求长度
int my_size()
{
return strlen(str);
}
//转化为c风格字符串
char *my_str()
{
return str;
}
//加号运算符
my_string operator +(const my_string& O)const
{
my_string temp;
strcpy(temp.str, this->str);
strcat(temp.str, O.str);
temp.len = this->len + O.len;
return temp;
}
//取成员运算符
const char operator [](const int index)
{
return this->str[index-1];
}
// //赋值运算符
// const my_string& operator =(const my_string& R)
// {
// strcpy(this->str, R.str);
// return *this;
// }
};
//关系运算符
const bool operator >(const my_string& L, const my_string& R)
{
if(strcmp(L.str,R.str) > 0)
return true;
else
return false;
}
const bool operator <(const my_string& L, const my_string& R)
{
if(strcmp(L.str,R.str) < 0)
return true;
else
return false;
}
const bool operator ==(const my_string& L, const my_string& R)
{
if(strcmp(L.str,R.str) == 0)
return true;
else
return false;
}
const bool operator >=(const my_string& L, const my_string& R)
{
if(strcmp(L.str,R.str) >= 0)
return true;
else
return false;
}
const bool operator <=(const my_string& L, const my_string& R)
{
if(strcmp(L.str,R.str) <= 0)
return true;
else
return false;
}
const bool operator !=(const my_string& L, const my_string& R)
{
if(strcmp(L.str,R.str) != 0)
return true;
else
return false;
}
//加号运算符
//const my_string& operator +(my_string& L, const my_string& R)
//{
// strcat(L.str, R.str);
// L.len = L.len + R.len;
// return L;
//}
int main()
{
// my_string ms1;
// char* str = "zhangying";
// my_string ms2(str);
// cout << ms2.my_empty() << endl;
// ms2.display();
// my_string ms3(ms2);
// cout << ms3.my_empty() << endl;
// ms3.display();
// my_string ms4 = ms3;
// cout << ms4.my_empty() << endl;
// ms4.display();
my_string ms1("zhang");
my_string ms2("ying");
//关系运算符
cout << "ms1>ms2:" << (ms1>ms2) << endl;
cout << "ms1<ms2:" << (ms1<ms2) << endl;
cout << "ms1==ms2:" << (ms1==ms2) << endl;
cout << "ms1>=ms2:" << (ms1>=ms2) << endl;
cout << "ms1<=ms2:" << (ms1<=ms2) << endl;
cout << "ms1!=ms2:" << (ms1!=ms2) << endl;
//加号运算符
my_string ms3=ms1+ms2;
ms3.display();
cout << ms3.my_str()[2] << endl;
return 0;
}