仓库管理
创立一个货物的类
每一个货物都是一个对象每一个对象都是一个结点类的静态成员变量必须在类外初始化,不仅如此,类的静态成员变量最好在源文件中初始化,而不能在头文件初始化,否则,编译的时候就会报错
#include "Goods.h"
int Goods::total_weight = 0;
void buy(Goods*& head, int w)
{
Goods* new_goods = new Goods(w);
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;
}
#pragma once
#include<iostream>
using namespace std;
class Goods
{
public:
Goods()
{
weight = 0;
next = NULL;
cout << "创建一个重量为"
<< weight << "的货物"
<< endl;
}
Goods(int 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);
#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;
}
转载请注明原文地址:https://blackberry.8miu.com/read-861.html