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

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;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • python爬虫初体验(三)——将网页数据导出csv和excel文件
  • Zero-shot、One-shot、Few-shot 这三种学习分别是什么?
  • 8.11Zero Crossing Detection (零交叉检测)
  • python全栈学习记录(十七)logging、json与pickle、time与datatime、random
  • 大数据新视界 --大数据大厂之大数据实战指南:Apache Flume 数据采集的配置与优化秘籍
  • SSM+Vue共享单车管理系统
  • MySQL 生产环境性能优化
  • 【数据结构初阶】栈接口实现及经典OJ题超详解
  • 某易易盾验证码逆向
  • 10. 排序
  • 【表达式求值算法】拆解复杂问题:实现计算器
  • Rust调用tree-sitter解析C语言
  • 11 - TCPClient实验
  • 中断合并参数coalesce_params解释
  • StringReader 使用 JAXB自动将 XML 数据映射到 Java 对象
  • IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
  • 【React系列】如何构建React应用程序
  • Akka系列(七):Actor持久化之Akka persistence
  • Android 控件背景颜色处理
  • Android交互
  • android图片蒙层
  • Brief introduction of how to 'Call, Apply and Bind'
  • HomeBrew常规使用教程
  • iBatis和MyBatis在使用ResultMap对应关系时的区别
  • iOS编译提示和导航提示
  • JavaScript异步流程控制的前世今生
  • Lucene解析 - 基本概念
  • MaxCompute访问TableStore(OTS) 数据
  • node-glob通配符
  • node入门
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 前端面试总结(at, md)
  • 深入浅出Node.js
  • 什么是Javascript函数节流?
  • 思维导图—你不知道的JavaScript中卷
  • zabbix3.2监控linux磁盘IO
  • ​学习笔记——动态路由——IS-IS中间系统到中间系统(报文/TLV)​
  • # Apache SeaTunnel 究竟是什么?
  • #NOIP 2014#Day.2 T3 解方程
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • $.ajax()
  • (C语言)逆序输出字符串
  • (MTK)java文件添加简单接口并配置相应的SELinux avc 权限笔记2
  • (二开)Flink 修改源码拓展 SQL 语法
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (力扣)1314.矩阵区域和
  • (利用IDEA+Maven)定制属于自己的jar包
  • (论文阅读40-45)图像描述1
  • (一)、软硬件全开源智能手表,与手机互联,标配多表盘,功能丰富(ZSWatch-Zephyr)
  • (转)IIS6 ASP 0251超过响应缓冲区限制错误的解决方法
  • (转)linux下的时间函数使用
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • .net core开源商城系统源码,支持可视化布局小程序
  • .Net 代码性能 - (1)
  • .net 怎么循环得到数组里的值_关于js数组