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;
}
相关推荐
froginwe1119 分钟前
C# 循环
开发语言
EnCi Zheng26 分钟前
Java_钻石操作符详解
java·开发语言
行者..................35 分钟前
petalinux 安装Openblass库
qt
拾忆,想起1 小时前
RabbitMQ事务机制深度剖析:消息零丢失的终极武器
java·开发语言·分布式·后端·rabbitmq·ruby
IvanCodes1 小时前
八、Scala 集合与函数式编程
大数据·开发语言·scala
Never_Satisfied2 小时前
在JavaScript / HTML中,浏览器提示 “Refused to execute inline event handler” 错误
开发语言·javascript·html
Never_Satisfied2 小时前
在JavaScript / HTML中,事件监听的捕获和冒泡阶段解析
开发语言·javascript·html
HalvmånEver2 小时前
初学者入门 C++ map 容器:从基础用法到实战案例
开发语言·c++·学习·map
毕设源码-朱学姐3 小时前
【开题答辩全过程】以 python基于Hadoop的服装穿搭系统的设计与实现为例,包含答辩的问题和答案
开发语言·hadoop·python
爱砸键盘的懒洋洋3 小时前
Python第四课:数据类型与转换
开发语言·python