USING C++ Class Declaration/definition including data members, static members, m
ID: 3697649 • Letter: U
Question
USING C++
Class Declaration/definition including data members, static members, member functions, and class implementation.
Problem Specification:
The XP corporation has four regions, each responsible for sales to a different geographic location. Design a DivSales class that keeps sales data for a division, with the following members:
A data member that holds the region name, possible values are (“East”, “West”, ”North”, “South”).
An array with four elements for holding four quarters of sales figures for the region.
A private static variable for holding the total corporate sales for all divisions for the entire year.
A constructor that initializes the name to null “”, and the quarters to 0;
A null destructor.
A member function that prompts the user to enter the region name and sales for four quarters for that region. The value entered should be added to the static variable that holds the yearly corporate sales.
A member function that returns the region name.
A function that takes an integer argument with the range 0 to 3. The argument is to be used as a subscript into the regions quarterly sales array. The function should return the value of the array element with that subscript.
A member function that returns the total sales for any given region.
A member function that returns the total sale for all regions.
Write a program that creates an array of four DivSales objects. The program should ask the user to enter the region’s name, and sales for four quarters for each region. After the data is entered, the program should display a table showing each region’s name followed by the sales for each quarter, and then followed by the total sale for the region. At the end, the program should display the total corporate sales for the year.
Sample input:
Enter Region : East
Quarter 1 : 32000
Quarter 2 : 34000
Quarter 3 : 45000
Quarter 4 : 55000
Enter Region : West
Quarter 1 : 1000
Quarter 2 : 2000
Quarter 3 : 3000
Quarter 4 : 4000
Enter Region : North
Quarter 1 : 2000
Quarter 2 : 4000
Quarter 3 : 6000
Quarter 4 : 8000
Enter Region : South
Quarter 1 : 3000
Quarter 2 : 6000
Quarter 3 : 9000
Quarter 4 : 12000
Sample output:
East
Quarter 1 32000.00
Quarter 2 34000.00
Quarter 3 45000.00
Quarter 4 55000.00
Region’s total : 166000.00
West
Quarter 1 1000.00
Quarter 2 2000.00
Quarter 3 3000.00
Quarter 4 4000.00
Regions total : 10000.00
North
Quarter 1 2000.00
Quarter 2 4000.00
Quarter 3 6000.00
Quarter 4 8000.00
Regions total : 20000.00
South
Quarter 1 3000.00
Quarter 2 6000.00
Quarter 3 9000.00
Quarter 4 12000.00
Regions total : 30000.00
All Regions Total : 226000.00
Extra work:
All Regions Quarter 1 39000.00
All Regions Quarter 2 46000.00
All Regions Quarter 3 63000.00
All Regions Quarter 4 79000.00
Requirements:
Comment thoroughly, each function should have its specification above it (2 or 3 lines).
The client program includes the function main where the array of objects is declared/instantiated.
All accessor member functions are constant.
The class DivSales is defined in a header file.
All methods are defined in an implementation file
Include the preprocessor #ifndef..endif as a guard.
Include a class diagram for the class DivSales.
main has sufficient comments to explain what is being done.
every function has specifications.
A header file contains the class definition.
A cpp file contains the functions definitions/implementations.
The #ifndef guard is properly used where necessary.
a constructor and destructor are defined and do what they supposed to.
a static variable accumulates the sales for all regions.
All accessor member functions are constant.
a UML Class diagram is submitted and is correct.
an array of objects is declared in main and calls a function to read the data for each of the regions.
Program runs efficiently, correctly and performs the specified task.
Structure of the program (neatness, spacing, indentation, use of variables, organization).
output is printed formatted and looks neat.
EXTRA: if at the end, the total of each quarter for the regions is printed by accessing a member function to accumulate the quarters (may require another static variable if you wish)
Explanation / Answer
Can help you with this:
#include <iostream>
using namespace std;
class sales
{
private:
static int Year_Sales;
int Qtrsale[4];
public:
void AddSales (int,int,int,int);
int Sales(int);
int getvalue()
{
return Year_Sales;
}
};
void DivSales::AddSales(int sale1, int sale2, int sale3, int sale4)
{
Qtrsale[0] = sale1;
Qtrsale[1] = sale2;
Qtrsale[2] = sale3;
Qtrsale[3] = sale4;
Year_Sales = Year_Sales + sale1 + sale2 + sale3 + sale4;
}
int DivSales::Sales(int n)
{
int value = Qtrsale[n];
return value;
}
void error()
{
system("cls");
cout << "Ufff you entered a negative value. Restart program and try again";
cout << "Press any key to restart program ";
system("pause");
exit(0);
}
int DivSales::Year_Sales;
int main()
{
const int DS = 4;
DivSales DivisionSale[DS];
int quarter, division;
for (division = 0; division < 4; division++)
{
int Qrt1, Qrt2, Qrt3, Qrt4;
int Q;
cout << "Enter Sales of Division: " << division + 1 << endl;
cout << "Enter Q1 Sales: ";
cin >> Qrt1;
Q = Qrt1;
if (Q < 0)
error();
cout << "Enter Q2 Sales: ";
cin >> Qrt2;
Q = Qrt2;
if (Q < 0)
error();
cout << "Enter Q3 Sales: ";
cin >> Qrt3;
Q = Qrt3;
if (Q < 0)
error();
cout << "Enter Q4 Sales: ";
cin >> Qrt4;
Q = Qrt4;
if (Q < 0)
error();
DivisionSale[division].AddSales(Qrt1,Qrt2,Qrt3,Qrt4);
}
cout << " ---------------------------------------- ";
cout << " " << "Q1" << " " << "Q2" << " " << "Q3" << " " << "Q4" << endl;
for (division = 0; division < 4; division++)
{
cout << "Div " << division + 1;
for (quarter = 0; quarter < 4; quarter++)
{
cout << " $" << DivisionSale[division].Sales(quarter);
}
cout << endl;
}
cout << "---------------------------------------- ";
cout << " Total corporate sales for the year: ";
cout << "$" << DivisionSale[0].getvalue() << endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.