C++——仿照string类,实现myString类

#ifndef MYSTRING_H
#define MYSTRING_H

#include <iostream>
#include <cstring>

class MyString {
private:
    char *str;          // 记录C风格的字符串
    int size;            // 记录字符串的实际长度
public:
    // 定义无参构造
    MyString();
    
    // 定义有参构造
    MyString(const char *s);
    
    // 定义拷贝构造函数
    MyString(const MyString &other);
    
    // 定义拷贝赋值函数
    MyString &operator=(const MyString &other);
    
    // 定义析构函数
    ~MyString();
    
    // 判空函数
    bool empty() const;
    
    // size函数
    int my_size() const;
    
    // c_str函数
    const char *c_str() const;
    
    // at函数
    char &at(int pos);
};

#endif // MYSTRING_H
#include "MyString.h"

// 定义无参构造
MyString::MyString() : size(10) {
    str = new char[size];
    strcpy(str, "");
    std::cout << "MyString::无参构造,this = " << this << std::endl;
}

// 定义有参构造
MyString::MyString(const char *s) {
    size = strlen(s);
    str = new char[size + 1];
    strcpy(str, s);
    std::cout << "MyString::有参构造,this = " << this << std::endl;
}

// 定义拷贝构造函数
MyString::MyString(const MyString &other) : str(new char[other.size + 1]), size(other.size) {
    strcpy(this->str, other.str);
    std::cout << "MyString::拷贝构造,this = " << this << std::endl;
}

// 定义拷贝赋值函数
MyString &MyString::operator=(const MyString &other) {
    if (this != &other) {  // 防止自己给自己赋值
        delete[] str;
        size = other.size;
        str = new char[size + 1];
        strcpy(str, other.str);
    }
    std::cout << "MyString::拷贝赋值,this = " << this << std::endl;
    return *this;
}

// 定义析构函数
MyString::~MyString() {
    delete[] str;
    std::cout << "MyString::析构函数,this = " << this << std::endl;
}

// 判空函数
bool MyString::empty() const {
    return size == 0 || str[0] == '\0';
}

// size函数
int MyString::my_size() const {
    return size;
}

// c_str函数
const char *MyString::c_str() const {
    return str;
}

// at函数
char &MyString::at(int pos) {
    return str[pos];
}
#include "MyString.h"
#include <iostream>

int main() {
    // 调用无参构造
    MyString s1;
    std::cout << "my_size = " << s1.my_size() << "  str1 = " << s1.c_str() << std::endl;

    // 调用有参构造
    MyString s2("XHJ");
    std::cout << "my_size = " << s2.my_size() << "  str2 = " << s2.c_str() << std::endl;

    // 调用拷贝构造
    MyString s3(s2);
    std::cout << "my_size = " << s3.my_size() << "  str3 = " << s3.c_str() << std::endl;

    // 调用at函数
    std::cout << "第一个字符:" << s3.at(0) << std::endl;
    std::cout << "第二个字符:" << s3.at(1) << std::endl;
    std::cout << "第三个字符:" << s3.at(2) << std::endl;

    // 调用有参构造
    MyString s4("");
    std::cout << "my_size = " << s4.my_size() << "  str4 = " << s4.c_str() << std::endl;

    // 调用empty函数
    // 判断字符串s3是否为空
    if (s3.empty())
        std::cout << "s3字符串为空!" << std::endl;
    else
        std::cout << "s3字符串不为空!" << std::endl;

    // 判断字符串s4是否为空
    if (s4.empty())
        std::cout << "s4字符串为空!" << std::endl;
    else
        std::cout << "s4字符串不为空!" << std::endl;

    s4 = s2;
    std::cout << "my_size = " << s4.my_size() << "  str4 = " << s4.c_str() << std::endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值