9.24作业

1.

#include <iostream>
#include <cstring>

using namespace std;

class My_string {
private:
    char *str;
    size_t len;

public:
    My_string(const char *s = "") {
        if (s) {
            len = strlen(s);
            str = new char[len + 1];
            strcpy(str, s);
        } else {
            str = new char[1];
            *str = '\0';
            len = 0;
        }
    }

    ~My_string() {
        delete[] str;
    }

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

    // 赋值运算符
    My_string& operator=(const My_string &other) {
        if (this != &other) {
            delete[] str;
            len = other.len;
            str = new char[len + 1];
            strcpy(str, other.str);
        }
        return *this;
    }

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

    // += 运算符
    My_string& operator+=(const My_string &other) {
        char *new_str = new char[len + other.len + 1];
        strcpy(new_str, str);
        strcat(new_str, other.str);
        delete[] str;
        str = new_str;
        len += other.len;
        return *this;
    }

    // + 运算符(字符)
    My_string operator+(const char c) const {
        My_string result;
        result.len = len + 1;
        result.str = new char[result.len + 1];
        strcpy(result.str, str);
        result.str[len] = c;
        result.str[result.len] = '\0';
        return result;
    }

    // += 运算符(字符)
    My_string& operator+=(const char c) {
        str[len] = c;
        str[len + 1] = '\0';
        len++;
        return *this;
    }

    // [] 运算符
    char& operator[](size_t index) {
        return str[index];
    }

    const char& operator[](size_t index) const {
        return str[index];
    }

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

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

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

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

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

    // != 运算符
    bool operator!=(const My_string &other) const {
        return !operator==(other);
    }

    // 输入运算符
    friend istream& operator>>(istream &input, My_string &myStr) {
        delete[] myStr.str;
        input >> myStr.str;
        myStr.len = strlen(myStr.str);
        return input;
    }

    // 输出运算符
    friend ostream& operator<<(ostream &output, const My_string &myStr) {
        output << myStr.str;
        return output;
    }

    // 获取字符串长度
    size_t size() const {
        return len;
    }
};

int main() {
    My_string s1("Hello");
    My_string s2("World");
    My_string s3 = s1 + s2;  // 使用 +
    My_string s4;
    s4 += s1;  // 使用 += 与字符串
    s4 += '!';  // 使用 += 与字符

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

    if (s1 < s2) {
        cout << "s1 is less than s2" << endl;
    }

    if (s1 == s2) {
        cout << "s1 is equal to s2" << endl;
    }

    My_string s5("Moonshot");
    My_string s6("Moonshot");
    cout << "s5 == s6: " << (s5 == s6 ? "true" : "false") << endl;

    return 0;
}

2.

#include <iostream>
#include <vector>
#include <stdexcept>  // for std::out_of_range

template <typename T>
class My_stack {
private:
    std::vector<T> data;

public:
    // 构造函数
    My_stack() {}

    // 析构函数
    ~My_stack() {}

    // 赋值运算符
    My_stack& operator=(const My_stack& other) {
        if (this != &other) {
            data = other.data;
        }
        return *this;
    }

    // 元素访问
    T top() const {
        if (empty()) {
            throw std::out_of_range("Stack is empty");
        }
        return data.back();
    }

    // 容量
    bool empty() const {
        return data.empty();
    }

    size_t size() const {
        return data.size();
    }

    // 修改器
    void push(const T& value) {
        data.push_back(value);
    }

    template <typename... Args>
    void emplace(Args&&... args) {
        data.emplace_back(std::forward<Args>(args)...);
    }

    void pop() {
        if (empty()) {
            throw std::out_of_range("Stack is empty");
        }
        data.pop_back();
    }

    void swap(My_stack& other) {
        data.swap(other.data);
    }
};

int main() {
    My_stack<int> stack;

    stack.push(10);
    stack.push(20);
    stack.push(30);

    std::cout << "Top element: " << stack.top() << std::endl;  // 输出 30

    stack.pop();
    std::cout << "Top element after pop: " << stack.top() << std::endl;  // 输出 20

    std::cout << "Size: " << stack.size() << std::endl;  // 输出 2

    if (stack.empty() == false) {
        std::cout << "Stack is not empty" << std::endl;
    }

    My_stack<int> anotherStack;
    anotherStack.push(40);
    anotherStack.push(50);

    anotherStack.swap(stack);

    std::cout << "Top element after swap: " << anotherStack.top() << std::endl;  // 输出 20
    std::cout << "Top element after swap: " << stack.top() << std::endl;  // 输出 50

    return 0;
}

3.C++ 类成员和特性

├── 友元(friend)
│   ├── 友元函数
│   │   ├── 全局函数作为友元
│   │   └── 其他类中的成员函数作为友元
│   └── 友元类
│       ├── 允许友元类的所有成员函数访问私有成员
│       └── 设置格式:friend class 类名;

├── 常成员(const)
│   ├── 常成员属性
│   │   └── 定义:在成员变量前加const
│   ├── 常成员函数
│   │   ├── 定义:在成员函数后加const
│   │   ├── 功能:保护成员变量不被修改
│   │   └── this指针:const 类名 * const this;
│   ├── 常对象
│   │   ├── 定义:const 类名 对象名;
│   │   ├── 调用:只能调用常成员函数
│   │   └── 限制:不能修改成员变量的值
│   └── mutable关键字
│       ├── 定义:mutable 数据类型 成员变量名;
│       └── 作用:允许在常成员函数中修改成员变量

├── 运算符重载(operator)
│   ├── 算术运算符
│   │   ├── 种类:+、-、*、/、%
│   │   └── 规则:同类临时变量作为结果
│   ├── 赋值运算符
│   │   ├── 种类:=、+=、-=、*=、/=、%=
│   │   └── 结果:返回值自身的引用
│   ├── 关系运算符
│   │   ├── 种类:>、<、==、>=、<=、!=
│   │   └── 结果:bool类型结果
│   ├── 单目运算符
│   │   ├── 种类:!、~、-(负号)、&(取地址)
│   │   └── 结果:根据情况而定
│   ├── 自增自减运算符
│   │   ├── 种类:++、--
│   │   └── 结果:同类的类对象、引用
│   ├── 插入和提取运算符
│   │   └── 定义:<< 和 >>
│   └── 类型转换运算符
│       └── 定义:operator 要转的类型 ()

└── 静态成员(static)
    ├── 静态成员变量
    │   ├── 定义:static 数据类型 变量名
    │   ├── 特点:独立于类对象存在
    │   └── 访问:类名或对象名访问
    └── 静态成员函数
        ├── 定义:static 返回值类型 函数名(形参列表)
        └── 特点:只能访问静态成员变量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值