【C++】手动实现String类的封装(分文件编译)

news2024/9/20 9:05:08

        实现了String类的大部分封装,采用分文件编译 

//mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H

#include <iostream>
#include <cstring>
using namespace std;

class myString
{
private:
    char *str;    //定义一个字符串
    int size;     //记录字符串长度

public:
    //成员函数:

    //1.构造函数
    myString(const char* s);

    //2.析构函数
    ~myString();

    //3.为字符串赋值函数 "="
    myString & operator=(const myString &other);

    //4.访问指定字符,有边界检查 "at"
    char &s_at(int index);

    //5.访问指定字符 "operator[]"
    char &s_operator(int index);

    //6.返回指向字符串首字符的指针 "data"
    char *s_data();

    //7.返回字符串的不可修改的C字符数组版本 "c_str"
    const char* s_c_str()const;

    //8.检查字符串是否为空 "empty"
    bool empty();

    //9.返回字符串长度 "size"
    int s_size();

    //10.返回字符串长度 "length"
    int s_lenth();

    //11.返回当前对象分配的存储空间能保存的字符数量 "capacity"
    int s_capacity();

    //12.清除内容 "clear"
    void s_clear();

    //13.后附字符到结尾 "push_back"
    void s_push_back(char c);

    //14.移除末尾字符 "pop_back"
    void s_pop_back();

    //15.后附字符到结尾 "append"
    void s_append(const char *s);

    //16.后附字符到结尾
    myString &operator+=(const myString &other);

    //17.连接两个字符串或者一个字符串和一个字符
    const myString operator+(const myString &other)const;

    //18.比较字符串
    bool operator==(const myString &other) const;
    bool operator!=(const myString &other) const;
    bool operator<(const myString &other) const;
    bool operator>(const myString &other) const;
    bool operator<=(const myString &other) const;
    bool operator>=(const myString &other) const;

    //19.输入输出
    friend ostream &operator<<(ostream &os, const myString &s);
    friend istream &operator>>(istream &is, myString &s);
};



#endif // MYSTRING_H
//mystring.cpp
#include "mystring.h"
myString::myString(const char* s)
{
    size = strlen(s);             //求传入的字符串长度
    str = new char[size+1];       //申请空间存储字符串
    strcpy(str,s);                //将字符串拷贝到str中
}

myString::~myString()
{
    delete[] str;
}

myString &myString::operator=(const myString &other)
{
    if(this != &other)
    {
        delete[] str;
        size = other.size;
        str = new char[size + 1];
        strcpy(str, other.str);
    }
    return *this;
}

char &myString::s_at(int index)
{
    if(index < 0 || index >= size)
    {
        cout<<"输入错误"<<endl;
    }
    return str[index];
}

char &myString::s_operator(int index)
{
    return str[index];
}

char *myString::s_data()
{
    return str;
}

const char *myString::s_c_str() const
{
    return str;
}

bool myString::empty()
{
    return size == 0;
}

int myString::s_size()
{
    return size;
}

int myString::s_lenth()
{
    return size;
}

int myString::s_capacity()
{
    return size+1;       //有一个'\0'
}

void myString::s_clear()
{
    delete[] str;
    str = NULL;
    size = 0;
}

void myString::s_push_back(char c)
{
    delete[] str;
    str = new char[size+1];
    str[size-1] = c;
    str[size] = '\0';
}

void myString::s_pop_back()
{
    if(size > 0)
    {
        size--;
        str[size] = '\0';
    }
}

void myString::s_append(const char *s)
{
    int new_size = size + strlen(s);
    char* temp = str;
    str = new char[new_size + 1];     // 分配新内存
    strcpy(str, temp);                // 复制旧字符串
    strcat(str, s);                   // 追加新字符串
    delete[] temp;                    // 释放旧内存
    size = new_size;
}

myString &myString::operator+=(const myString &other)
{
    s_append(other.str);
    return *this;
}

const myString myString::operator+(const myString &other) const
{
    myString temp(*this);
    temp.s_append(other.str);
    return temp;
}

bool myString::operator==(const myString &other) const {
    return strcmp(str, other.str) == 0;
}

bool myString::operator!=(const myString &other) const {
    return strcmp(str, other.str) != 0;
}

bool myString::operator<(const myString &other) const {
    return strcmp(str, other.str) < 0;
}

bool myString::operator>(const myString &other) const {
    return strcmp(str, other.str) > 0;
}

bool myString::operator<=(const myString &other) const {
    return strcmp(str, other.str) <= 0;
}

bool myString::operator>=(const myString &other) const {
    return strcmp(str, other.str) >= 0;
}

istream &operator>>(istream &is, myString &s)
{
    char buffer[1024];   // 假设输入的字符串不超过1024个字符
    is >> buffer;       // 从输入流中读取字符串
    s = buffer;         // 将读取的字符串赋值给 myString 对象
    return is;          // 返回输入流的引用
}

ostream &operator<<(ostream &os, const myString &s)
{
    os<<s.s_c_str();
    return os;
}

 

//main.cpp
#include "mystring.h"
int main()
{
    myString s1("hello");
    cout<<s1<<endl;
    cout<<"*************************"<<endl;

    myString s2("world");
    cout<<s1+s2<<endl;

    if(s1>s2 == 1)
    {
        cout<<"s1 > s2"<<endl;
    }
    else
    {
        cout<<"s1 < s2"<<endl;
    }
    return 0;
}

输出结果如下:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2098639.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

比亚迪方程豹携手华为乾崑智驾,加速中国智驾技术向前

近日&#xff0c;比亚迪方程豹与华为乾崑智驾在深圳签署合作协议&#xff0c;中国两大科技巨头强强联合&#xff0c;共同合作开发全球首个硬派专属智能驾驶方案&#xff0c;实现整车智驾深度融合&#xff0c;首发搭载在即将上市的方程豹豹8车型。 比亚迪智驾以自主研发和开放合…

MySQL之数据库基础

目录 一、数据库 1、基本概念 2、常见的数据库 3、MySQL数据库 连接MySQL服务器 数据逻辑存储 二、数据库和表的本质 三、SQL语句 四、服务器&#xff0c;数据库&#xff0c;表的关系 五、存储引擎 查看存储引擎 一、数据库 1、基本概念 一般来说&#xff0c;数据库…

【软件技巧】第33课,软件逆向安全工程师之如何快速的跑到某行代码EIP设置,每天5分钟学习逆向吧!

鼠标右键在此设置EIP EIP&#xff08;Extended Instruction Pointer&#xff09;是x86架构中一个重要的寄存器&#xff0c;它用于存储当前正在执行的指令的地址。EIP是程序计数器&#xff08;Program Counter&#xff09;的扩展版本&#xff0c;因为它是32位寄存器&#xff0c…

Centos安装node_exporter

使用以下命令下载最新版本的node_exporter, 地址: https://github.com/prometheus/node_exporter/releasesorter/releases&#xff1a; wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz或本地下载上传到服…

python 字典怎么提取value

打开编辑器&#xff0c;写上注释内容。 新建一个函数getvalue。 新建一个字典。 zidian{"country1":"america","country2":"australia","country3":"germany"} 利用values这个方法来获取字典中的所有Vlue值&…

浅析synchronized锁升级的原理与实现 1

目录 背景 锁的用法 底层实现 原理分析 锁的状态 锁升级 锁升级过程 锁升级简要步骤 锁升级细化流程 背景 在多线程编程中&#xff0c;线程同步是一个关键的概念&#xff0c;它确保了多个线程对共享资源的安全访问。Java中的synchronized关键字是一种常用的线程同步机制…

数据安全与个人信息保护的辨析

文章目录 前言一、合规1、合规的目标导向原则2、监管平衡的原则二、基础设施1、公共基础设施2、企业基础设施三、数据流通1、数据生产要素是数字化时代生产要素的变革理论2、数据产品的保护源自于数据产品的价值四、产品与服务1、数据安全与网络安全2、数据安全的分类分级与数据…

Qt-信号与槽

1. 认识信号和槽 Qt中&#xff0c;谈到信号&#xff0c;涉及到三个要素. 信号源&#xff1a;由哪个控件发出的信号信号的类型&#xff1a;用户进行不同的操作&#xff0c;就可能出发不同的信号 点击按钮&#xff0c;触发点击信号 在输入框中移动光标&#xff0c;触发移动光标的…

github私有仓库通过action部署hexo到公开仓库

github私有仓库通过action部署hexo到公开仓库 有一段时间一直将博客md文件直接放到公开仓库然后通过工作流action创建一个gh-page分支&#xff0c;来实现部署 但是这样做有一个问题&#xff0c;如果你的源文件&#xff0c;或者配置文件中有涉及变量&#xff0c;或者密钥key&a…

STM32G474之TIM1输出PWM互补信号(无死区时间和BKIN输入)

STM32G474之TIM1输出PWM互补信号&#xff0c;无死区时间&#xff0c;无BKIN输入。定时器1是16向上计数器&#xff0c;16向下计数器&#xff0c;16向上/向下计数器&#xff0c;输入时钟分频值&#xff1a;“1至65536”中的任意整数&#xff1b;捕获输入通道4个&#xff0c;比较输…

【C++ Primer Plus习题】9.4

问题: 解答: main.cpp #include <iostream> #include "sales.h" using namespace std; using namespace SALES;int main() {Sales s1, s2;double de[QUARTERS] { 12.1,32.1,42.1,51.1 };setSales(s1, de, QUARTERS);showSales(s1);cout << endl;setSal…

AI图片扩展工具 | 一名非技术人员依靠AI就能写出来

前言 我本职是一个技术支持&#xff0c;原本和开发搭不到边。但这两年 AI 发展迅猛&#xff0c;让我这样的半吊子也能借助 AI 的力量写网站。 因为这两年压力大&#xff0c;所以琢磨着出海看看能不能挣到钱&#xff0c;所以在学习做网站。 这个站是我的第 5 个作品了。前面 …

盘点大模型中转 API 平台,并比较费用

1. 大模型中转 API 平台集合 1.1 DevAGI DevAGI开放平台 Open AI 价格 1.2 Deepbricks 官网价格 1.3 AiHubMix AiHubMix 官网 使用教程 价格&#xff1a; 1.4 WildCard 开卡订阅 WildCard官网 价格 有3.5% 的充值手续费&#xff0c;API 价格与 Open AI 一样 2. 价…

vue3 element-plus form 表单 循环校验

需求&#xff1a; 表单是循环出来的&#xff0c;3个一组&#xff0c;每组对于前端来说是一样的&#xff0c; 需要校验输入框是否必填是否为小数或者整数 效果&#xff1a; 未输入--显示&#xff1a;请输入 输入不是小数或整数--显示&#xff1a;请输入整数或小数 输入正确…

南京大学软件学院硕士毕业流程

背景介绍 南京大学作为国内顶尖的985高校&#xff0c;拥有丰富的校内资源和雄厚的师资力量。然而&#xff0c;在管理与协调方面仍存在一定的不足。尤其是在硕士生培养过程中&#xff0c;临近毕业阶段的流程中常出现信息不透明和混乱的现象&#xff0c;导致学生在了解所需材料和…

codeforces Round 970 (Div. 3)(A-F)

文章目录 [Codeforces Round 970 (Div. 3)](https://codeforces.com/contest/2008)A-[Sakurakos Exam](https://codeforces.com/contest/2008/problem/A)B-[Square or Not](https://codeforces.com/contest/2008/problem/B)C-[Longest Good Array](https://codeforces.com/cont…

Halcon!!!最新!! 从零认识标定板——制作描述文件和自己的标定板

一.标定板简介 ‌标定板是一种带有固定间距图案阵列的几何模型&#xff0c;主要用于机器视觉、图像测量、摄影测量、三维重建等领域。‌它的主要功能包括校正镜头畸变、确定物理尺寸和像素间的转换关系&#xff0c;以及确定空间物体表面某点的三维几何位置与其在图像中对应点之…

C练手题--Progressive Spiral Number Position 【7 kyu】

一.原题 链接&#xff1a;Training on Progressive Spiral Number Position | Codewars Assume that you started to store items in progressively expanding square location, like this for the first 9 numbers: 二、解题 1、分析 &#xff08;1&#xff09;数字必须存…

自闭症自言自语会好吗

在自闭症儿童的成长过程中&#xff0c;自言自语作为一种常见的行为表现&#xff0c;往往让家长们既担忧又困惑。这种非社交性的语言行为&#xff0c;虽然在一定程度上是自闭症孩子自我交流的方式&#xff0c;但过度或不适宜的自言自语却可能影响其社交技能和日常功能的发展。那…

[米联客-XILINX-H3_CZ08_7100] FPGA程序设计基础实验连载-25 RGB转HDMI显示方案

软件版本&#xff1a;VIVADO2021.1 操作系统&#xff1a;WIN10 64bit 硬件平台&#xff1a;适用 XILINX A7/K7/Z7/ZU/KU 系列 FPGA 实验平台&#xff1a;米联客-MLK-H3-CZ08-7100开发板 板卡获取平台&#xff1a;https://milianke.tmall.com/ 登录“米联客”FPGA社区 http…