Create a class for a salesman that has a data member of an array of double data
ID: 3537063 • Letter: C
Question
Create a class for a salesman that has a data member of an array of double data type as the sales of some number of quarters, a data member average as the average of the sales of all quarters, another data member as max as the maximum of the sales of all quarters, and one data member min as the minimum of sales of all quarters.
After that write a default constructor and a user-defined constructor. Default constructor should ask user the number of quarters and enter the sales, calculate average, maximum, and minimum of all sales. User-defined constructor should pass an array of double as the sales and the number of quarters by constant reference and calculate average, maximum, and minimum. Write a member function that displays all of the information about this salesman. Write a main to test it.
note: i need this program in three files sales.h, salesman.cpp, main cpp where i call functions. use ms visual studio
Explanation / Answer
Sales.h
#ifndef SALES_H
#define SALES_H
class Salesman
{
public:
double sales[100];
double average;
double maximum;
double minimum;
Salesman();
Salesman(double [],int );
double getAvg();
double getMax();
double getMin();
};
#endif
Salesman.cpp
#include "Sales.h"
#include<iostream>
#include <cstdlib>
Salesman::Salesman()
{
int quarters,i=0;
std::cout<<"Enter the number of quarters: ";
std::cin>>quarters;
double avg=0;
double minv=0;
double maxv=0;
for(i=0;i<quarters;i++)
{
std:: cout<<"Enter the sale in quarter: "<<(i+1)<< std::endl;
std::cin>>sales[i];
avg=avg+sales[i];
if(i==0)
{
minv=sales[i];
maxv=sales[i];
}
else
{
if(minv>sales[i])
{
minv=sales[i];
}
if(maxv<sales[i])
{
maxv=sales[i];
}
}
}
avg=avg/quarters;
average=avg;
maximum=maxv;
minimum=minv;
}
Salesman::Salesman(double salearray[],int quarters)
{
int i=0;
double avg=0;
double minv=0;
double maxv=0;
for(i=0;i<quarters;i++)
{
sales[i]=salearray[i];
avg=avg+sales[i];
if(i==0)
{
minv=sales[i];
maxv=sales[i];
}
else
{
if(minv>sales[i])
{
minv=sales[i];
}
if(maxv<sales[i])
{
maxv=sales[i];
}
}
}
}
double Salesman:: getAvg()
{
return average;
}
double Salesman:: getMin()
{
return minimum;
}
double Salesman:: getMax()
{
return maximum;
}
main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "Sales.h"
using namespace std;
int main()
{
int i=0,q;
double arr[100];
Salesman s;
cout<<"Average is "<<s.getAvg()<<endl;
cout<<"Max sale is "<<s.getMax()<<endl;
cout<<"Min sales is "<<s.getMin()<<endl;
cout<<"Test for Userdefined constructor:"<<endl;
cout<<"ENter the no of quarter"<<endl;
cin>>q;
for(i=0;i<q;i++)
{
cout<<"ENter the sales in quarter: "<<i<<endl;
cin>>arr[i];
}
Salesman p(arr,q);
cout<<"Average is "<<p.getAvg()<<endl;
cout<<"Max sale is "<<p.getMax()<<endl;
cout<<"Min sales is "<<p.getMin()<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.