C++DAY5

该文展示了如何在C++中创建一个名为mystring的自定义字符串类,实现了包括构造函数、拷贝构造函数、析构函数、拷贝赋值运算符以及比较和字符串操作等方法。此外,还涉及了继承的概念,通过Son类从Father类派生并实现相应的构造函数和拷贝赋值运算符。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <cstring>

using namespace std;

class mystring
{
private:
    char *str;
    int len;
public:
    mystring():len(100)
    {
        str = new char[len];
        strcpy(str,"");
    }
    mystring(const char *s)
    {
        len = strlen(s);
        str = new char[len+1];
        strcpy(str,s);
    }

    //拷贝构造
    mystring(const mystring & other):str(new char (*(other.str))),len(other.len)
    {

    }
    //析构构造
    ~mystring()
    {
        delete str;
        cout<<"析构函数"<<"  "<<this<<endl;
    }

    void show()
    {
        cout << "str = " << str << endl;
    }

    //拷贝赋值函数
    mystring &operator = (const mystring &other)
    {
        if(this != &other)
        {
            delete this->str;
            this->len = other.len;
            this->str = new char[len+1];
            strcpy(str,other.str);
        }
        return *this;
    }

    //判空函数
    bool empty()
    {
       if(0 == *str)
       {
           cout << "空" << endl;
           return false;
       }
       else
       {
           cout << "不空" << endl;
           return true;
       }
    }

    //size函数
    int size()
    {
        int num = 0;
        char *p = str;
        while(*p != 0)
        {
            num++;
            p++;
        }
        return num;
    }

    //c_str函数
    char *c_str()
    {
        return str;
    }

    //at函数
    char &at(int pos)
    {
       return str[pos];
    }

    //加号运算符重载
    const mystring operator + (const mystring &r)const
    {
        mystring temp;
        strcat(this->str,r.str);
        strcpy(temp.str,this->str);
        temp.len = this->len + r.len;
        return temp;
    }

    //加等于运算符重载
    mystring &operator += (const mystring &r)
    {
        strcat(this->str,r.str);
        this->len += r.len;
        return *this;
    }

    //关系运算符>重载
    bool operator > (const mystring &r) const
    {
        int n = strcmp(this->str,r.str);
        return n>0;
    }

    //关系运算符<重载
    bool operator < (const mystring &r) const
    {
        int n = strcmp(this->str,r.str);
        return n<0;
    }

    //关系运算符==重载
    bool operator == (const mystring &r) const
    {
        int n = strcmp(this->str,r.str);
        return n=0;
    }
    
    //关系运算符!=重载
    bool operator != (const mystring &r) const
    {
        int n = strcmp(this->str,r.str);
        return n!=0;
    }
    
    //插入运算符重载
    friend ostream & operator << (ostream &l,const mystring &r);
    
    //提取运算符重载
    friend istream & operator >> (istream &l,mystring &r);
    
    //方括号运算符重载
    char &operator [] (int pos)
    {
        return str[pos];
    }
};

ostream & operator <<(ostream &l, const mystring &r)
{
    l << r.str;
    return  l;
}

istream & operator >>(istream &l,mystring &r)
{
    l >> r.str;
    r.len = strlen(r.str)+1;
    return l;
}

 




int main()
{
    char *s1 = "hello";
    mystring s2(s1);
    s2.show();
    s2.empty();
    cout << "size = " << s2.size() << endl;
    mystring s3("world");
    s3.show();
    mystring s4;
    s4 = s2+s3;
    s4.show();
    cout << "size = " << s4.size() << endl;
    s4 += s3;
    s4.show();
    cout << "size = " << s4.size() << endl;
    if(s4>s3)
    {
        cout << "yes" << endl;
    }
    else
    {
        cout << "no" << endl;
    }
    if(s4<s3)
    {
        cout << "yes" << endl;
    }
    else
    {
        cout << "no" << endl;
    }
    if(s4==s3)
    {
        cout << "yes" << endl;
    }
    else
    {
        cout << "no" << 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(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;
    }
    //子类析构函数
    ~Son() {cout<<"Son::析构函数"<<endl;}

    void show()
    {
        cout << "name = " << name << "  toy = " << toy << endl;
    }
};



int main()
{
    Son s1;
    Son s2("迪迦","卡面来打");
    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、付费专栏及课程。

余额充值