日常练习,c++的静态数据成员和静态成员函数。

商店销售某一商品,上带你每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购买10件以上者,还可以享受98折优待。

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
class Story
{
public:
	Story(int n, int q, float p) :num(n), quantity(q), price(p) {}
	void total();
	static void display();
	static float average();
private:
	
	int num;
	int quantity;
	float price;
	static float discount;
	static float sum;
	static int count;
};
void Story::total()
{
	float rate = 1.0;
	if (quantity > 10)
		rate = 0.98*rate;
	sum = sum + quantity * price*rate*(1 - discount);
	count = count + quantity;
}
void Story::display()
{
	cout << sum << endl;
	cout << average() << endl;
}
float Story::average()
{
	return(sum / count);
}
float Story::sum = 0;
float Story::discount = 0.05;
int Story::count = 0;
int main()
{
	Story sto[3] =
	{
		Story(101, 5, 23.5),
		Story(102, 12, 24.56),
		Story(103, 100, 21.5)
	};
	int i;
	for (i = 0; i < 3; i++)
		sto[i].total();
	Story::display();
	system("pause");
	return 0;
}

结果:2387.66
20.4073
请按任意键继续·······

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值