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

C++:类中的特殊关键字,运算重载符

1.My_string类中重载以下的运算符:

+、[] 、>、<、==、>=、<=、!=、+=、输入输出(>>、<<)

主函数:

#include <iostream>
#include "my_string.h"using namespace std;int main()
{My_string s1("cat");My_string s2(" fash");My_string s3=s1+s2;s3.show();cout<<"索引:"<<s3[5]<<endl;My_string s4("beef");My_string s5(" milk");s4+=s5;s4.show();My_string s6("Abc");My_string s7("abc");cout<<(s6>s7)<<endl;cout<<(s6<s7)<<endl;return 0;}

my_string.h

#ifndef MY_STRING_H
#define MY_STRING_H
#include <iostream>
#include <cstring>using namespace std;
class My_string
{
private:char *ptr;int size;int len;
public://无参构造My_string();//有参构造My_string(const char *src);//析构函数~My_string();My_string operator+(My_string &S);char & operator[](int index);bool operator>(const My_string &S) const;bool operator<(const My_string &S) const;bool operator==(const My_string &S) const;bool operator>=(const My_string &S) const;bool operator<=(const My_string &S) const;bool operator!=(const My_string &S) const;My_string & operator+=(const My_string &S);friend ostream& operator<<(ostream &out, const My_string &S);friend istream& operator>>(istream &in, My_string &S);void show();};#endif // MY_STRING_H

my_string.cpp

#include "my_string.h"//无参构造
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=0;for(int i=0;src[i]!='\0';i++){len++;}size=len+1;this->ptr=new char[size];for(int i=0;i<len;i++){ptr[i]=src[i];}ptr[len]='\0';
}//析构函数
My_string::~My_string()
{delete []ptr;//cout<<"析构函数"<<this<<endl;
}//重载 +操作符My_string My_string::operator+(My_string &S)
{int newlen=len+S.len;char *newptr=new char[newlen+1];//分配新内存strcpy(newptr,this->ptr);strcat(newptr,S.ptr);My_string temp(newptr);//创建临时对象delete []newptr; // 释放临时内存return temp;
}//重载 [] 操作符
char & My_string::operator[](int index)
{if(index<0 ||index>=len){exit(EXIT_FAILURE);}else{return ptr[index-1];}
}// 重载 > 操作符
bool My_string::operator>(const My_string &S) const
{return strcmp(this->ptr, S.ptr) > 0;
}// 重载 < 操作符
bool My_string::operator<(const My_string &S) const
{return strcmp(this->ptr, S.ptr) < 0;
}// 重载 == 操作符
bool My_string::operator==(const My_string &S) const
{return strcmp(this->ptr, S.ptr) == 0;
}// 重载 >= 操作符
bool My_string::operator>=(const My_string &S) const
{return strcmp(this->ptr, S.ptr) >= 0;
}// 重载 <= 操作符
bool My_string::operator<=(const My_string &S) const
{return strcmp(this->ptr, S.ptr) <= 0;
}// 重载 != 操作符
bool My_string::operator!=(const My_string &S) const
{return strcmp(this->ptr, S.ptr) != 0;
}
// 重载 += 操作符
My_string & My_string::operator+=(const My_string &S)
{int newlen=len+S.len;char *newptr = new char[newlen+1];strcpy(newptr, this->ptr);strcat(newptr, S.ptr);delete[] this->ptr;this->ptr = newptr; // 更新 ptr 指向新内存this->len += newlen; // 更新长度this->size = newlen + 1; // 更新容量return *this;
}ostream& operator<<(ostream &out, const My_string &S)
{out << S.ptr;return out;
}istream& operator>>(istream &in, My_string &S)
{delete[] S.ptr; // 释放已有内存S.ptr = new char[S.size]; // 重新分配内存in >> S.ptr; // 读取字符串S.len = strlen(S.ptr); // 更新长度return in;
}
void My_string::show()
{cout<<"*ptr="<<ptr<<endl;cout<<"size="<<size<<endl;cout<<"len="<<len<<endl;
}

相关文章:

  • Groupby_SQL和pandas等效例子
  • 数据结构7—树(顺序存储二叉树—堆)含TOPK问题
  • OpenCV视频I/O(1)视频采集类VideoCapture介绍
  • YOLOv9改进,YOLOv9主干网络替换为GhostNetV3(2024年华为提出的轻量化架构,全网首发),助力涨点
  • 作家依靠AI一年内创作120部作品
  • 微信小程序实战教程:轻松实现列表批量选择功能
  • Python自然语言处理之spacy模块介绍、安装与常见操作案例
  • 从零开始手写STL库:Stack
  • 【质优价廉】GAP9 AI算力处理器赋能智能可听耳机,超低功耗畅享未来音频体验!
  • 【C语言内存管理】第三章 堆内存管理
  • 《ToDesk 云电脑、易腾云、青椒云移动端体验实测:让手机秒变超级电脑》
  • ARM 服务器上安装 OpenEuler (欧拉)
  • 银河麒麟桌面操作系统V10登录闪退问题解决
  • python并发编程实战
  • R 语言 | 取数据框一列子集时,如何保持数据框结构?drop=F
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • Angularjs之国际化
  • CEF与代理
  • Docker: 容器互访的三种方式
  • ES6之路之模块详解
  • github指令
  • iOS动画编程-View动画[ 1 ] 基础View动画
  • JavaScript-Array类型
  • Java多态
  • Java应用性能调优
  • MYSQL如何对数据进行自动化升级--以如果某数据表存在并且某字段不存在时则执行更新操作为例...
  • REST架构的思考
  • spark本地环境的搭建到运行第一个spark程序
  • 从0到1:PostCSS 插件开发最佳实践
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 聊聊hikari连接池的leakDetectionThreshold
  • 前端面试总结(at, md)
  • 数组的操作
  • 突破自己的技术思维
  • 微服务入门【系列视频课程】
  • 我这样减少了26.5M Java内存!
  • 系统认识JavaScript正则表达式
  • 再谈express与koa的对比
  • 栈实现走出迷宫(C++)
  • 正则表达式小结
  • PostgreSQL之连接数修改
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • #HarmonyOS:Web组件的使用
  • (Python) SOAP Web Service (HTTP POST)
  • (vue)el-tabs选中最后一项后更新数据后无法展开
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (剑指Offer)面试题41:和为s的连续正数序列
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在