面向对象的程序设计-26-对于商品库存量计算的实例

///*作业一:
//某商店经销一种货物。货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,因此,商店需要记录下目录库存的总重量。
//现在用C++模拟商店货物购进和卖出的情况,设计一个可以统计货物总重量的程序。要求定义一个Goods类,完成此功能。*/
//#include <iostream>
//using namespace std;
//class Goods
//{
// int mass;//质量
// static int i;//记录箱数
//public:
// Goods(){mass=0;}
// Goods(int m){mass=m;++i;}
// int read_mass(){return mass;}
// static int read_total(){return i;};
// int sell(){return -mass;}
// int buy(){return mass;}
//};
//int Goods::i=0;
//void main()
//{
// Goods g1(20),g2(30),g3(50);
// cout<<"经手的箱数:"<<Goods::read_total()<<endl;
// int total=0;
// total=g1.buy()+g2.sell()+g3.buy();
// cout<<"所有的存放质量:"<<total<<endl;
//
//};
/*
以前的例子,对于同一品种货物的买进与卖出的描述。建立该货物对象后,买进多少,再卖出多少,能够计算当下的库存量
*/
#include <iostream>
using namespace std;
class Goods
{
double total,remain,saled;
public:
Goods(){total=remain=saled=0;}
Goods(double i){total=remain=i;saled=0;}
void Buy(double i)
{
remain=total=i+remain;
cout<<"购入货物总量(kg):"<<i<<endl;
cout<<"目前库存剩余总量(kg):"<<remain<<endl;
}


void Sale()
{
double j;
cout<<"输入卖出货物重量(kg):";
cin>>j;
if(remain<j){cout<<"库存不足!"<<endl;}
else
{
remain-=j;saled+=j;
cout<<"已卖出货物(kg):"<<saled<<endl<<"目前库存剩余总量(kg):"<<remain<<endl;
}
}
};
void main()
{
Goods g(500);
g.Buy(300);g.Sale();g.Sale();
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值