c++&Qt_day04

作业1

题目

仿 String 类,手动实现 MyString

效果

c++&Qt_day04_第1张图片

代码

main.cpp

#include 

#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 

/**
 * @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;
}

你可能感兴趣的:(c++,开发语言)