#include <iostream>
#include <cstring>
using namespace std;
class mystring
{
char *str;
int size;
public:
mystring():size(1)
{
str = new char[this->size];
strcpy(str,"");
}
mystring(const char *buf)
{
size = strlen(buf);
str = new char[size+1];
strcpy(str,buf);
}
//拷贝构造
mystring(const mystring &s):
str(new char [s.size+1]),
size(s.size){
strcpy(this->str,s.str);
}
//析构函数
~mystring()
{
delete []str;
str = nullptr;
}
//拷贝赋值
mystring &operator=(const mystring &s)
{
if(this!=&s){
delete []this->str;
//得到s的size,赋值给this
this->size = s.size;
//new除size+1的空间,将字符串赋值过来
this->str = new char[this->size+1];
strcpy(this->str,s.str);
}
return *this;
}
//判空
bool strempty()
{
return *(str)==0?true:false;
}
//size
int retsize()
{
return size;
}
//c_str
char* &c_str()
{
return str;
}
//at
char &at(int pos)
{
return *(str+pos);
}
//+
mystring operator+(const mystring &R)//加返回临时值
{
mystring temp;
temp.size=this->size + R.size;
temp.operator=(strcat(this->str,R.str));
return temp;
}
//+=
mystring &operator+=(const mystring &R)//加等返回对象自己
{
this->size+=R.size;
strcat(this->str,R.str);
return *this;
}
//>
bool operator>(const mystring &R)const//关系判断两边操作数都只读
{
for(int i=0;i<this->size;i++)//for判断size内是否>
{
if(*(this->str+i)>*(R.str+i))
return true;
else if(*(this->str+i)==*(R.str+i))//相等进入下次循环
continue;
else
return false;//不等返回false
}
return false;//全部循环结束表示size内全都相等,说明R更长,返回不等
}
//<
bool operator<(const mystring &R)const
{
for(int i=0;i<this->size;i++)
{
if(*(this->str+i)<*(R.str+i))
return true;
else if(*(this->str+i)==*(R.str+i))
continue;
else
return false;
}
return false;
}
//>=
bool operator>=(const mystring &R)const//等号不再continue
{
for(int i=0;i<this->size;i++)
{
if(*(this->str+i)>=*(R.str+i))
return true;
else
return false;
}
return false;
}
//<=
bool operator<=(const mystring &R)const
{
for(int i=0;i<this->size;i++)
{
if(*(this->str+i)<=*(R.str+i))
return true;
else
return false;
}
return false;
}
//==
bool operator==(const mystring &R)const//先判断size是否相等,相等再循环,循环体内不等返回false,循环结束返回true
{
if(this->size!=R.size)
return false;
for(int i=0;i<this->size;i++)
{
if(*(this->str+i)!=*(R.str+i))
return false;
}
return true;
}
//!=
bool operator!=(const mystring &R)const
{
if(this->size!=R.size)
return true;
for(int i=0;i<this->size;i++)
{
if(*(this->str+i)!=*(R.str+i))
return true;
}
return false;
}
char &operator[](const int &dst)//调用at函数,返回成员引用
{
return this->at(dst);
}
friend ostream &operator <<(ostream &out,const mystring &s);
friend istream &operator >>(istream & in,const mystring &s);
};
//<<
ostream &operator <<(ostream &out,const mystring &s)//全局,申请友元
{
out<<s.str<<endl;
return out;
}
//>>
istream &operator >>(istream & in,const mystring &s)
{
in>>s.str;
return in;
}
int main()
{
mystring s1("hello world");
mystring s2("how are you");
mystring s3(s1);
cout<<"s1:"<<s1.c_str()<<endl;
cout<<"s2:"<<s2.c_str()<<endl;
if(s1>=s2)
{
cout<<"s1>=s2"<<endl;
}
else
cout<<"s1<=s2"<<endl;
cin>>s3;
cout<<s3;
return 0;
}
继承:
#include <iostream>
using namespace std;
class father
{
public:
int *pwd;//需要深拷贝
father(){cout<<"father无参构造"<<endl;}//无参构造
father(int *p):pwd(p){cout<<"father有参构造"<<endl;}//有参构造
father(const father& other):pwd(new int(*(other.pwd))){cout<<"father拷贝构造"<<endl;}//拷贝构造
father & operator=(const father &other)//拷贝赋值
{
cout<<"father拷贝赋值"<<endl;
if(this!=&other)
{
delete pwd;
pwd = new int(*(other.pwd));
}
return *this;
}
~father(){cout<<"father析构函数"<<endl;}
};
class son:public father
{
private:
double score;
public:
son(){cout<<"son无参构造"<<endl;}//son调用无参构造,默认自动调用father的无参构造
son(int *p,double sco):score(sco),father(p){cout<<"son有参构造"<<endl;}//son的有参构造,需要再初始化列表中调用father的有参构造
son( const son &other):score(other.score),father(other){cout<<"son的拷贝构造"<<endl;}//son的拷贝构造,需要再初始化列表中调用father的拷贝构造
son &operator =(const son & other)//son的拷贝赋值,需要调用father的拷贝赋值
{
cout<<"son的拷贝赋值"<<endl;
if(this!=&other)
{
this->score=other.score;
father::operator=(other);
}
return *this;
}
~son(){cout<<"son的析构函数"<<endl;}
};
int main()
{
int pwd = 123;
son son1(&pwd,59);
cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl;
son son2(&pwd,100);
cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl;
son1 = son2;
cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl;
son son3(son1);
cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl;
son son4;
cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl;
return 0;
}
脑图:
https://www.mubu.com/doc/4Sp6apfRbQz
https://www.mubu.com/doc/6ZDW8gtyIQz