#include<iostream>
#include<iomanip>
using namespace std;
//商店銷售某一商品,每天公布統一的折扣
class Goods
{
public:
Goods(int nu,int q,double p):num(nu),quantity(q),price(p){}
void total();
static double average();
void display();
private:
static double discount;
static double sum;
static int n;
int num;
int quantity;
double price;
};
void Goods::total()
{
double rate = 0.95; //書上似乎沒有提到這個統一的折扣是95折。。。
if (quantity > 10)
sum += quantity * price * rate * discount;
else sum += quantity * price * rate ;
n += quantity;
}
double Goods::average()
{
return sum / n;
}
void Goods::display()
{
cout << "當日總貨款:" << sum << endl;
cout << "當日平均售價:" << setiosflags(ios::fixed) << setprecision(2) << average() << endl;
}
double Goods::discount = 0.98;
double Goods::sum = 0;
int Goods::n = 0;
int main()
{
Goods test[3] = {
Goods(101,5,23.5),
Goods(102,12,24.56),
Goods(103,100,21.5)
};
for (int i = 0; i < 3; i++)
test[i].total();
test[2].display();
return 0;
}
譚浩強c++第三版9-9