面对对象特征之封装

#include <iostream>
#include <cstring>
#include <stdexcept>

using namespace std;

class myString {
private:
    int size;
    char *str;

public:
    // 无参构造
    myString() : size(10) {
        str = new char[size + 1];
        str[0] = '\0';
    }

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

    // 有参构造
    myString(int n, char ch) : size(n) {
        str = new char[size + 1];
        memset(str, ch, size);
        str[size] = '\0';
    }

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

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

    // 拷贝赋值函数
    myString &operator=(const myString &other) {
        if (this != &other) {
            delete[] str;
            size = other.size;
            str = new char[size + 1];
            strcpy(str, other.str);
        }
        return *this;
    }

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

    // size函数
    int length() const {
        return size;
    }

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

    // at函数
    char &at(int index) {
        if (index < 0 || index >= size) {
            throw std::out_of_range("Index out of range");
        }
        return str[index];
    }

    // 二倍扩容
    void expend() {
        size *= 2;
        char *newStr = new char[size + 1];
        strcpy(newStr, str);
        delete[] str;
        str = newStr;
    }

    // += 运算符重载
    myString &operator+=(const myString &other) {
        int newSize = size + other.size;
        char *newStr = new char[newSize + 1];
        strcpy(newStr, str);
        strcat(newStr, other.str);
        delete[] str;
        str = newStr;
        size = newSize;
        return *this;
    }

    // [] 运算符重载
    char &operator[](int index) {
        return at(index);
    }

    // + 运算符重载
    myString operator+(const myString &other) const {
        myString result;
        result.size = size + other.size;
        result.str = new char[result.size + 1];
        strcpy(result.str, str);
        strcat(result.str, other.str);
        return result;
    }

    // == 运算符重载
    bool operator==(const myString &other) const {
        return strcmp(str, other.str) == 0;
    }

    // != 运算符重载
    bool operator!=(const myString &other) const {
        return !(*this == other);
    }

    // < 运算符重载
    bool operator<(const myString &other) const {
        return strcmp(str, other.str) < 0;
    }

    // > 运算符重载
    bool operator>(const myString &other) const {
        return strcmp(str, other.str) > 0;
    }

    // <= 运算符重载
    bool operator<=(const myString &other) const {
        return !(*this > other);
    }

    // >= 运算符重载
    bool operator>=(const myString &other) const {
        return !(*this < other);
    }

    // << 运算符重载 (流输出)
    friend ostream &operator<<(ostream &os, const myString &s) {
        os << s.str;
        return os;
    }

    // >> 运算符重载 (流输入)
    friend istream &operator>>(istream &is, myString &s) {
        char buffer[1024]; // 假设最大输入长度为1024
        is >> buffer;
        s = myString(buffer); // 使用已有的构造函数
        return is;
    }
};

int main() {
    myString s1("Hello");
    myString s2(" World");
    myString s3 = s1 + s2; // 使用 + 运算符重载

    cout << "s1: " << s1 << endl;
    cout << "s2: " << s2 << endl;
    cout << "s3: " << s3 << endl;

    cout << "s1 length: " << s1.length() << endl;
    cout << "s1[1]: " << s1[1] << endl;

    // 测试比较运算符
    cout << "s1 == s2: " << (s1 == s2) << endl;
    cout << "s1 != s2: " << (s1 != s2) << endl;
    cout << "s1 < s2: " << (s1 < s2) << endl;

    // 测试输入
    myString s4;
    cout << "Enter a string: ";
    cin >> s4;
    cout << "You entered: " << s4 << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值