仓库管理练习

    科技2022-07-10  111

    仓库管理

    创立一个货物的类

    每一个货物都是一个对象每一个对象都是一个结点类的静态成员变量必须在类外初始化,不仅如此,类的静态成员变量最好在源文件中初始化,而不能在头文件初始化,否则,编译的时候就会报错 //Goods.cpp #include "Goods.h" int Goods::total_weight = 0;//不能放在.h文件,会被重复定义 void buy(Goods*& head, int w) { Goods* new_goods = new Goods(w); //cout << "收到"<<endl; if (head == NULL) { head = new_goods; } else { new_goods->next = head; head = new_goods; } } int Goods::get_total_weight() { return total_weight; } void sale(Goods*& head) { if (head == NULL) { cout << "仓库中没有货物" << endl; return; } Goods* temp = head; head = head->next; delete temp; cout << "卖了" << endl; } //Goods.h #pragma once #include<iostream> using namespace std; class Goods { public: Goods() { weight = 0; next = NULL; cout << "创建一个重量为" << weight << "的货物" << endl; } Goods(int w) { //需要创建一个质量为w的货物,并且仓库加上这个重量 weight = w; next = NULL; total_weight += w; } ~Goods() { //仓库减少这个货物的重量 cout << "删除了一箱重量是" << weight << "的货物" << endl; total_weight -= weight; } static int get_total_weight(); Goods* next; private: int weight;//货物重量 static int total_weight;//仓库的总重量 }; void buy(Goods*& head, int w); void sale(Goods*& head); //main.cpp #include"Goods.h" int main(void) { int chioce = 0; Goods* head = NULL; int w; do { cout << "1 进货" << endl; cout << "2 出货" << endl; cout << "0 退出" << endl; cin >> chioce; switch (chioce) { case 1://进货 cout << "请输入要创建货物的重量" << endl; cin >> w; buy(head, w); break; case 2://出货 sale(head); break; case 0://退出 system("pause"); return 0; default: cout << "输入错误,重新输入" << endl; break; } cout << "此时仓库有" << Goods::get_total_weight() << "重量的货物" << endl; } while (1); system("pause"); return 0; }
    Processed: 0.011, SQL: 8