【C++】Day5

本文展示了如何在C++中实现一个名为myString的类,该类模仿了标准库中的string类,包括构造函数、析构函数、拷贝构造和拷贝赋值。同时,文章涵盖了运算符重载,如[]、+、+=、>等,以及输入输出流操作符重载。此外,还介绍了类的继承概念,通过一个Father和Son类的例子说明了成员变量和函数的继承与拷贝构造、赋值操作。
摘要由CSDN通过智能技术生成

1. 仿照string类,实现Mystring类

运算符重载:[] () cin cout

#include <iostream>
#include <cstring>

using namespace std;

class myString
{
    private:
        char *str;          //记录c风格的字符串
        int size;           //记录字符串的实际长度
    public:
        //无参构造函数
        myString():size(10)
        {
            str = new char[size];   //构造出一个长度为10的字符串
            strcpy(str,"");         //赋值为空串
        }
        //有参构造函数
        myString(const char *s)     //string  s("hello world")
        {
            size = strlen(s);
            str = new char[size+1];
            strcpy(str, s);
        }

        //拷贝构造函数
        myString(const myString& other)    // 深拷贝
        {
            size = strlen(other.str);
            str = new char(size+1);
            strcpy(str, other.str);
            cout<<"myString::拷贝构造"<<endl;
         }


        //析构函数
        ~myString()
        {
            delete []str;
            str = nullptr;
        }

        //拷贝赋值函数
        myString& operator=(const myString &other)
        {
            if(this != &other)   //防止自己给自己赋值
            {
                this->size = strlen(other.str);
                this->str = new char(size+1);//给指针成员单独开辟内存空间
                strcpy(this->str, other.str);
            }
            return *this;       //返回自身的引用
        }

        //判空函数
        bool empty()
        {
            return !strlen(this->str);
        }

        //size函数
        unsigned int stringsize()
        {
            return strlen(this->str);
        }

        //c_str函数
        const char *c_str()
        {
            cout<<this->str<<endl;
            return this->str;
        }

        //at函数
        char &at(int pos)
        {
            if(pos<0 || pos>this->size)
            {
                cout<<"输入越界"<<endl;
            }
            return str[pos];
        }

        //成员函数版实现 [] 运算符重载
        char operator[](const int index)
        {
            if(index<0 || index>this->size)
            {
                cout<<"输入越界"<<endl;
            }
            return this->str[index];
        }

       //成员函数版实现+运算符重载
       const myString operator+(const myString &R)const
        {
           myString temp;   //  调用无参构造,长度只有10
           temp.size=this->size+R.size;
           temp.str=new char[temp.size+1];
           strcpy(temp.str,this->str);
           strcat(temp.str,R.str);
           return temp;
        }


      //成员函数版实现+=运算符重载
      myString& operator+=(const myString &R)
       {
           strcat(this->str,R.str);
           return  *this;
       }

       //关系运算符重载(>)aaaaaaaaaa
       bool operator>(const myString &R)const
       {
           return strcmp(this->str,R.str)>0;
       }

       friend ostream& operator<<(ostream& out, myString& other);
       friend istream& operator>>(istream& cin, myString& other);
};

//实现一个<<运算符重载;
ostream& operator<<(ostream& out, myString& other)
{
    out << other.str;
    return out;
}

//实现一个>>运算符重载;
istream& operator>>(istream& in, myString& other)
{
    in >> other.str;
    return cin;
}

int main()
{
    myString c1("hello");
    c1.c_str();

    myString c2("world world world world");
    c2.c_str();

    myString c3;
    c3=c1+c2;
    c3.c_str();

    c1+=c2;
    c1.c_str();

    if(c1>c2)   // >
    {
        cout<<"yes"<<endl;
    }
    else
    {
        cout<<"no"<<endl;
    }

    cout<<c1.at(1)<<endl;   //at
    cout<<c1[1]<<endl;  //[]

    myString c4;
    cin>>c4;   //cin
    cout<<c4<<endl;  //cout


    return 0;
}

2.继承

#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;     //调用无参构造
    Son s2("zhangsan","car");  //调用有参构造
    Son s3(s2); //调用拷贝构造

    s1=s3;      //调用拷贝赋值函数
    s1.show();

    return 0;
}

3.思维导图

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值