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;
}
相关推荐
修炼前端秘籍的小帅4 分钟前
精读《JavaScript 高级程序设计 第4版》第6章 集合引用类型(三)Map、WeakMap、Set、WeakSet
开发语言·javascript·ecmascript
@LetsTGBot搜索引擎机器人16 分钟前
打造属于你的 Telegram 中文版:汉化方案 + @letstgbot 搜索引擎整合教程
开发语言·python·搜索引擎·机器人·.net
人工智能的苟富贵25 分钟前
使用 Tauri + Rust 构建跨平台桌面应用:前端技术的新边界
开发语言·前端·rust·electron
j_xxx404_27 分钟前
C++ STL:string类(3)|operations|string类模拟实现|附源码
开发语言·c++
GHZero1 小时前
Java 之解读String源码(九)
java·开发语言
Swift社区1 小时前
Lombok 不生效 —— 从排查到可运行 Demo(含实战解析)
java·开发语言·安全
南清的coding日记1 小时前
Java 程序员的 Vue 指南 - Vue 万字速览(01)
java·开发语言·前端·javascript·vue.js·css3·html5
@大迁世界1 小时前
我用 Rust 重写了一个 Java 微服务,然后丢了工作
java·开发语言·后端·微服务·rust
himobrinehacken1 小时前
c语言宏注意事项
c语言·开发语言
自在极意功。1 小时前
Java static关键字深度解析
java·开发语言·面向对象·static