SALES.h
#ifndef SALES_H_ #define SALES_H_ //测试这一句的作用 namespace SALES{ class Sales{ const static int QUARTERS=4; private: double sales[QUARTERS]; double average; double max; double min; public: Sales(const double ar[],int n); Sales(); void showS(int n){ for(int i=0;i<n;i++) std::cout<<sales[i]<<" ";//用于方便显示数组内容; }; void showSales(); };//不能缺少";"?? }; #endif
SALES.cpp
#include <iostream> #include "SALES.h" using namespace std; //using namespace SALES; static int m=0; SALES::Sales::Sales(const double ar[],int n) { bool flag=false;//用于判断max,min,avg是否要设置为0; double Max=-999; double Min=999; double Avg=0; double Sum=0; if(n<0) cout<<"\nError input!"<<endl; if(n>4) { n=4; m=n; } if(n<=4&&n>0) { m=n;//记得要及时更新m的值,否则showS(m)容易出错 flag=true;//这里就不是设置为0; for(int i=0;i<n;i++) { sales[i]=ar[i]; Sum+=sales[i]; if(Max<sales[i]) Max=sales[i]; if(Min>sales[i]) Min=sales[i]; } } //设置max,min,avg if(flag) { Avg=Sum*1.0/m; max=Max; min=Min; average=Avg; } else { max=0; min=0; average=0; } } SALES::Sales::Sales() { m=4;//切记更新 double Max=-999; double Min=999; double Avg=0; double Sum=0; int i=0; while(i<4) { cout<<"\nPlease enter the number:"; cin>>sales[i]; Sum+=sales[i]; if(Max<sales[i]) Max=sales[i]; if(Min>sales[i]) Min=sales[i]; i++; } Avg=Sum*1.0/m; max=Max; min=Min; average=Avg; } void SALES::Sales::showSales() { cout<<"\nshow on!"<<endl; cout<<"arr is:"; //SALES::Sales:: showS(m); cout<<"\n"<<"max="<<max<<" min="<<min<<" avg="<<average<<endl; cout<<"show end!"<<endl; }
main104.cpp
#include <iostream> #include "SALES.h" using namespace std; using namespace SALES; void main104() { double arr[6]={10,5.5,4.5,7.4,5.6,9.7}; Sales s1(arr,4); s1.showSales(); Sales s2=Sales(); s2.showSales(); system("pause"); }
——————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:https://www.royalchen.com/
author:royalchen
Email:royalchen@royalchen.com
———————————————————————————————————————————————————