2023/3/329 运算符重载
使用C++封装String,并为其重载运算符
#include <iostream>
#include <cstring>
#include <exception>
using namespace std;
class MyExcption : public std ::exception
{
private:
std::string message;
public:
MyExcption(const char* msg): message(msg) {}
virtual const char* what() const throw()
{
return message.c_str();
}
};
class mystring
{
private:
char *str;
int s_size;
public:
mystring():s_size(0)
{
str = new char[s_size+1];
*str = '\0';
}
mystring(const char *s)
{
if(nullptr == s)
{
s_size = 0;
str = new char[s_size+1];
*str = '\0';
}else
{
s_size = strlen(s);
str = new char[s_size+1];
strcpy(str,s);
}
}
~mystring()
{
delete []str;
}
bool is_empty()
{
if(0 == s_size)
{
return true;
}else
{
return false;
}
}
int s_len()
{
return s_size;
}
char *c_str()
{
return str;
}
char &at(int i)
{
char *c = str;
try {
if(i<0 && i>s_size)
{
throw MyExcption("下标越界");
}
}catch (const std::exception& e) {
std::cerr << "Error:" << e.what() <<endl;
}
return *(c+i);
}
mystring(const mystring & other)
{
s_size = other.s_size;
str = new char[s_size+1];
strcpy(str,other.str);
}
mystring & operator = (const mystring & other)
{
if(this != &other)
{
delete []str;
this->s_size = other.s_size;
this->str = new char[s_size+1];
strcpy(this->str,other.str);
}
return *this;
}
char &operator[](const int &i)const
{
return this->str[i];
}
const mystring operator+(const mystring &other)const
{
mystring temp;
temp.s_size = this->s_size + other.s_size;
temp.str = new char[s_size+1];
*temp.str = '\0';
strcat(temp.str,this->str);
strcat(temp.str,other.str);
return temp;
}
bool operator==(const mystring &other)const
{
if(strcmp(this->str,other.str) == 0)
{
return true;
}else
{
return false;
}
}
bool operator!=(const mystring &other)const
{
if(strcmp(this->str,other.str) != 0)
{
return true;
}else
{
return false;
}
}
bool operator<(const mystring &other)const
{
if(strcmp(this->str,other.str) < 0)
{
return true;
}else
{
return false;
}
}
bool operator>(const mystring &other)const
{
if(strcmp(this->str,other.str) > 0)
{
return true;
}else
{
return false;
}
}
bool operator<=(const mystring &other)const
{
if(strcmp(this->str,other.str) <= 0)
{
return true;
}else
{
return false;
}
}
bool operator>=(const mystring &other)const
{
if(strcmp(this->str,other.str) >= 0)
{
return true;
}else
{
return false;
}
}
friend istream &operator>>(istream &L,mystring &other);
friend ostream &operator<<(ostream &L,mystring &other);
};
istream &operator>>(istream &L,mystring &other)
{
char s[other.s_len()];
cin >> s;
strcpy(other.c_str(),s);
return L;
}
ostream &operator<<(ostream &L,mystring &other)
{
L << other.c_str()<< endl;
return L;
}
int main()
{
mystring s1;
cout << "s1 = " << s1.c_str() << endl;
mystring s2("world");
cout << "s2 = " << s2.c_str() << endl;
cout << s1.is_empty() << endl;
cout << s2.is_empty() << endl;
int len = s2.s_len();
for(int i=0;i<len;i++)
{
cout << s2.at(i) << "\t";
}
cout << endl;
cout << " =: ";
mystring s3;
s3 = s2;
cout << "s3 = " << s3.c_str() << endl;
cout << " (): ";
mystring s4(s2);
cout << "s4 = " << s4.c_str() << endl;
cout << " +: ";
mystring s5;
s5 = s2 + s3;
cout << "s5 = " << s5;
cout << "s5[1] = " << s5[1] << endl;
cout << "s2 = " << s2;
cout << "s3 = " << s3;
cout << " >: ";
if(s3 > s2)
{
cout << "yes" << endl;
}else
{
cout << "no" << endl;
}
cout << " >=: ";
if(s3 >= s2)
{
cout << "yes" << endl;
}else
{
cout << "no" << endl;
}
cout << " ==: ";
if(s2 == s3)
{
cout << "yes" << endl;
}else
{
cout << "no" << endl;
}
cout << " <: ";
if(s3 < s2)
{
cout << "yes" << endl;
}else
{
cout << "no" << endl;
}
cout << " <=: ";
if(s3 <= s2)
{
cout << "yes" << endl;
}else
{
cout << "no" << endl;
}
cout << " !=: ";
if(s3 != s2)
{
cout << "yes" << endl;
}else
{
cout << "no" << endl;
}
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/83fc893d92246d7f1f68799e03892fb1.png)