C++ 作业 day5 7/20

1.完善my_string类,将能够重载的运算符,全部进行重载

#include <iostream>
#include <string.h>
#include <math.h>

using namespace std;

class MyString
{
private:
    //字符串
    char *str=nullptr;
    //最大长度
    int size=15;
    //元素个数
    int len=0;
public:
    //无参构造
    MyString()
    {
        str=new char[size];
        strcpy(str,"");
    }

    //有参构造
    MyString(const char *s)
    {
        this->len=strlen(s);
        if(this->len>=this->size)
        {
            this->size=15*pow(2,(this->len)/15);
        }

        str=new char[this->size];

        strcpy(this->str,s);

    }

    //拷贝构造
    MyString(const MyString &other):
       size(other.size),
       len(other.len)
    {

        this->str=new char[other.size];
        strcpy(this->str,other.str);
    }

    //析构函数
    ~MyString()
    {
        delete []str;
        this->str=nullptr;
    }

    //拷贝赋值函数
    MyString &operator=(const MyString &other)
    {
        //防止自己给自己赋值
        if(this!=&other)
        {
            this->len=other.len;
            this->size=other.size;
            this->str=new char[other.size];
            strcpy(this->str,other.str);
        }

        return *this;
    }

    //判空函数
    bool empty()
    {
        if(this->len==0)
            return true;
        return false;
    }

    //length函数
    int length()
    {
        if(this->str==nullptr||empty())
            return 0;
        return this->len;
    }

    //c_str函数
    char *c_str()
    {
        //返回字符串
        return this->str;
    }

    //at函数
    char &at(int i)
    {
        //越界检查
        if(i<0||i>this->len-1)
        {
            cout<<"越界访问!"<<endl;
            return this->str[len];
        }
        return this->str[i];
    }

    //最大容量
    int capacity()
    {
        return this->size;
    }

    //加号运算符重载
    const MyString operator+(const MyString &other)const
    {
        int len=this->len+other.len;
        char str1[len+1];
        strcpy(str1,this->str);
        MyString s1(strcat(str1,other.str));
        return s1;

    }

    //加等于号重载
    MyString& operator+=(const MyString &other)
    {
         this->len+=other.len;
        if(this->len>=this->size)
        {
                this->size=15*pow(2,(this->len)/15);
        }
        char str1[this->size];
        strcpy(str1,this->str);
        delete []this->str;
        this->str=new char[this->size];
        strcat(str1,other.str);
        strcpy(this->str,str1);
        return *this;

    }
    //关系运算符重载(>)
    bool operator>(const MyString &other)
    {
             if(strcmp(this->str,other.str)>0)
                 return true;
             return false;
    }

    //中括号重载
    char &operator[](const int index)const
    {
        if(index>=this->len)
            return  this->str[len];
        return this->str[index];
    }

    //插入运算符重载(友元)
    friend ostream &operator<<(ostream &o,const MyString &s);

    //提取运算符重载 (友元)
    friend istream &operator>>(istream &i, MyString &s);

    //关系运算符重载(<)
    bool operator<(const MyString &other)
    {
             if(strcmp(this->str,other.str)<0)
                 return true;
             return false;
    }

    //关系运算符重载(==)
    bool operator==(const MyString &other)
    {
             if(strcmp(this->str,other.str)==0)
                 return true;
             return false;
    }

    //关系运算符重载(!=)
    bool operator!=(const MyString &other)
    {
             if(strcmp(this->str,other.str)!=0)
                 return true;
             return false;
    }

    //关系运算符重载(>=)
    bool operator>=(const MyString &other)
    {
             if(strcmp(this->str,other.str)>=0)
                 return true;
             return false;
    }

    //关系运算符重载(<=)
    bool operator<=(const MyString &other)
    {
             if(strcmp(this->str,other.str)<=0)
                 return true;
             return false;
    }

};

//插入运算符重载
ostream &operator<<(ostream &o,const MyString &s)
{
    o<<s.str;
    return o;
}

//提取运算符重载
istream &operator>>(istream &i, MyString &s)
{
    if(s.str!=nullptr)
    {
        delete []s.str;
        s.str=nullptr;
    }
    char buf[1024];
    i>>buf;
    s.len=strlen(buf);
    //判断字符串大小,二倍扩容
    if(s.len>=s.size)
    {
        s.size=15*pow(2,(s.len)/15);
    }
    s.str=new char[s.size];
    strcpy(s.str,buf);
    return i;
}


int main()
{
    MyString s1("hello");
    MyString s2("world");
    MyString s3;
    s3=s1;
    MyString s4(s2);

    cout<<">>重载";
    cin>>s1;
    cout<<"<<重载"<<s1<<endl;
    cout<<"中括号重载"<<s1[0]<<endl;
    cout<<"最大容量"<<s1.capacity()<<endl;
    cout<<"at()重载"<<s3.at(0)<<endl;
    cout<<"c_str()重载"<<s1.c_str()<<endl;
    cout<<"length()重载"<<s3.length()<<endl;
    if(s1.empty())
        cout<<"字符串为空"<<endl;
    else {
        cout<<"字符串不为空"<<endl;
    }

    if(s1>s2)
    {
        cout<<"s1>s2"<<endl;
    }
    else
    {
        cout<<"s1<s2"<<endl;
    }

    s3=s1+s2;
    cout<<s3.at(7)<<endl;
    cout<<"最大容量"<<s3.capacity()<<endl;

    s3=s1;
    cout<<s3.length()<<endl;
    s4+=s2;
    cout<<s4.at(7)<<endl;

    return 0;
}

2> 将继承过程中特殊成员函数相关代码重新实现一遍

#include <iostream>

using namespace std;

class Father
{
protected:
    string name;
public:
    //无参构造
    Father() {cout<<"Father::无参构造"<<endl;}

    //有参构造
    Father(string s): name(s) {cout<<"Father::有参构造"<<endl;}

    //析构函数
    ~Father(){cout<<"析构"<<endl;}

    //拷贝构造
    Father(const Father& other):name(other.name){cout<<"Father::拷贝构造"<<endl;}

    //拷贝赋值
    Father &operator=(const Father& other)
    {
        if(this!=&other)
        {
            name=other.name;
        }
        cout<<"Father::拷贝赋值"<<endl;
        return *this;
    }

};

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);
            toy=other.toy;
        }
        cout<<"Son::拷贝赋值"<<endl;
        return *this;
    }

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

};

int main()
{
    Son s1;//无参构造
    Son s2("zhangsan","car");//有参

    Son s3(s2);//拷贝

    s1=s3;//赋值

    s1.show();
    return 0;
}

3> 完成今日的内容的思维导图绘制

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值