【静态成员】

商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,单价不一样,因此商店需要记录下目前库存的货物的总重量和总价值。编写一个程序,通过定义类Carlo来模拟商店货物购进和卖出的情况。

(本题目主要练习静态数据成员的使用,定义私有变量存每件货物的价格和重量,用静态数据成员存货物的总重量和总价钱,定义构造函数和析构函数,当定义新的对象完成初始化的功能和删除对象时,从总重量和总价钱中减去对象的重量和价格)

// Menu.h

#ifndef MENU_H
#define MENU_H
class Menu
{	public:
		int show();
};
#endif;
// Carlo.h
#include <iostream>
#include <windows.h>
using namespace std;
class Carlo
{public:
 Carlo(double Weight = 0,double Price = 0);
		 virtual         ~Carlo();
	     void            SetCarlo(double = 0,double = 0);
		 const double    &GetCurrentTotalWeight() const; 
		 const double    &GetCurrentTotalPrice() const;  
		 void            BuyBox();
		 void            SellBox();
		 void            ShowBoxInfor() const;
      protected:             
	     static  double  TotalWeight;
	     static  double  TotalPrice;       
      private:
         double  BoxWeight;
	     double  BoxPrice;
};



//Carlo.cpp
//#include <iostream>
//#include "Carlo.h"
double Carlo::TotalPrice=0.0;
double Carlo::TotalWeight=0.0;

Carlo::Carlo(double Weight, double Price)
{	 
	 BoxWeight = Weight;
     BoxPrice  = Price;
     TotalPrice += Weight * Price;
     TotalWeight += Weight;
}

void Carlo::BuyBox()
{
	double Weight,Price;
	cout<<"Please input the Weight and Price of buy: ";
	cin>>Weight>>Price;
	TotalWeight+=BoxWeight;
	TotalPrice+=BoxPrice*BoxWeight;
}

const double &Carlo::GetCurrentTotalPrice() const
{       
	return TotalPrice;          
}

const double &Carlo::GetCurrentTotalWeight() const
{        
	return TotalWeight;
}

void Carlo::SetCarlo(double Weight, double Price)
{
	double Weight1,Price1;
	cout<<"Weight,Price: ";
	cin>>Weight1>>Price1;
	BoxWeight = Weight1;
	BoxPrice= Price1;
}

void Carlo::SellBox()
{
	double Weight,Price;
	cout<<"Please input the Weight and Price of sell:"<<endl;
    cin>>Weight>>Price;
	TotalWeight-=BoxWeight;
	TotalPrice-=BoxPrice*BoxWeight;
}

void Carlo::ShowBoxInfor() const
{
	system("cls");
	cout<<"库存总重量:"<<TotalWeight<<endl;
	cout<<"库存总价:"<<TotalPrice<<endl;
	cin.get();
}
Carlo::~Carlo()
{
}
//Menu.cpp
//#include <iostream>
//#include "Menu.h"
int Menu::show()
{       system("cls");
    	int choice;
	cout<<"1.购进货物"<<endl;
	cout<<"2.卖出货物"<<endl;
	cout<<"3.库存信息"<<endl;
	cout<<"4.退出"<<endl;
	cin>>choice;
	cin.get();
	return (choice);   
}
//CarloDemo.cpp
//#include "Menu.h"
//#include "Carlo.h"
int main()
{   Menu menu;
	Carlo carlo;
    int chioce;
    while( (chioce = menu.show()) - 4)
	{   switch(chioce)
		{    case 1: carlo.BuyBox();
				     break;
			 case 2: carlo.SellBox();
				     break;
			 case 3: carlo.ShowBoxInfor();
				     break;
			 case 4:
					 return 0;
		}
	}
	return 0;
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@白白吖~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值