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;
}

思维导图

相关推荐
bin91533 分钟前
AI工具赋能Python开发者:项目开发中的创意守护与效率革命
开发语言·人工智能·python·工具·ai工具
被放养的研究生6 分钟前
Python常用的一些语句
开发语言·python
fox_lht2 小时前
第一章 不可变的变量
开发语言·后端·rust
骁的小小站3 小时前
Verilator 和 GTKwave联合仿真
开发语言·c++·经验分享·笔记·学习·fpga开发
心灵宝贝5 小时前
申威架构ky10安装php-7.2.10.rpm详细步骤(国产麒麟系统64位)
开发语言·php
lly2024065 小时前
PHP 字符串操作详解
开发语言
像是套了虚弱散7 小时前
DevEco Studio与Web联合开发:打造鸿蒙混合应用的全景指南
开发语言·前端·华为·harmonyos·鸿蒙
旭意7 小时前
C++蓝桥杯之结构体10.15
开发语言·c++
麦麦鸡腿堡8 小时前
Java的单例设计模式-饿汉式
java·开发语言·设计模式
简单点了8 小时前
go前后端项目的启动 、打包和部署
开发语言·后端·golang