#include <iostream>
#include <cstring>
using namespace std;
class mystring
{
private:
char *str; //记录c风格的字符串
int size; //记录字符串的实际长度
public:
//无参构造
mystring():size(10)
{
str=new char[this->size];
strcpy(str,""); //字符串设空
}
//有参构造
mystring(const char *s)
{
size=strlen(s);
str=new char[size+1];
strcpy(str,s);
}
//拷贝构造(深拷贝)
mystring(const mystring &other):
str(new char[other.size+1]),
size(other.size)
{
strcpy(this->str,other.str);
cout<<"mystring::拷贝构造"<<endl;
}
//析构函数
~mystring()
{
delete []str; //释放指针成员
str=NULL;
cout<<"mystring::析构函数"<<endl;
}
//拷贝赋值函数
mystring& operator=(const mystring &other)
{
if(this != &other) //防止自己给自己赋值
{
this->size = other.size;
this->str = new char[other.size+1]; //给指针成员开辟空间
strcpy(this->str,other.str); //赋值
}
cout<<"mystring::拷贝赋值函数"<<endl;
return *this; //返回自身的引用
}
//判空
bool empty()
{
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);
}
//加号运算符重载 +
const mystring operator+(const mystring &R) const
{
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++) //判断size内大小
{
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
{
for(int i=0;i<this->size;i++) //判断size内大小
{
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
{
for(int i=0;i<this->size;i++) //判断size内大小
{
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++) //判断size内大小
{
if(*(this->str+i)<=*(R.str+i))
return true;
else
return false;
}
return false;
}
// ==
bool operator == (const mystring &R) const
{
if(this->size!=R.size)
return false;
for(int i=0;i<this->size;i++) //判断size内大小
{
if(*(this->str+i) != *(R.str+i))
return false;
}
return false;
}
//!=
bool operator != (const mystring &R) const
{
if(this->size!=R.size)
return true;
for(int i=0;i<this->size;i++) //判断size内大小
{
if(*(this->str+i) != *(R.str+i))
return true;
}
return false;
}
friend ostream &operator << (ostream &L,const mystring &R);
friend ostream &operator >> (ostream &L,const mystring &R);
};
// <<
ostream &operator << (ostream &L,const mystring &R)
{
L<<R.str<<endl;
return L;
}
// >>
ostream &operator >> (ostream &L,const mystring &R)
{
L>>R.str;
return L;
}
int main()
{
mystring s1("hi");
mystring s2("world");
mystring s3;
s3=s1+s2;
cout<<"s3 = "<<s3.c_str()<<endl;
cout<<endl;
mystring s4;
s4+=s2;
cout<<"s4 = "<<s4.c_str()<<endl;
if(s1>=s2)
{
cout<<"s1>s2"<<endl;
}else
cout<<"s2>s1"<<endl;
if(s3<=s4)
{
cout<<"s3<=s4"<<endl;
}else
cout<<"s4<=s3"<<endl;
return 0;
}
继承:
#include <iostream>
using namespace std;
class Father
{
protected:
string name;
public:
//无参构造
Father() {cout<<"Father::无参构造"<<endl;}
//有参构造
Father(string n):name(n) {cout<<"Father::有参构造"<<endl;}
//拷贝构造
Father(const Father&other):name(other.name) {cout<<"Father::拷贝构造"<<endl;}
//拷贝赋值
Father& operator=(const Father&other)
{
if(this != &other)
{
this->name = other.name;
}
cout<<"Father::拷贝赋值函数"<<endl;
return *this;
}
~Father() {cout<<"Father::析构函数"<<endl;}
};
class son:public Father
{
private:
string toy; //玩具
public:
//子类无参构造
son() {cout<<"son::无参构造"<<endl;}
//子类有参构造
son(string n,string t):Father(n),toy(t)
{cout<<"Son::有参构造"<<endl;}
//子类析构函数
~son() {cout<<"son::析构函数"<<endl;}
//子类的拷贝构造函数
son(const son &other):Father(other),toy(other.toy)
{
cout<<"son::拷贝构造"<<endl;
}
//子类的拷贝赋值函数
son &operator=(const son &other)
{
if(this != &other)
{
//显性调用父类的拷贝赋值函数
Father::operator=(other);
this->toy = other.toy;
}
cout<<"son::拷贝赋值函数"<<endl;
return *this;
}
void show()
{
cout<<"name = "<<name<<" toy = "<<toy<<endl;
}
};
int main()
{
son s1; //无参构造
cout<<endl;
son s2("zhangsan","car"); //有参构造
cout<<endl;
son s3(s2); //拷贝构造
s1=s3;
s1.show();
cout<<endl;
return 0;
}
思维导图: