C++ day4

1、仿照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,"");               //赋值为空串
    }

    //有参构造
    myString(const char *s)          //string  s("hello world")
    {
        size = strlen(s);
        str = new char[size+1];
        strcpy(str, s);
    }

    //拷贝构造
    myString(const myString &other):str(other.str), size(other.size)
    {
        str = new char[size];
        str = new char[size+1];
        strcpy(str, other.str);
    }

    //析构函数
    ~myString()
    {
        delete []str;              //释放成员指针的空间

        cout<<"Stu::析构函数:"<<this<<endl;
    }

    //拷贝赋值函数
    myString & operator=(const myString &other)
    {
        if(this != &other)
        {
            this->str = other.str;
            this->size = other.size;

            //判断原来指针空间释放被清空
            if(this->str != NULL)
            {
                delete this->str;
            }
            this->str = new char(*other.str);
        }
        cout<<"Stu:: 拷贝赋值函数"<<endl;

        //返回自身引用
        return  *this;
    }
    //判空函数
    bool empty()const
    {
        if(size == 0)
        {
            return true;
        }
        return false;
    }

    //size函数
    int my_size()const
    {
        return size;
    }

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

    //at函数
    char &at(int pos)
    {
        if (pos >= 0 && pos < size)
        {
            return str[pos];
        }
    }

    //加号运算符重载
    const myString operator+ (const myString &R)const
    {
        myString c;
        c.size = this->size + R.size;
        c.str = new char[c.size+1];
        strcpy(c.str, str);
        strcat(c.str, R.str);

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

        return *this;
    }
    //关系运算符重载(>)
    bool operator> (const myString &R)const
    {
        return (strcmp(str, R.str) > 0);
    }
    //中括号运算符重载
    char &operator[](int pos)
    {
        return at(pos);
    }
};

int main()
{
    myString s1("good");
    myString s2("day");

    myString s3 = s1 + s2;
    cout<<s3.c_str()<<endl;

    s1 += "morning";
    cout<<s1.c_str()<<endl;

    if(s1 >s2)
    {
        cout<<s1.c_str()<<"大于"<<s2.c_str()<<endl;
    }

    return 0;
}

运行结果:

cpp 复制代码
goodday
Stu::析构函数:0x61fdf0
Stu::析构函数:0x61fe00
goodmorning
goodmorning大于day
Stu::析构函数:0x61fdc0
Stu::析构函数:0x61fdd0
Stu::析构函数:0x61fde0

2、思维导图

相关推荐
imX2G9 小时前
爆破小游戏2.0
c++
Cx330❀9 小时前
【优选算法必刷100题】第41-42题(模拟):Z 字形变换,外观数列
c++·算法
Cx330❀10 小时前
【优选算法必刷100题】第038题(位运算):消失的两个数字
开发语言·c++·算法·leetcode·面试
燃于AC之乐10 小时前
我的算法修炼之路--5——专破“思维陷阱”,那些让你拍案叫绝的非常规秒解
c++·算法·贪心算法·bfs·二分答案·扩展域并查集·动态规划(最长上升子序列)
艾莉丝努力练剑10 小时前
【优选算法必刷100题】第021~22题(二分查找算法):山脉数组的峰顶索引、寻找峰值
数据结构·c++·算法·leetcode·stl
C++ 老炮儿的技术栈11 小时前
C/C++ 中 inline(内联函数)和宏定义(#define)的区别
开发语言·c++·git·算法·机器人·visual studio
yeflx12 小时前
CMake+CUDA
c++
Word码13 小时前
[C++语法]-vector(用法详解及实现)
开发语言·c++
安全二次方security²13 小时前
CUDA C++编程指南(7.15&16)——C++语言扩展之内存空间谓词和转化函数
c++·人工智能·nvidia·cuda·内存空间谓词函数·内存空间转化函数·address space
L1869245478213 小时前
Win 下 PCL部分函数析构崩溃问题总结
c++·计算机视觉·3d·pcl