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;
}
相关推荐
lead520lyq几秒前
Golang Grpc接口调用实现账号密码认证
开发语言·后端·golang
EQ-雪梨蛋花汤几秒前
【问题反馈】JNI 开发:为什么 C++ 在 Debug 正常,Release 却返回 NaN?
开发语言·c++
naruto_lnq1 分钟前
高性能消息队列实现
开发语言·c++·算法
charlie1145141911 分钟前
malloc 在多线程下为什么慢?——从原理到实测
开发语言·c++·笔记·学习·工程实践
kyrie学java4 分钟前
SpringWeb
java·开发语言
写代码的【黑咖啡】6 分钟前
Python 中的 Gensim 库详解
开发语言·python
饺子大魔王的男人2 小时前
Remote JVM Debug+cpolar 让 Java 远程调试超丝滑
java·开发语言·jvm
兩尛8 小时前
c++知识点2
开发语言·c++
fengfuyao9858 小时前
海浪PM谱及波形的Matlab仿真实现
开发语言·matlab
xiaoye-duck9 小时前
C++ string 底层原理深度解析 + 模拟实现(下)——面试 / 开发都适用
开发语言·c++·stl