1.运算符重载
头文件
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
private:
char *str;//C风格字符串
int size=0;
public:
std::string s_str;
//转换构造函数
myString(const std::string &s);
//无参构造
myString();
//有参构造
//string s("hello world")
myString(const char *s);
//有参构造
//string s(5,'A');
myString(int n, const char s);
//析构函数
~ myString();
//拷贝构造函数
myString(const myString &other);
//拷贝赋值函数
myString &operator=(const myString &other);
//判空函数
//返回1表示空,返回0表示非空
bool empty();
//size函数
int str_size();
//c_str函数
const char* c_str() const;
//at函数
char &at(int index);
//二倍扩容
void expand();
//+运算符重载
const myString operator+(const myString &R) const;
//+运算符重载
const myString operator+(const char &R);
//+=运算符重载
myString & operator+=(const myString &R);
//&(取地址)运算符重载
const myString * operator&() const;
//[]运算符重载
char & operator[](const int index);
//==运算符重载
bool operator==(const myString &R);
//!=运算符重载
bool operator!=(const myString &R);
//<运算符重载
bool operator<(const myString &R);
//>运算符重载
bool operator>(const myString &R);
//<=运算符重载
bool operator<=(const myString &R);
//>=运算符重载
bool operator>=(const myString &R);
//将<<重载函数设为友元
friend ostream & operator<<(ostream &L, const myString &R);
//将>>重载函数设为友元
friend istream & operator<<(istream &L, const myString &R);
//将getline重载函数设为友元
friend istream & getline(istream &L, myString &R);
};
#endif // MYSTRING_H
源文件
#include "mystring.h"
//函数定义
//转换构造函数
myString::myString(const std::string &s):s_str(s)
{
cout<<"转换构造"<<endl;
}
//无参构造
myString::myString():size(10)
{
str = new char[size];
memset(str,'\0',10);
cout<<"无参构造"<<endl;
}
//有参构造
//string s("hello world")
myString::myString(const char *s):size(strlen(s)+1)
{
str = new char[size];
strcpy(str,s);
cout<<"有参构造"<<endl;
}
//有参构造
//string s(5,'A');
myString::myString(int n, const char s):size(n+1)
{
str = new char[size];
memset(str,s,n);
str[n] = '\0';
cout<<"有参构造"<<endl;
}
//析构函数
myString::~ myString()
{
delete []str;
cout<<"析构成功"<<endl;
}
//拷贝构造函数
myString::myString(const myString &other):size(other.size)
{
str = new char[size];
memcpy(str,other.str,size);
cout<<"拷贝构造完成"<<endl;
}
//判空函数
//返回1表示空,返回0表示非空
bool myString::empty()
{
if(size==0||str[0]=='\0')
{
cout<<"该字符串为空"<<endl;
return 1;
}
cout<<"该字符串非空"<<endl;
return 0;
}
//size函数
int myString::str_size()
{
cout<<"该字符串长度为"<<size<<endl;
return size;
}
//c_str函数
const char* myString::c_str() const
{
cout<<"该字符串为"<<str<<endl;
return str;
}
//at函数
char &myString::at(int index)
{
if(empty()||index<0||index>=size)
{
cout<<"查找失败"<<endl;
}
cout<<"该字符串下标为"<<index<<"的字符为"<<str[index]<<endl;
return str[index];
}
//二倍扩容
void myString::expand()
{
if(empty())
{
cout<<"二倍扩容失败"<<endl;
return ;
}
char *nstr = new char[2*size];
memcpy(nstr,str,size);
delete []str;
str = nstr;
size*=2;
cout<<"二倍扩容成功"<<endl;
}
//拷贝赋值函数
//=运算符重载
myString & myString::operator=(const myString &other)
{
if(&other!=this)
{
delete []str;
size = other.size;
str = new char[size];
strcpy(str,other.str);
cout<<"=运算符重载"<<endl;
}
return *this;
}
//+运算符重载
const myString myString::operator+(const myString &R) const
{
myString temp;
temp.size = size+R.size-1;
temp.str = new char[size];
strcpy(temp.str,str);
strcat(temp.str,R.str);
cout<<"+运算符重载"<<endl;
return temp;
}
//+运算符重载
const myString myString::operator+(const char &R)
{
if(R=='\0')
{
cout<<"+运算符重载"<<endl;
return *this;
}else
{
str[size-1] = R;
size++;
str[size] = '\0';
cout<<"+运算符重载"<<endl;
return *this;
}
}
//+=运算符重载
myString & myString::operator+=(const myString &R)
{
int n_size = size+R.size-1;
char *n_str = new char[n_size];
strcpy(n_str,str);
strcat(n_str,R.str);
delete []str;
str = n_str;
size = n_size;
cout<<"+=运算符重载"<<endl;
return *this;
}
//&(取地址)运算符重载
const myString * myString::operator&() const
{
cout<<"&运算符重载"<<endl;
return this;
}
//[]运算符重载
char & myString::operator[](const int index)
{
if(index>=0&&index<size)
{
cout<<"该字符串下标为"<<index<<"的字符为"<<str[index]<<endl;
cout<<"[]运算符重载"<<endl;
return str[index];
}
throw out_of_range("索引越界,请更改索引!");
}
//==运算符重载
bool myString::operator==(const myString &R)
{
if(strcmp(str,R.str)==0)
{
cout<<"这两个字符串相等"<<endl;
cout<<"==运算符重载"<<endl;
return 1;
}
cout<<"这两个字符串不相等"<<endl;
cout<<"==运算符重载"<<endl;
return 0;
}
//!=运算符重载
bool myString::operator!=(const myString &R)
{
if(strcmp(str,R.str)!=0)
{
cout<<"这两个字符串不相等"<<endl;
cout<<"==运算符重载"<<endl;
return 1;
}
cout<<"这两个字符串相等"<<endl;
cout<<"!=运算符重载"<<endl;
return 0;
}
//<运算符重载
bool myString::operator<(const myString &R)
{
if(strcmp(str,R.str)<0)
{
cout<<str<<"小于"<<R.str<<endl;
cout<<"<运算符重载"<<endl;
return 1;
}
cout<<str<<"不小于"<<R.str<<endl;
cout<<"<运算符重载"<<endl;
return 0;
}
//>运算符重载
bool myString::operator>(const myString &R)
{
if(strcmp(str,R.str)>0)
{
cout<<str<<"大于"<<R.str<<endl;
cout<<">运算符重载"<<endl;
return 1;
}
cout<<str<<"不大于"<<R.str<<endl;
cout<<">运算符重载"<<endl;
return 0;
}
//<=运算符重载
bool myString::operator<=(const myString &R)
{
if(strcmp(str,R.str)<=0)
{
cout<<str<<"小于等于"<<R.str<<endl;
cout<<"<=运算符重载"<<endl;
return 1;
}
cout<<str<<"大于"<<R.str<<endl;
cout<<"<=运算符重载"<<endl;
return 0;
}
//>=运算符重载
bool myString::operator>=(const myString &R)
{
if(strcmp(str,R.str)>=0)
{
cout<<str<<"大于等于"<<R.str<<endl;
cout<<">=运算符重载"<<endl;
return 1;
}
cout<<str<<"小于"<<R.str<<endl;
cout<<">=运算符重载"<<endl;
return 0;
}
//<<运算符重载
ostream & operator<<(ostream &L, const myString &R)
{
L<<R.str<<endl;
cout<<"<<运算符重载"<<endl;
return L;
}
//>>运算符重载
istream & operator<<(istream &L, const myString &R)
{
L>>R.str;
cout<<">>运算符重载"<<endl;
return L;
}
//getline重载
istream & getline(istream &L, myString &R)
{
std::string temp;
std::getline(L, temp);
R.s_str = temp;
cout<<"getline重载"<<endl;
return L;
}
主程序
#include "mystring.h"
int main()
{
myString str1;
myString str2("Hello world!");
str2[9];
myString str3(10,'Z');
myString str4 = str2;
myString str5("You are a genius.");
str4.operator!=(str2);
str5.operator>(str3);
str2.operator<=(str5);
cout<<str5;
myString s6;
cout<<"请输入字符串:";
getline(cin,s6);
return 0;
}
2.思维导图