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

embedded/2024/9/25 6:25:30/

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


http://www.ppmy.cn/embedded/116486.html

相关文章

Java面试篇基础部分-Semaphore及其用法详解

Semaphore 是一种基于计数的信号量&#xff0c;在定义信号量对象的时候可以设置一个阈值&#xff0c;然后基于这个阈值&#xff0c;多线程可以竞争访问信号量&#xff0c;线程竞争到许可的信号之后&#xff0c;开始执行具体的业务逻辑&#xff0c;业务逻辑在执行完成之后释放这…

6.C++面向对象2(默认成员函数,构造函数,析构函数详解)

⭐本篇为C学习第6章&#xff0c;主要了解默认成员函数&#xff0c;构造函数&#xff0c;析构函数 ⭐本人Gitee C代码仓库&#xff1a;yzc的c学习: 小川c的学习记录 - Gitee.com 目录 一. 类的6个默认成员函数 二. 构造函数 三.析构函数 四 多个对象调用构造函数和析构函数的…

ubuntu挂载磁盘或U盘

方法 在Ubuntu中&#xff0c;使用命令行读取U盘通常涉及到以下几个步骤&#xff1a;插入U盘、查找设备名称、挂载U盘以及访问文件。以下是详细步骤&#xff1a; 1. 插入U盘 将U盘插入计算机的USB端口。 2. 查找设备名称 你可以使用lsblk或dmesg命令来查找新插入的U盘设备名…

游戏开发2025年最新版——八股文面试题(unity,虚幻,cocos都适用)

1.静态合批与动态合批的原理是什么&#xff1f;有什么限制条件&#xff1f;为什么&#xff1f;对CPU和GPU产生的影响分别是什么&#xff1f; 原理&#xff1a;Unity运行时可以将一些物体进行合并&#xff0c;从而用一个描绘调用来渲染他们&#xff0c;就是一个drawcall批次。 限…

Linux 基本指令的学习

01. ls 指令 语法 &#xff1a; ls [ 选项 ][ 目录或文件 ] 功能 &#xff1a;对于目录&#xff0c;该命令列出该目录下的所有子目录与文件。对于文件&#xff0c;将列出文件名以及其他信息。 常用选项&#xff1a; -a 列出目录下的所有文件&#xff0c;包括以 . 开头的隐含…

从 Oracle 集群到单节点环境(详细记录一次数据迁移过程)之一:生产环境与目标服务器详情

从 Oracle 集群到单节点环境&#xff08;详细记录一次数据迁移过程&#xff09;之一&#xff1a;生产环境与目标服务器详情 目录 从 Oracle 集群到单节点环境&#xff08;详细记录一次数据迁移过程&#xff09;之一&#xff1a;生产环境与目标服务器详情一、操作系统环境二、Or…

springboot整合openfeign

文章目录 准备一、引入必要依赖二、写一个feign client并暴露到注册中心2.1 client2.2 开启Feign客户端功能 三、别的服务引入IProductClient并调用方法3.1 建一个order-service&#xff0c;引入IProductClient所在模块3.2 注入IProductClient&#xff0c;并调用方法 四、启动服…

【计算机网络 - 基础问题】每日 3 题(二十)

✍个人博客&#xff1a;Pandaconda-CSDN博客 &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/fYaBd &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 C 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞&#x1f44d;收藏&…