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

C++学习9.24

1、 将昨天的My_string类中的所有能重载的运算符全部进行重载

+、[] 、>、=、>)

头文件

#ifndef MY_STRING_H
#define MY_STRING_H


#endif // MY_STRING_H

#include <iostream>
#include <cstring>
//#include <stdexcept>

using namespace std;

class My_string
{
private:
    char *ptr;         //指向字符数组的指针
    int size;           //字符串的最大容量
    int len;            //字符串当前容量


public:
    //加法
    const My_string operator+(const My_string &R)const;

    const My_string operator+=(const My_string &R);

    char operator[](int index)const;

    friend ostream & operator<< (ostream &L,const My_string &R);

    friend istream & operator>> (istream &L,My_string &R);

    bool  operator>(My_string &R);

    bool  operator<(My_string &R);

    bool  operator==(My_string &R);

    bool  operator<=(My_string &R);

    bool  operator>=(My_string &R);

    bool  operator!=(My_string &R);

    //无参构造
    My_string();


    //有参构造
    My_string(const char* src);
    My_string(int num, char value);
    //拷贝构造
    My_string(const My_string &other);
    //拷贝赋值
    My_string& operator= (const My_string &other);
    //析构函数
    ~My_string();
    //判空
    bool  empty();
    //尾插
    void push_back(char value);
    //尾删
    void pop_back();
    //at函数实现
    char &at(int index);
    //清空函数
    void clear();
    //返回C风格字符串
    char *data();
    //返回实际长度
    int get_length();
    //返回当前最大容量
    int get_size();
};

源文件

#include "My_string.h"
//加法
const My_string My_string::operator+(const My_string &R)const
{
    My_string result;
    result.len = len + R.len;
    result.size = size + R.size;
    result.ptr = new char[result.size];
    strcpy(result.ptr,ptr);
    strcat(result.ptr,R.ptr);

    return result;

}
//加等
const My_string My_string::operator+=(const My_string &R)
{
    *this = *this + R;

    return *this;
}

char My_string::operator[](int index)const
{
    if(index<0 || index>len)
    {
        return -1;
    }
    return ptr[index];
}

ostream & operator<< (ostream &L,const My_string &R)
{
    L<<R.ptr<<endl;

    return L;
}

istream & operator>> (istream &L,My_string &R)
{
    char buff[1024];
    L >> buff;
    R.len = strlen(buff);
    R.size = R.len + 1;
    delete [] R.ptr;
    R.ptr = new char[R.size];
    strcpy(R.ptr,buff);


    return L;
}

bool My_string:: operator>(My_string &R)
{
    return strcmp(ptr,R.ptr)>0;
}

bool My_string:: operator<(My_string &R)
{
    return strcmp(ptr,R.ptr)<0;
}

bool My_string:: operator==(My_string &R)
{
    return strcmp(ptr,R.ptr)==0;
}

bool My_string:: operator<=(My_string &R)
{
    return strcmp(ptr,R.ptr)<=0;
}

bool My_string:: operator>=(My_string &R)
{
    return strcmp(ptr,R.ptr)>=0;
}

bool My_string:: operator!=(My_string &R)
{
    return strcmp(ptr,R.ptr)!=0;
}






//无参构造
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)
{
    len = strlen(src);
    size = len+1;
    ptr = new char[size];
    strcpy(ptr,src);
}
My_string:: My_string(int num, char value): size(num+1),len(num)
{
    ptr = new char[size];
    for(int i=0;i<num;i++)
    {
        ptr[i] = value;
    }
    ptr[num] = '\0';
}
//拷贝构造
My_string:: My_string(const My_string &other):size(other.size),len(other.len)
{
    ptr = new char[size];
    strcpy(ptr,other.ptr);
}
//拷贝赋值
My_string & My_string ::operator =(const My_string &other)
{
    if(this ==&other)
    {
        return *this;
    }
    delete [] ptr;

    size = other.size;
    len = other.len;
    ptr = new char[size];
    strcpy(ptr,other.ptr);

    return *this;
}
//析构函数
My_string:: ~My_string()
{
    delete [] ptr;
}
//判空
bool My_string:: empty()
{
    return len ==0;
}
//尾插
void My_string:: push_back(char value)
{
    if(len+1>=size)
    {
        size *=2;
        char* new_ptr = new char[size];
        strcpy(new_ptr,ptr);
        delete []ptr;
        ptr = new_ptr;
    }

    ptr[len] = value;
    len++;
    ptr[len] = '\0';
}
//尾删
void My_string:: pop_back()
{
    if(len>0)
    {
        len--;
        ptr[len]='\0';
    }
}
//at函数实现
char&My_string:: at(int index)
{
    if(index<0 ||index>=len)
    {
        //throw  out_of_range("超出");
    }
    return ptr[index];
}
//清空函数
void My_string:: clear()
{
    len = 0;
    ptr[0] = '\0';
}
//返回C风格字符串
char*My_string:: data()
{
    return ptr;
}
//返回实际长度
int My_string:: get_length()
{
    return len;
}
//返回当前最大容量
int My_string:: get_size()
{
    return size;
}

2、 思维导图


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

相关文章:

  • git本地分支落后于远程分支,因此推送被拒绝怎么办?
  • nodejs逐字读取文件示例
  • Python中的`super()`函数:掌握面向对象编程的艺术
  • PHP“===”的意义
  • 工具类:JWT
  • 【AI学习】Lilian Weng:Extrinsic Hallucinations in LLMs(LLM 的外在幻觉)
  • TS-AI:一种用于多模态个体化脑区划分的深度学习管道,并结合任务对比合成|文献速递-Transformer架构在医学影像分析中的应用
  • 生产环境升级mysql流程及配置主从服务
  • YOLOv8改进 | 主干篇,YOLOv8改进主干网络为华为的轻量化架构GhostNetV1
  • C++ 左值右值引用梳理(一)
  • 蓝桥杯—STM32G431RBT6(RTC时钟获取时间和日期)
  • python 如何引用变量
  • LeetCode 每日一题 最佳观光组合
  • 水波荡漾效果+渲染顺序+简单UI绘制
  • Chromium 屏蔽“缺少 Google API 密钥,因此 Chromium 的部分功能将无法使用。”提示 c++
  • Conda 虚拟环境使用指南,python,anaconda,miniconda
  • MySQL InnoDB 事务commit逻辑分析
  • C++的new关键字
  • 如何在Android上运行Llama 3.2
  • 关于TrustedInstaller权限
  • c++-类和对象-设计立方体类
  • 每天学习一个技术栈 ——【Django Channels】篇(2)
  • ansible实现远程创建用户
  • [BUUCTF从零单排] Web方向 03.Web入门篇之sql注入-1(手工注入详解)
  • Java 编码系列:注解处理器详解与面试题解析
  • Uptime Kuma运维监控服务本地部署结合内网穿透实现远程在线监控
  • PostgreSQL的扩展Citus介绍
  • 非常全面的中考总复习资料-快速提升中考成绩!
  • 总结C/C++中内存区域划分
  • 点餐小程序实战教程14点餐功能