7.20 作业 c++

文章展示了如何在C++中创建一个名为mystring的自定义字符串类,实现了包括构造函数、拷贝构造、析构函数、拷贝赋值等核心功能,并重载了比较和加法运算符。此外,还给出了一个继承示例,展示了一个名为son的子类如何继承并扩展Father类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

思维导图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值