注释
本题是《C++面向对象程序设计(第2版)》第3章第9题
题目简介:商品打折,对一次购10件以上者,9.8折。计算总销售款sum以及每件商品的平均售价average。
销货员号(num) | 销货件数(quantity) | 销货单价(price) |
---|---|---|
101 | 5 | 23.5 |
102 | 12 | 24.56 |
103 | 100 | 21.5 |
// An highlighted block
var foo = 'bar';
#include<iostream.h>
class Clerk{
public:
Clerk(int n,int q,double p):num(n),quantity(q),price(p){}
void total(){
if(quantity<10)
sum+=quantity*price;
else
sum+=0.98*quantity*price;
count+=quantity;
}//计数、求和
static double average(){
return(sum/count);
}//静态数据成员,求平均
private:
int num;
int quantity;
double price;
static double sum;
static int count;
};
double Clerk::sum=0;
int Clerk::count=0;
int main(){
Clerk man[3]={
Clerk(101,5,23.5),
Clerk(102,12,24.56),
Clerk(103,100,21.5)
};
for(int i=0;i<3;i++)
man[i].total();
cout<<"average: "<<Clerk::average()<<endl;
return 0;
}