完善myString类
//单目运算符重载
const myString operator- ()const;
//自增自减函数运算符重载
const myString operator ++ ();
const myString operator ++ (int);
//插入运算符作友元函数(友元)
friend ostream &operator << (ostream &L,const myString &R);
//中括号运算符重载
char & operator[](const int index);
//单目运算符重载
const myString myString::operator-() const
{
myString temp;
temp.size = -this->size;
return temp;
}
//自增自减函数运算符重载(++)
const myString myString::operator ++()
{
++this->size;
}
const myString myString::operator ++(int)
{
myString temp;
temp.size = this->size++;
}
//中括号运算符重载
char &myString::operator[](const int index)
{
if(index == 1)
return str[index];
}
//插入运算符重载函数(全局函数版)
ostream &operator << (ostream &L,const myString &R)
{
L<<R.str<<endl;
return L;
}
继承的相关特殊函数
class Father
{
protected:
string name;
public:
Father()
{ cout<<"父::无参构造"<<endl; }
Father(string n):name(n)
{ cout<<"父::有参构造"<<endl; }
Father(const Father&other):name(other.name)
{ cout<<"父::拷贝构造"<<endl; }
Father &operator = (const Father &other)
{
if(this != &other)
this->name = other.name;
cout<<"父::拷贝赋值"<<endl;
return *this;
}
~Father()
{ cout<<"父::析构函数"<<endl; }
};
class Son : public Father
{
private:
string namae;
public:
Son() { cout<<"子::无参构造"<<endl; }
Son(string n,string m):Father(n),namae(m)
{ cout<<"Son::有参构造"<<endl; }
~Son() { cout<<"子::析构函数"<<endl; }
Son(const Son &other):Father(other),namae(other.namae)
{ cout<<"子::拷贝构造"<<endl; }
Son &operator = (const Son &other)
{
if(this != &other)
{
Father::operator=(other);
this->namae = other.namae;
}
cout<<"子::拷贝赋值"<<endl;
return *this;
}
void show()
{
cout<<"name = "<<name<<endl;
cout<<"namae = "<<namae<<endl;
}
};
思维导图