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

思维导图

相关推荐
世转神风-18 分钟前
qt-字符串版本与数值版本互转
开发语言·qt
极客代码36 分钟前
深入解析C语言中的函数指针:原理、规则与实践
c语言·开发语言·指针·状态机·函数·函数指针
w-w0w-w1 小时前
C++模板参数与特化全解析
开发语言·c++
不绝1911 小时前
C#核心:继承
开发语言·c#
AI即插即用2 小时前
即插即用系列(代码实践)专栏介绍
开发语言·人工智能·深度学习·计算机视觉
码农水水2 小时前
蚂蚁Java面试被问:混沌工程在分布式系统中的应用
java·linux·开发语言·面试·职场和发展·php
喵了meme2 小时前
c语言经验分享
c语言·开发语言
Knight_AL2 小时前
用 JOL 验证 synchronized 的锁升级过程(偏向锁 → 轻量级锁 → 重量级锁)
开发语言·jvm·c#
啊阿狸不会拉杆3 小时前
《数字图像处理》第 4 章 - 频域滤波
开发语言·python·数字信号处理·数字图像处理·频率域滤波