9.11-QT-QT的基本使用

实现一个简单的登陆界面:

实现Mystring:

cpp 复制代码
#include <iostream>
#include <cstring>
using namespace std;

class MyString {
private:
    char str[128];
public:
    MyString() {
        cout << "请输入字符串内容" << endl;
        cin >> str;
    }
    MyString(const char str[]) {
        strcpy(this->str, str);
    }
    MyString(const MyString &p) {
        strcpy(this->str, p.str);
    }
    void Long() {
        cout << strlen(str) << endl;
    }
    string Add(const MyString &p) {
        char temp[256] = {0};
        strcpy(temp, str);
        strcat(temp, p.str);
        cout << temp << endl;
        return temp;
    }
    bool Compare(const MyString &p) {
        if (strcmp(str, p.str) == 0) {
            cout << "true" << endl;
            return true;
        } else {
            cout << "false" << endl;
            return false;
        }
    }
    void show() {
        cout << "str = " << str << endl;
    }
};

int main() {
    MyString s1;
    MyString s2("world");
    MyString s3 = s2;
    s3.show();
    s1.Long();
    s1.Add(s2);
    s1.Compare(s2);
    return 0;
}

实现图书管理:

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Book
{
private:
    string name;
    string author;
    int num;
    static vector<string> books;
public:
    Book() {}
    Book(string n, string author, int num) : name(n), author(author), num(num)
    {
        books.push_back(n);
    }
    void LendBooks(int num)
    {
        if (this->num < num)
        {
            cout << "库存不足" << endl;
        }
        else
        {
            this->num -= num; // 借出后库存减少
        }
    }
    void GiveBack(int num)
    {
        this->num += num;
    }
    void seek(const Book &p)
    {
        bool found = false;
        for (const auto &book : books)
        {
            if (book == p.name)
            {
                cout << "书名: " << name << " 作者: " << author << " 数量:" << num << endl;
                found = true;
                break; // 找到后退出循环
            }
        }
        if (!found)
        {
            cout << "未找到该书" << endl;
        }
    }
    void show()
    {
        cout << "书名: " << name << " 作者: " << author << " 数量:" << num << endl;
    }
};
vector<string> Book::books; // 静态成员初始化

int main()
{
    Book b1("斗罗大陆", "唐家三少", 20);
    Book b2("斗破苍穹", "天蚕土豆", 40);
    b1.LendBooks(2);
    b1.show();
    b2.GiveBack(20);
    b2.show();
    b1.seek(b2);
    return 0;
}
相关推荐
灰子学技术1 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
二十雨辰2 小时前
[python]-AI大模型
开发语言·人工智能·python
Yvonne爱编码2 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚2 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂2 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
pas1362 小时前
41-parse的实现原理&有限状态机
开发语言·前端·javascript
琹箐2 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
Monly213 小时前
Java:修改打包配置文件
java·开发语言
我命由我123453 小时前
Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响
android·java·开发语言·java-ee·android studio·android-studio·android runtime
island13143 小时前
CANN ops-nn 算子库深度解析:核心算子(如激活函数、归一化)的数值精度控制与内存高效实现
开发语言·人工智能·神经网络