C++入门学习(中)

完善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;
    }
};

思维导图

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值