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;
}
相关推荐
峥嵘life16 分钟前
2026免费的 opencode 使用分享:Windows端 + 服务器CLI 实战总结
android·大数据·开发语言
纪念 22924 分钟前
C++类和对象(二)
开发语言·c++
泡海椒32 分钟前
Java 后端最优 PDF 导出方案:jquick-pdf 项目引入与快速测试
java·开发语言·pdf
Evand J33 分钟前
【MATLAB例程】二维Astar路径规划与AOA-TDOA定位仿真。完整代码,附下载链接。包运行成功,带中文注释
开发语言·matlab·路径规划·代码·定位·融合定位
李永奉35 分钟前
中科蓝讯SDK开发-耳机项目功耗问题排查教程
c语言·开发语言·嵌入式硬件·物联网·智能手机
linx2951 小时前
第七章 · 标准库容器、算法与 ranges
c语言·开发语言·数据结构·c++·算法
峥嵘life1 小时前
2026华为AI码道 CodeArts 使用分享:Windows端 + 服务器CLI 实战总结
android·大数据·开发语言·python
x秀x1 小时前
sam3新手使用教程
开发语言·python
Java后端的Ai之路1 小时前
大模型LLM评估完全指南
开发语言·人工智能·python·llm·评估
Brilliantwxx2 小时前
【STM32】 从HAL库源码深度解析I2C(源码解析+面试题)
开发语言·stm32·单片机·嵌入式硬件·架构