自己定义myString将能进行重载的运算符全部重载
main.c
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
private:
char *str; //记录C风格的字符串
int size; //记录字符串的实际长度
public:
//无参构造
myString():size(10)
{
str = new char[size]; //构造出一个长度为10的字符串
strcpy(str,"");
}
//有参构造
myString(const char *s)
{
size = strlen(s);
str = new char[size + 1];
strcpy(str, s);
}
//拷贝构造函数
myString(myString &other):str(new char(*(other.str)))
{
size = other.size;
str = new char[size + 1];
strcpy(str, other.str);
}
//析构函数
~myString()
{
delete str;
};
//判空函数
bool empty()
{
if(strlen(str) == 0)
{
return false;
}
else
{
return true;
}
}
//size函数
int mysize()
{
return size;
}
//c_str函数
char *c_str()
{
return str;
}
//at函数
char &at(int pos)
{
return str[pos];
}
myString &operator = (const myString &other)
{
if(this != &other)
{
delete []str;
this -> size = other.size;
this -> str = new char[size + 1];
strcpy(this -> str, (other.str));
}
return *this;
}
//=========================================================================
//算数运算符重载
//“+” 运算
const myString operator + (const myString &R)const
{
myString temp;
temp.size = this -> size + R.size;
temp.str = new char[size + 1];
strcpy(temp.str, this -> str);
strcat(temp.str, R.str);
return temp;
}
//中括号[]
char &operator[](int index)
{
if(index <= 0 || index >= this -> size)
return this -> str[index];
}
//“==”恒等于
bool operator == (const myString &R)const
{
if(strcmp(this -> str, R.str) == 0)
{
return true;
}
else
{
return false;
}
}
//“>”大于
bool operator > (const myString &R)const
{
if(strcmp(this -> str, R.str) > 0)
{
return true;
}
else
{
return false;
}
}
//“<”小于
bool operator < (const myString &R)const
{
if(strcmp(this -> str, R.str) < 0)
{
return true;
}
else
{
return false;
}
}
//将<<运算符重载函数设置成有元
friend ostream &operator << (ostream &L, const myString &O);
//将<<运算符重载函数设置成有元
friend istream &operator >> (istream &L, const myString &O);
};
//全局函数“<<”
ostream &operator << (ostream &L, const myString &O)
{
L << O.str << endl;
return L;
}
//全局函数“>>”
istream &operator >> (istream &L, const myString &O)
{
L >> O.str;
cout << O.str << endl;
return L;
}
int main()
{
cout << "===================================================" << endl;
cout << "有参类型" << endl;
//有参类型
char s[50] = "";
cout << "请输入一个字符串:";
gets(s);
myString str(s);
cout << "str = " << str.c_str() << endl;
cout << "===================================================" << endl;
cout << endl;
cout << endl;
cout << "===================================================" << endl;
cout << "拷贝构造函数" << endl;
//拷贝构造函数
myString str1(str);
cout << "str1 = " << str1.c_str() << endl;
cout << "===================================================" << endl;
cout << endl;
cout << endl;
cout << "===================================================" << endl;
cout << "size函数" << endl;
//size
cout << "str_size = " << str.mysize() << endl;
cout << "===================================================" << endl;
cout << endl;
cout << endl;
cout << "===================================================" << endl;
cout << "empty判空函数" << endl;
//empty
cout << "str_empty = " << str.empty() << endl;
cout << "===================================================" << endl;
cout << endl;
cout << endl;
// //at函数
// int n;
// cout << "请输入要查找字符串的第几位:";
// cin >> n;
// int size = strlen(s);
// if(n < 0 || n > size)
// {
// cout << "访问越界" << endl;
// }
// cout << "str_at[" << n << "] = " << str.at(n) << endl;
cout << "===================================================" << endl;
cout << "构造拷贝赋值函数" << endl;
//构造拷贝赋值函数
myString str2(str);
cout << "str2 = " << str2.c_str() << endl;
cout << "===================================================" << endl;
cout << endl;
cout << endl;
cout << "===================================================" << endl;
cout << "“+” 运算" << endl;
//“+” 运算
myString str3;
str3 = str + " word";
cout << "str3 = " << str3.c_str() << endl;
cout << "===================================================" << endl;
cout << endl;
cout << endl;
cout << "===================================================" << endl;
cout << "“[]”中括号" << endl;
//“[]”中括号
str[0] = 'H';
cout << "str = " << str.c_str() << endl;
cout << "===================================================" << endl;
cout << endl;
cout << endl;
cout << "===================================================" << endl;
cout << "“==”恒等于" << endl;
//“==”恒等于
if(str == str1)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
cout << "===================================================" << endl;
cout << endl;
cout << endl;
cout << "===================================================" << endl;
cout << "“>”大于" << endl;
//“>”大于
if(str > str1)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
cout << "===================================================" << endl;
cout << endl;
cout << endl;
cout << "===================================================" << endl;
cout << "“<”小于" << endl;
//“<”小于
if(str < str1)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
cout << "===================================================" << endl;
cout << endl;
cout << endl;
cout << "===================================================" << endl;
cout << "“<<” 输出函数" << endl;
//“<<” 输出函数
cout << str;
cout << "===================================================" << endl;
cout << endl;
cout << endl;
cout << "===================================================" << endl;
cout << "“>>” 输入函数" << endl;
//“>>” 输入函数
cin >> "hello";
cout << "===================================================" << endl;
cout << endl;
cout << endl;
return 0;
}
结果: