当前位置: 首页 > article >正文

9.24 C++ 常成员,运算符重载

//my_string.cpp
#include "my_string.h"
#include <iostream>
#include <cstring>

using namespace std;



    My_string::My_string():size(15)
    {
        this->ptr = new char[size];
        this->ptr[0] = '\0';            //表示串为空串
        this->len = 0;
    }


    //有参构造
    My_string::My_string(const char* src)
    {
        this->len=strlen(src);
        this->size=len+1;
        this->ptr=new char[size];
        strcpy(this->ptr,src);
    }
    My_string::My_string(int num, char value):size(num+1),len(num)
    {
        this->ptr=new char[size];
        memset(this->ptr,value,num);
        this->ptr[num]='\0';
    }
    //拷贝构造
    My_string::My_string(const My_string &other)
    {
        this->len=other.len;
        this->size=other.size;
        this->ptr=new char[size];
        strcpy(this->ptr,other.ptr);
    }
    //拷贝赋值
    My_string &My_string::operator=(const My_string &other)
    {
        if(this!=&other)
        {
            delete[] this->ptr;
            this->len=other.len;
            this->size=other.size;
            this->ptr=new char[size];
            strcpy(this->ptr,other.ptr);
        }
        return *this;
    }
    //析构函数
    My_string::~My_string()
    {
        delete [] this->ptr;
    }
    //判空
    bool My_string::empty() const
    {
        return len==0;
    }
    //尾插
    void My_string::push_back(char value)
    {
        if((len+1)>=size)
        {
            resize(2*size);
        }
        this->ptr[len++]=value;
        this->ptr[len]='\0';
    }
    //尾删
    void My_string::pop_back()
    {
        if(len>0)
        {
            this->ptr[len-1]='\0';
            len--;
        }
    }
    //at函数实现
    char &My_string::at(int index)
    {
        if(index>=0&&index<len)
        {
            return this->ptr[index];
        }
    }
    //清空函数
    void My_string::clear()
    {
        this->len=0;
        this->ptr[0]='\0';
    }
    //返回C风格字符串
    char *My_string::data() const
    {
        return this->ptr;
    }
    //返回实际长度
    int My_string::get_length()
    {
        return this->len;
    }
    //返回当前最大容量
    int My_string::get_size()
    {
        return this->size;
    }

    //  +
    My_string My_string::operator+(const My_string &other)const
    {
        My_string temp(this->ptr);
        temp+=other;
        return temp;
    }
    //[]
    char &My_string::operator[](int index)
    {
        return this->ptr[index];
    }

    // >
    bool My_string::operator>(const My_string &other) const
    {
        return strcmp(this->ptr,other.ptr)>0;
    }

    // <
    bool My_string::operator<(const My_string &other) const
    {
        return strcmp(this->ptr,other.ptr)<0;
    }

    // ==

    bool My_string::operator==(const My_string &other) const
    {
        for(int i=0;i<len;i++)
        {
            if(ptr[i]!=other.ptr[i])
            {
                return false;
            }
        }
        return true;
    }
    // !=
    bool My_string::operator!=(const My_string &other) const
    {
        return !(*this==other);
    }

    // >=
    bool My_string::operator>=(const My_string &other) const
    {
        return !(*this<other);
    }

    // <=
    bool My_string::operator<=(const My_string &other) const
    {
        return !(*this>other);
    }

    My_string &My_string::operator+=(const My_string &other)
    {
        for(int i=0;i<other.len;i++)
        {
            push_back(other.ptr[i]);
        }
        return *this;
    }

    My_string &My_string::operator+=(char value)
    {
        push_back(value);
        return *this;
    }


    std::ostream &operator<<(std::ostream &os, const My_string &str)
    {
        os << str.data();  // 输出字符串内容
        return os;
    }

    std::istream &operator>>(std::istream &is, My_string &str)
    {
        char temp[1024];
        is>>temp;
        str=My_string(temp);
        return is;
    }

    //君子函数:二倍扩容
    void My_string::resize(int new_size)
    {
        if (new_size > size) {
        char *new_ptr = new char[new_size];
        strcpy(new_ptr, this->ptr);  // 复制旧数据
        delete[] this->ptr;
        this->ptr = new_ptr;
        this->size = new_size;
        }
    }
//my_stack.cpp
#include <iostream>

class My_stack
{
private:
    int *data;
    int maxsize;
    int top_index;
    
public:
    My_stack(int max=10):maxsize(max),top_index(-1)
    {
        data=new int[maxsize];
    }
    
    ~My_stack()
    {
        delete [] data;
    }
    
    My_stack(const My_stack &other):maxsize(other.maxsize),top_index(other.top_index)
    {
        data=new int[maxsize];
        for(int i=0;i<=top_index;i++)
        {
            data[i]=other.data[i];
        }
    }
    
    My_stack& operator=(const My_stack & other)
    {
        delete [] data;
        maxsize=other.maxsize;
        top_index=other.top_index;
        data=new int[maxsize];
        for(int i=0;i<=top_index;i++)
        {
            data[i]=other.data[i];
        }
        return *this;
    }
    
    bool empty()const
    {
        return top_index==-1;
    }
    
    int &top()
    {
        return data[top_index];
    }
    
    int size()
    {
        return top_index+1;
    }
    
    void push(int value)
    {
        data[++top_index]=value;
    }
    
    void pop()
    {
        --top_index;
    }
};

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}


http://www.kler.cn/news/323547.html

相关文章:

  • 人工智能-机器学习-深度学习-分类与算法梳理
  • qt 模仿简易的软狗实现
  • Java NIO 全面详解:掌握 `Path` 和 `Files` 的一切
  • Keysight 下载信源 Visa 指令
  • 蓝桥杯模块二:数码管的静态、动态实现
  • 电脑录屏怎么录视频和声音?苹果macOS、windows10都可以用的原神录屏工具来啦
  • 【JAVA】算法笔记
  • Linux用户管理
  • 面试遇到的质量体系10个问题(深度思考)
  • 论文阅读 | 可证安全隐写(网络空间安全科学学报 2023)
  • 神经网络(二):卷积神经网络
  • Vscode 远程切换Python虚拟环境
  • 解决Android中使用jdk 9以上中的某个类(AbstractProcessor)但是无法导入的问题
  • Java中通过方法上注解实现入参校验
  • 计算机毕业设计 在线问诊系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • C++那些你不得不知道的(2)
  • .NET 控制台应用程序连接 MySQL 数据库实现增删改查
  • mysql数据库设置主从同步
  • 自动驾驶电车难题的康德式道德决策
  • 黑马头条day6-kafka及异步通知文章上下架
  • Spring 全家桶使用教程 —— 后端开发从入门到精通
  • C#——switch案例讲解
  • 计算机毕业设计 校园失物招领网站的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 58 深层循环神经网络_by《李沐:动手学深度学习v2》pytorch版
  • 【论文写作】描述一个模型比另一个模型效果好时
  • sentinel原理源码分析系列(二)-动态规则和transport
  • 如何在openEuler上安装和配置openGauss数据库
  • linux编辑文件保存退出的实操讲解
  • JVM基本了解
  • 神经网络激活函数