C++ Day4

目录

[仿照string类,完成myString 类](#仿照string类,完成myString 类)

思维导图


仿照string类,完成myString 类

cpp 复制代码
#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,"");         //赋值为空串
            cout<<"无参构造"<<endl;
        }
        //有参构造
        myString(const char *s)          //string  s("hello world")
        {
            size = strlen(s);
             str = new char[size+1];
             strcpy(str, s);
             cout<<"有参构造"<<endl;
        }

        //拷贝构造
        myString(const myString &other)
        {
            this->str=new char[other.size+1];
            strcpy(str,other.str);
            this->size=other.size;
            cout<<"拷贝构造"<<endl;
        }

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

        //拷贝赋值函数
        myString &operator=(const myString &other)
        {
            if(this!=&other)
            {
                this->size=other.size;
                if(this->str!=NULL)
                {
                    delete this->str;
                }
                this->str=new char[other.size+1];
                strcpy(str,other.str);
                cout<<"拷贝赋值"<<endl;
            }
            return *this;
        }

        //判空函数
        bool empty()
        {
            return 0==size;
        }

        //size函数
        int my_size()
        {
            cout<<strlen(str)<<endl;
            return strlen(str);
        }

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


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

        //加号运算符重载
        const myString operator+(const myString &other)const
        {
            myString c;
            c.str=new char[size+other.size];
            strcpy(c.str,str);
            strcat(c.str,other.str);
            c.size=size+other.size;
            return c;
        }

        //加等于运算符重载
        myString &operator+=(const myString &other)
        {
            strcat(str,other.str);
            size+=other.size;
            return *this;
        }

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

        //中括号运算符重载
        char & operator[](int i)
        {
            if(i >= 0 && i< size)
            {
                return this->str[i];
            }
            else
            {
                cout<<"数组越界"<<endl;
            }
        }

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

int main()
{
    char arr[10]="hello";
    myString s1(arr);  //有参构造

    myString s2=s1;   //拷贝构造

    myString s3;   //无参构造
    s3=s2;    //拷贝赋值函数

    s1.my_size();
    s2.my_size();    //size
    s3.my_size();

    s1.at(1)='o';      //at()函数
    s1.show();

    myString s4=s1+s2;   //加法运算符重载
    s4.show();

    myString s5("world");   //有参构造
    s4+=s5;      //+=运算符重载
    s4.show();
    s4.my_size();

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

    cout<<s4[6]<<endl;

    return 0;
}

思维导图

相关推荐
虚拟之1 小时前
36、stringstream
c++
我很好我还能学1 小时前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
蓝婷儿1 小时前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
渣渣盟2 小时前
基于Scala实现Flink的三种基本时间窗口操作
开发语言·flink·scala
糯米导航2 小时前
Java毕业设计:办公自动化系统的设计与实现
java·开发语言·课程设计
糯米导航2 小时前
Java毕业设计:WML信息查询与后端信息发布系统开发
java·开发语言·课程设计
南岩亦凛汀2 小时前
在Linux下使用wxWidgets进行跨平台GUI开发
c++·跨平台·gui·开源框架·工程实战教程
MessiGo2 小时前
Javascript 编程基础(5)面向对象 | 5.1、构造函数实例化对象
开发语言·javascript·原型模式
大霞上仙2 小时前
nonlocal 与global关键字
开发语言·python
曦月逸霜3 小时前
第34次CCF-CSP认证真题解析(目标300分做法)
数据结构·c++·算法