当前编程题: 实验2-类和对象的进一步讨论---静态数据成员和静态成员函数应用
3.【问题描述】某商品定价为30元。现在商店在搞促销活动,所有商品都会有固定的折扣,具体折扣当天宣布。现有3个销售员卖此商品。请根据销售的数量计算出当日此商品的总销售额及平均售价。设计main()函数,测试程序。本题已知的3个销售员的工号及销售此商品的数量为:1001、5;1002、20;1003、75。 【输入形式】输入占一行,为当天折扣。 【输出形式】输出占一行,有两个数。第1个数为当日此商品的总销售额;第2个数为平均售价。各数据用空格间隔。 【样例输入】8 【样例输出】2760 27.60 【样例说明】当天折扣为8%,即按原价92%销售。当日此商品的总销售额为2760;平均售价为27.60。 【评分标准】本题共20分,2个测试点,每个测试点10分。
【编程说明】本题必须使用类,在main()函数中按照给定的数值创建对象,通过调用函数完成所需计算。将商品价格、折扣、总销售款和商品销售总件数声明为静态数据成员。适当使用静态成员函数。非基于对象的程序不得分。
#include<iostream> #include<iomanip> using namespace std; int n; class discount { public: discount(int j,int n):jnum(j),num(n){} void total(); static float handle(); private: static int price,sum; int jnum; int num; }; int discount::sum=0; int discount::price=30; void discount::total() { sum+=num; } float discount::handle() { return(1.0*sum*price*(100-n)/100); } int main() { discount person1(1001,5),person2(1002,20),person3(1003,75); cin>>n; person1.total(); person2.total(); person3.total(); cout<<discount::handle()<<" "; cout<<setiosflags(ios::fixed)<<setprecision(2); cout<<1.0*discount::handle()/100<<endl; return 0; }