C++学习9.24

news/2024/9/30 0:29: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.ppmy.cn/news/1532165.html

相关文章

Windows打开HDF5图像:HDFView软件的下载、安装

本文介绍在Windows电脑中&#xff0c;下载、安装用以查看HDF5图像数据的软件HDFView的方法。 HDF5&#xff08;Hierarchical Data Format 5&#xff09;是一种用于存储和管理大量科学数据的文件格式&#xff0c;其由HDF Group开发和维护&#xff0c;广泛应用于科学计算、工程、…

基于微信的原创音乐小程序的设计与实现+ssm论文源码调试讲解

第二章 开发工具及关键技术介绍 2.1 JAVA技术 Java主要采用CORBA技术和安全模型&#xff0c;可以在互联网应用的数据保护。它还提供了对EJB&#xff08;Enterrise JavaBeans&#xff09;的全面支持&#xff0c;java servlet AI&#xff0c;JS&#xff08;java server ages&…

11.C++程序中的常用函数

我们将程序中反复执行的代码封装到一个代码块中&#xff0c;这个代码块就被称为函数&#xff0c;它类似于数学中的函数&#xff0c;在C程序中&#xff0c;有许多由编译器定义好的函数&#xff0c;供大家使用。下面就简单说一下&#xff0c;C中常用的函数。 1.sizeof sizeof函…

城市生命线安全监管系统:智慧城市的守护者

城市生命线安全监管系统是智慧城市建设中的重要组成部分&#xff0c;它涉及到城市燃气、供水、排水、热力、电力、电梯、通信、轨道交通、综合管廊、输油管线等关键基础设施的监测和管理。这些系统如同城市的“神经”和“血管”&#xff0c;保障着城市的正常运行和居民的生活安…

51单片机的光照强度检测【proteus仿真+程序+报告+原理图+演示视频】

1、主要功能 该系统由AT89C51/STC89C52单片机LCD1602显示模块光照传感器按键蜂鸣器LED等模块构成。适用于光照强度检测、光照强度测量报警等相似项目。 可实现功能: 1、LCD1602实时显示光照强度信息 2、光照强度传感器&#xff08;电位器模拟&#xff09;采集光照信息 3、可…

Go基础编程 - 15 - 延迟调用(defer)

延迟调用 defer 1. 特性2. 常用用途3. defer 执行顺序&#xff1a;同函数内先进后出4. defer 闭包5. defer 陷阱 上一篇&#xff1a;泛型 1. 特性 1. 关键字 defer 用于注册延迟调用。 2. defer 调用直到 return 前才被执行。 3. 同函数内多个 defer 语句&#xff0c;按先进后…

GPT-4提示工程大赛冠军的秘籍分享

前言 去年 11 月 8 日&#xff0c;新加坡政府科技局&#xff08;GovTech&#xff09;组织举办了首届 GPT-4 提示工程&#xff08;Prompt Engineering&#xff09;竞赛。数据科学家 Sheila Teo 最终夺冠。之后&#xff0c;Teo 发布了一篇题为《我如何赢得了新加坡 GPT-4 提示工…

数字安全二之密钥结合消息摘要

HMACSHA256的定义 HMACSHA256是一种使用 SHA-256 哈希算法的 HMAC&#xff08;基于哈希的消息认证码&#xff0c;Hash-based Message Authentication Code&#xff09; 机制。它结合了【散列函数】 和 【密钥】&#xff0c;用于确保消息的完整性和真实性 HMAC 与 SHA-256 的作…