商店销售某一商品,上带你每天公布统一的折扣(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
请按任意键继续·······