c++&Qt_day04

作业1

题目

仿 String 类,手动实现 MyString

效果

在这里插入图片描述

代码

main.cpp

#include <iostream>

#include "myString.h"

using namespace std;

void printStr(const char *msg, MyString &str)
{
    cout << msg << " = ";
    for (int i = 0; i < str.size(); i++) {
        cout << str.at(i);
    }
    cout << endl;
}

int main(int argc, char const *argv[])
{
    // 无参构造
    MyString s0;

    // 有参构造
    MyString s1("hello wrold");

    // 拷贝构造
    MyString s2 = "very good";
    MyString s3 = s2;

    // 拷贝赋值
    MyString s4;
    s4 = s1;
    s4 = "ok";

    printStr("s0", s0);
    printStr("s1", s1);
    printStr("s2", s2);
    printStr("s3", s3);
    printStr("s4", s4);

    // 转换c风格
    const char *s5 = s1.c_str();
    cout << "const char *s5 = " << s5 << endl;

    // 判空
    cout << "=========================" << endl;
    cout << boolalpha;
    cout << "s0 is empty = " << s0.isEmpty() << endl;
    cout << "s1 is empty = " << s1.isEmpty() << endl;

    // 长度
    cout << "=========================" << endl;
    cout << "s0 size = " << s0.size() << endl;
    cout << "s1 size = " << s1.size() << endl;

    // 按下标获取
    cout << "=========================" << endl;
    cout << "s1 at(6) = " << s1.at(6) << endl;

    return 0;
}

myString.h

#ifndef _MY_STRING_H_
#define _MY_STRING_H_

class MyString
{
   public:
    MyString();
    MyString(const char *s);
    MyString(const char *&s);              // 拷贝构造
    MyString(const MyString &other);       // 拷贝构造
    MyString &operator=(const char *s);    // 拷贝赋值
    MyString &operator=(MyString &other);  // 拷贝赋值
    ~MyString();                           // 析构
    bool isEmpty();                        // 判空
    int size();                            // 长度
    const char *c_str();                   // c风格字符串转换为cpp风格
    char &at(int pos);                     // 按下标获取字符

   private:
    char *str;
    int _size;
};

#endif  //_MY_STRING_H_

myString.cpp

#include "myString.h"

#include <cstring>

/**
 * @brief    无参构造
 * @param    无
 * @return   无
 */
MyString::MyString() : _size(0), str(new char) { strcpy(str, ""); }

/**
 * @brief    有参构造
 * @param    s 字符串
 * @return   无
 */
MyString::MyString(const char *s)
{
    this->_size = strlen(s);
    this->str = new char[this->_size + 1];
    strcpy(this->str, s);
}

/**
 * @brief    拷贝构造
 * @param    s 字符串
 * @return   无
 */
MyString::MyString(const char *&s)
{
    this->_size = strlen(s);
    this->str = new char[this->_size + 1];
    strcpy(this->str, s);
}

/**
 * @brief    拷贝构造
 * @param    other 目标对象
 * @return   无
 */
MyString::MyString(const MyString &other)
{
    if (this != &other) {
        this->_size = other._size;
        this->str = new char[other._size + 1];
        strcpy(this->str, other.str);
    }
}

/**
 * @brief    拷贝赋值
 * @param    s 目标字符串
 * @return   源对象
 */
MyString &MyString::operator=(const char *s)
{
    this->_size = strlen(s);
    delete (this->str);
    this->str = new char[this->_size + 1];
    strcpy(this->str, s);

    return *this;
}

/**
 * @brief    拷贝赋值
 * @param    other 目标对象
 * @return   源对象
 */
MyString &MyString::operator=(MyString &other)
{
    if (this != &other) {
        this->_size = other._size;
        delete (this->str);
        this->str = new char[this->_size + 1];
        strcpy(this->str, other.str);
    }

    return *this;
}

/**
 * @brief    析构函数
 * @param    无
 * @return   无
 */
MyString::~MyString() { delete (this->str); }

/**
 * @brief    判空
 * @param    无
 * @return   ture表示空   false表示非空
 */
bool MyString::isEmpty() { return (this->_size == 0) ? true : false; }

/**
 * @brief    获取长度
 * @param    无
 * @return   长度
 */
int MyString::size() { return this->_size; }

/**
 * @brief    将MyString类型转换为c风格字符串
 * @param
 * @return
 */
const char *MyString::c_str() { return this->str; }

/**
 * @brief    按下标获取字符
 * @param    pos 下标
 * @return   下标对应字符
 */
char &MyString::at(int pos)
{
    char &ch = this->str[pos];
    return ch;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值