创作本文目的:记录自己的学习历程
一、任务
1.要求
在昨天my_string的基础上,将能重载的运算符全部重载掉
关系运算符:>、<、==、>=、<=、!=
加号运算符:+
取成员运算符:[]
赋值运算符: =
2.代码
#include <iostream>
using namespace std;
class my_string
{
private:
char *str;
int len;
public:
//无参构造
my_string(){
cout<<"无参构造"<<endl;
this->str=new char[512];
this->len=0;
}
//有参构造
my_string(char *s,int l){
cout<<"有参构造"<<endl;
len = l;
str = new char[len+1];
memcpy(this->str,s,sizeof(char[len]));
str[len]=0;
}
//拷贝构造
my_string(const my_string &s){
len = s.len;
str = new char[len+1];
memcpy(this->str,s.str,sizeof(char[len]));
str[len]=0;
}
//拷贝赋值 赋值运算符重载
my_string &operator=(const my_string &s){
len = s.len;
str = new char[len+1];
memcpy(this->str,s.str,sizeof(char[len]));
str[len]=0;
return *this;
}
//关系运算符重载
bool operator<(my_string &s){
int i=0;
while(*(str+i)&&*(s.str+i) && *(str+i)==*(s.str+i))
{
i++;
}
if(*(str+i)<*(s.str+i))
return true;
else
return false;
}
bool operator>(my_string &s){
int i=0;
while(*(str+i)&&*(s.str+i) && *(str+i)==*(s.str+i))
{
i++;
}
if(*(str+i)>*(s.str+i))
return true;
else
return false;
}
bool operator==(my_string &s){
int i=0;
while(*(str+i)&& *(s.str+i) && *(str+i)==*(s.str+i))
{
i++;
}
if(*(str+i)==*(s.str+i))
return true;
else
return false;
}
bool operator!=(my_string &s){
int i=0;
while(*(str+i)&&*(s.str+i) && *(str+i)==*(s.str+i))
{
i++;
}
if(*(str+i)!=*(s.str+i))
return true;
else
return false;
}
bool operator<=(my_string &s){
int i=0;
while(*(str+i)&&*(s.str+i) && *(str+i)==*(s.str+i))
{
i++;
}
if(*(str+i)<=*(s.str+i))
return true;
else
return false;
}
bool operator>=(my_string &s){
int i=0;
while(*(str+i)&&*(s.str+i) && *(str+i)==*(s.str+i))
{
i++;
}
if(*(str+i)>=*(s.str+i))
return true;
else
return false;
}
//加号运算符重载
my_string& operator+(const my_string &s)
{
my_string tem;
char *q=tem.str;
char *y=this->str;
char *x=s.str;
int cout=0;
while(*q=*y)
{
q++;
y++;
cout++;
}
while(*q=*(x++))
{
q++;
cout++;
}
tem.len=cout;
return tem;
}
//取成员运算符重载
char operator[](int n)
{
if(n>len)
{
cout<<"数组越界"<<endl;
return '0';
}
return str[n];
}
//提取运算符重载
friend ostream& operator<<(ostream& ,my_string&);
//判空 空为真,非空为假
bool my_empty()
{
if(*str=='\0')
{
return true;
}
else
{
return false;
}
}
//求长度
int my_size()
{
char *q=str+len;
while(*q!='\0')
{
len++;
q++;
}
return len;
}
//转换为c风格字符串
char *my_str()
{
return str;
}
void display()
{
for(int i=0;i<len;i++)
{
cout<<*(str+i);
}
cout<<endl;
}
~my_string(){
// delete str;
// str=NULL;
}
};
ostream& operator<<(ostream& output,my_string& s)
{
char* q=s.str;
int i=0;
while(*q!='\0')
{
output<<*(s.str+i);
q++;
i++;
}
return output;
}
int main()
{
my_string s("hello world",11);
cout<<s.my_str()<<endl;
my_string s1(s);
s1.display();
my_string s2;
s2 = s1;
s2.display();
cout<<s2.my_size()<<endl;
my_string s3("",0);
if(s3.my_empty())
{
cout<<"空"<<endl;
}
else
{
cout<<"非空"<<endl;
s3.display();
}
my_string s4("hello zorld",11);
cout<<(s1>s2?"yes":"no")<<endl;
cout<<(s1==s2?"yes":"no")<<endl;
cout<<(s4>=s2?"yes":"no")<<endl;
s3=s1+s4;
cout<<s1+s2<<endl;
s3.display();
s1.display();
cout<<s3[2]<<endl;
cout<<s3<<endl;
return 0;
}