仓库管理练习

仓库管理

创立一个货物的类

  • 每一个货物都是一个对象
  • 每一个对象都是一个结点
  • 类的静态成员变量必须在类外初始化,不仅如此,类的静态成员变量最好在源文件中初始化,而不能在头文件初始化,否则,编译的时候就会报错
//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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_Csyn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值