9.11QT作业

cpp 复制代码
#include <iostream>
#include <string.h>
 
using namespace std;
 
class MyString
{
private:
    char *p;
public:
    MyString():p(nullptr){}
    MyString(const char *str)
    {
        if (str)
        {
            p = new char[strlen(str) + 1];
            strcpy(p, str);
        }
        else
        {
            p = new char[1];
            *p = '\0';
        }
    }
    MyString(MyString &other)
    {
        p = new char[strlen(other.p) + 1];
        strcpy(p, other.p);
    }
    ~MyString()
    {
            delete[] p;
    }
    MyString &operator=(const MyString &other)
    {
        if (this != &other)
        {
            delete[] p;
            p = new char[strlen(other.p) + 1];
            strcpy(p, other.p);
        }
        return *this;
    }
    MyString &operator+=(const MyString &q)
    {
        size_t old_len = strlen(p);
        size_t new_len = old_len + strlen(q.p) + 1;
        char *new_p = new char[new_len];
        strcpy(new_p, p);
        strcat(new_p, q.p);
        delete[] p;
        p = new_p;
        return *this;
    }
    int size()const
    {
        return strlen(p);
    }
    bool operator>(const MyString &q) const
    {
        return strcmp(p, q.p) > 0;
    }
};
 

int main()
{
    MyString s1("hello");
    MyString s2;
    s2=s1;
    s1.size();
    MyString s3;
    s3=s1+s2;
    cout<<s3;
    cin>>s2;
    cout<<s2;
    if(s1>s2)
    {
        cout<<"s1>s2";
    }
    else
    {
        cout<<"s2>=s1";
    }
}
cpp 复制代码
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class BookManger
{
private:
    string bookname;
    string writename;
    int num;
    static vector<string> Books;
public:
    BookManger(){}
    BookManger(string bookname , string writename, int num):bookname(bookname),writename(writename),num(num)
    {
        Books.push_back(bookname);
    }
    void addbook()
    {
        cout <<"书名:";
        cin >> bookname;
        Books.push_back(bookname);
        cout <<"作者:";
        cin >> writename;
        cout <<"库存数量:";
        cin >> num;
    }

    void lendbook(int lnum)
    {
        if(lnum<=num)
        {
            num = num - lnum;
        }
        else if(lnum > num)
        {
            cout << "剩余库存不足" << endl;
            cout << "剩余库存数量:" << num << endl;
        }
    }
    void sendbook(int snum)
    {
        num = num + snum;
        cout << "库存数量" << num << endl;
    }
    void searchbook(const BookManger &book)
    {
        for(const auto book : Books)
        {
            if(book==bookname)
            {
                cout << "书名:" << bookname << " 作者:" <<writename <<" 数量:"<< num <<endl;
            }
        }
        cout << "未找到该书" <<endl;
    }
    void show()
    {
        cout << "书名:" << bookname << "作者:" << writename << "库存数量:" << num << endl;
    }
};

vector<string> BookManger::Books={};

int main()
{
    BookManger s1("111","ygq",5);
    BookManger s2;
    s2.addbook();
    s1.show();
//    s1.lendbook(7);
//    s1.lendbook(3);
//    s1.show();
//    s1.sendbook(2);
//    s1.show();
    return 0;
}

思维导图

相关推荐
fenglllle9 分钟前
JDK8升级JDK17使用CompletableFuture在线程中classloader的变化
java·开发语言·jvm
froginwe1110 分钟前
Scala 正则表达式
开发语言
时寒的笔记11 分钟前
11期_js逆向核心案例解析(sichuan&某理财网)
开发语言·javascript·ecmascript
csbysj202013 分钟前
PHP 文件:深入解析与最佳实践
开发语言
JAVA面经实录91718 分钟前
Java+SpringAI企业级实战项目完整官方文档(生产终版)
java·开发语言·spring·ai编程
梵得儿SHI18 分钟前
Java IO 流进阶:Buffer 与 Channel 核心概念解析及与传统 IO 的本质区别
java·开发语言·高并发·nio·channel·buffer·提升io效率
j_xxx404_21 分钟前
Linux线程:从内存分页机制(Page Table/TLB/Page Fault)彻底读懂 Linux 线程本质
linux·运维·服务器·开发语言·c++·人工智能·ai
2301_7890156223 分钟前
C++_string增删查改模拟实现
java·开发语言·c++
星河耀银海24 分钟前
JAVA 注解(Annotation):从原理到实战应用
java·开发语言·数据库
AI人工智能+电脑小能手26 分钟前
【大白话说Java面试题 第68题】【JVM篇】第28题:对于 JDK 自带的监控和性能分析工具用过哪些?一般你怎么用的?
java·开发语言·jvm·面试