当前位置: 首页 > 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;
}

http://www.lryc.cn/news/445348.html

相关文章:

  • C#设计模式之访问者模式
  • 一次RPC调用过程是怎么样的?
  • 鸭脖变“刺客”,啃不起了
  • 力扣 —— 删除有序数组中的重复项
  • rmdir :删除空文件夹
  • 网络爬虫Request静态页面数据获取
  • 网页聊天——测试报告——Selenium自动化测试
  • mysql5.7常用操作命令手册
  • 前端组件库Element UI 的使用
  • 【C++ 基础数学 】2121. 2615相同元素的间隔之和|1760
  • 从手动测试菜鸟,到自动化测试老司机,实现自动化落地
  • docker zookeeper集群启动报错:Cannot open channel to * at election address /ip:3888
  • 【Linux探索学习】第一弹——Linux的基本指令(上)——开启Linux学习第一篇
  • 3.Vue2结合element-ui实现国际化多语言i18n
  • 整数二分算法和浮点数二分算法
  • 智能回收箱的功能和使用步骤介绍
  • Remix在SPA模式下,出现ErrorBoundary错误页加载Ant Design组件报错,不能加载样式的问题
  • ADB ROOT开启流程
  • 传输层协议 —— TCP协议(上篇)
  • YOLOv8改进,YOLOv8的Neck替换成AFPN(CVPR 2023)
  • 学习大数据DAY59 全量抽取和增量抽取实战
  • YOLOv8——测量高速公路上汽车的速度
  • 在线相亲交友系统:寻找另一半的新方式
  • MySQL 中存储过程参数的设置与使用
  • 2k1000LA 调试HDMI
  • 24年蓝桥杯及攻防世界赛题-MISC-1
  • 前端项目代码开发规范及工具配置
  • 【JVM】JVM执行流程和内存区域划分
  • Python | 读取.dat 文件
  • 信息技术的变革与未来发展的思考