2024/03/27(C++·day3)

一、思维导图

二、完成下面类

代码

#include <cstring>
#include <iostream>

using namespace std;

class myString
{
private:
    char *str;  // 记录C风格的字符串
    int size;   // 记录字符串的实际长度

public:
    // 无参构造函数
    myString() : size(10)
    {
        str = new char[size]; // 构造出一个长度为10的字符串
        strcpy(str, "");       // 初始化为空字符串
    }

    // 有参构造函数
    myString(const char *s)
    {
        size = strlen(s);
        str = new char[size + 1];
        strcpy(str, s);
    }

    // 拷贝构造函数
    myString(const myString &other)
    {
        size = other.size;
        str = new char[size + 1];
        strcpy(str, other.str);
    }

    // 析构函数
    ~myString()
    {
        delete[] str; // 释放内存
    }

    // 判空函数
    bool isEmpty() const
    {
        return (size == 0);
    }

    // size函数返回字符串长度
    int getSize() const
    {
        return size;
    }

    // c_str返回C风格字符串
    const char *c_str() const
    {
        return str;
    }

    // at函数访问指定位置字符
    char &at(int pos)
    {
        if (pos >= 0 && pos < size)
        {
            return str[pos];
        }
        else
        {
            // 索引超出范围,返回默认的字符
            static char default_char = '\0';
            return default_char;
        }
    }
};

int main()
{
    myString str1;                // 调用无参构造函数
    cout << "String 1: " << str1.c_str() << endl;

    myString str2("Hello Shanghai !");       // 调用有参构造函数
    cout << "String 2: " << str2.c_str() << endl;

    myString str3 = str2;         // 调用拷贝构造函数
    cout << "String 3: " << str3.c_str() << endl;

    cout << "string是否为空? " << (str1.isEmpty() ? "Yes" : "No") << endl;
    cout << "String2大小: " << str2.getSize() << endl;

    cout << "String2的第四个位置: " << str2.at(4) << endl;

    return 0;
}

实现效果

相关推荐

  1. <span style='color:red;'>c</span>++ <span style='color:red;'>day</span><span style='color:red;'>3</span>

    c++ day3

    2024-03-28 10:06:05      34 阅读
  2. c++day3

    2024-03-28 10:06:05       34 阅读
  3. <span style='color:red;'>C</span>++<span style='color:red;'>Day</span><span style='color:red;'>3</span>

    C++Day3

    2024-03-28 10:06:05      36 阅读
  4. <span style='color:red;'>C</span>++ <span style='color:red;'>day</span><span style='color:red;'>3</span>

    C++ day3

    2024-03-28 10:06:05      35 阅读
  5. <span style='color:red;'>day</span><span style='color:red;'>3</span>-<span style='color:red;'>C</span>++

    day3-C++

    2024-03-28 10:06:05      15 阅读
  6. <span style='color:red;'>c</span>++<span style='color:red;'>day</span><span style='color:red;'>3</span>

    c++day3

    2024-03-28 10:06:05      16 阅读
  7. <span style='color:red;'>C</span>++ <span style='color:red;'>day</span><span style='color:red;'>3</span>

    C++ day3

    2024-03-28 10:06:05      11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-28 10:06:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-28 10:06:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-28 10:06:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-28 10:06:05       20 阅读

热门阅读

  1. 【PCIe硬件】PCIe引脚PRSNT与热插拔

    2024-03-28 10:06:05       22 阅读
  2. Android 手势相关(二)

    2024-03-28 10:06:05       19 阅读
  3. python保存中间变量(学习笔记)

    2024-03-28 10:06:05       15 阅读
  4. AcWing 1221. 四平方和

    2024-03-28 10:06:05       18 阅读
  5. shutil模块篇

    2024-03-28 10:06:05       22 阅读
  6. 手机安卓系统内嵌测试代码分享

    2024-03-28 10:06:05       27 阅读
  7. 在 Android 系统中,窗口(Window)按照功能和层级

    2024-03-28 10:06:05       21 阅读
  8. 视觉循迹小车(旭日x3派、摄像头、循迹)

    2024-03-28 10:06:05       21 阅读
  9. 2023.03.28

    2024-03-28 10:06:05       19 阅读
  10. 软考 - 软件架构设计师 - 架构风格

    2024-03-28 10:06:05       24 阅读
  11. 【React】React 组件 API

    2024-03-28 10:06:05       23 阅读