Task 4: Classes. Create a new project. -Modify the code from Task 3 to use a cla
ID: 3742771 • Letter: T
Question
Task 4: Classes.
Create a new project.
-Modify the code from Task 3 to use a class instead of a structure. Don’t initialize the values.
-Place the Class declaration into a separate .h file.
-Place the Class definition into a separate .cpp file.
-Write the code to read the values for the items of the class from console.
ID: 914
Units: 842
Price: 12.95
-Ensure you have tested this with minimum three inputs from the console
-----------------------------------------------------------------------------------------------------------------------------
/ This program produces a sales report for DLC, Inc.
#include
#include
using namespace std;
struct product{
int id;
int units;
double price;
double sales;
};
// Function prototypes
void calcSales(product[], int);
void showOrder(product[], int);
void dualSort(product[], int);
void showTotals(product[], int);
// NUM_PRODS is the number of products produced.
const int NUM_PRODS = 9;
int main()
{
product prodArray[NUM_PRODS] = {{914, 842, 12.95, 0},{915, 416, 14.95, 0},{916, 127, 18.95, 0}, {917, 514, 16.95, 0},{918, 437, 21.95, 0}, {919, 269, 31.95, 0}, {920, 97, 14.95, 0}, {921, 492, 14.95, 0}, {922, 212, 16.95, 0}};
// Calculate each product's sales.
calcSales(prodArray, NUM_PRODS);
// Sort the elements in the sales array in descending
// order and shuffle the ID numbers in the id array to
// keep them in parallel.
dualSort(prodArray, NUM_PRODS);
// Set the numeric output formatting.
cout << setprecision(2) << fixed << showpoint;
// Display the products and sales amounts.
showOrder(prodArray, NUM_PRODS);
// Display total units sold and total sales.
showTotals(prodArray, NUM_PRODS);
return 0;
}
//****************************************************************
// Definition of calcSales. Accepts units, prices, and sales *
// arrays as arguments. The size of these arrays is passed *
// into the num parameter. This function calculates each *
// product's sales by multiplying its units sold by each unit's *
// price. The result is stored in the sales array. *
//****************************************************************
void calcSales(product prodArr[], int num)
{
for (int index = 0; index < num; index++)
prodArr[index].sales = prodArr[index].units * prodArr[index].price;
}
//***************************************************************
// Definition of function dualSort. Accepts id and sales arrays *
// as arguments. The size of these arrays is passed into size. *
// This function performs a descending order selection sort on *
// the sales array. The elements of the id array are exchanged *
// identically as those of the sales array. size is the number *
// of elements in each array. *
//***************************************************************
void dualSort(product prodArr[], int size)
{
int startScan, maxIndex, tempid;
double maxValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
maxIndex = startScan;
maxValue = prodArr[startScan].sales;
tempid = prodArr[startScan].id;
for(int index = startScan + 1; index < size; index++)
{
if (prodArr[index].sales > maxValue)
{
maxValue = prodArr[index].sales;
tempid = prodArr[index].id;
maxIndex = index;
}
}
prodArr[maxIndex].sales = prodArr[startScan].sales;
prodArr[maxIndex].id = prodArr[startScan].id;
prodArr[startScan].sales = maxValue;
prodArr[startScan].id = tempid;
}
}
//****************************************************************
// Definition of showOrder function. Accepts sales and id arrays *
// as arguments. The size of these arrays is passed into num. *
// The function first displays a heading, then the sorted list *
// of product numbers and sales. *
//****************************************************************
void showOrder(product prodArr[], int num)
{
cout << "Product Number Sales ";
cout << "---------------------------------- ";
for (int index = 0; index < num; index++)
{
cout << prodArr[index].id << " $";
cout << setw(8) << prodArr[index].sales << endl;
}
cout << endl;
}
//*****************************************************************
// Definition of showTotals function. Accepts sales and id arrays *
// as arguments. The size of these arrays is passed into num. *
// The function first calculates the total units (of all *
// products) sold and the total sales. It then displays these *
// amounts. *
//*****************************************************************
void showTotals(product prodArr[], int num)
{
int totalUnits = 0;
double totalSales = 0.0;
for (int index = 0; index < num; index++)
{
totalUnits += prodArr[index].units;
totalSales += prodArr[index].sales;
}
cout << "Total units Sold: " << totalUnits << endl;
cout << "Total sales: $" << totalSales << endl;
}
Explanation / Answer
If you have any doubts, please give me comment...
SalesDemo.cpp
#include<iostream>
#include<iomanip>
using namespace std;
#include "product.h"
const int NUM_PRODS = 9;
void calcSales(product[], int);
void showOrder(product[], int);
void dualSort(product[], int);
void showTotals(product[], int);
int main()
{
product prodArray[NUM_PRODS];
int id, units;
double price;
for(int i=0; i<NUM_PRODS; i++){
cout<<" Enter product"<<(i+1)<<" Details: "<<endl;
cout<<"Enter Product ID: ";
cin>>id;
prodArray[i].setID(id);
cout<<"Enter Units: ";
cin>>units;
prodArray[i].setUnits(units);
cout<<"Enter Price: ";
cin>>price;
prodArray[i].setPrice(price);
prodArray[i].calcSales();
}
// Sort the elements in the sales array in descending
// order and shuffle the ID numbers in the id array to
// keep them in parallel.
dualSort(prodArray, NUM_PRODS);
// Set the numeric output formatting.
cout << setprecision(2) << fixed << showpoint;
// Display the products and sales amounts.
showOrder(prodArray, NUM_PRODS);
// Display total units sold and total sales.
showTotals(prodArray, NUM_PRODS);
return 0;
}
product.h
#ifndef PRODUCT_H
#define PRODUCT_H
class product
{
private:
int id;
int units;
double price;
double sales;
public:
product();
product(int id, int units, double price);
void setID(int id);
void setUnits(int units);
void setPrice(double price);
void setSales(double sales);
int getID();
int getUnits();
double getPrice();
double getSales();
void calcSales();
};
#endif
#include "product.cpp"
product.cpp
product::product(){
id = 0;
units = 0;
price = 0.0;
sales = 0.0;
}
product::product(int id, int units, double price){
this->id = id;
this->units = units;
this->price = price;
calcSales();
}
int product::getID(){
return id;
}
int product::getUnits(){
return units;
}
double product::getPrice(){
return price;
}
double product::getSales(){
return sales;
}
void product::calcSales()
{
sales = units * price;
}
void product::setID(int id){
this->id = id;
}
void product::setUnits(int units){
this->units = units;
}
void product::setPrice(double price){
this->price = price;
}
void product::setSales(double sales){
this->sales = sales;
}
//***************************************************************
// Definition of function dualSort. Accepts id and sales arrays *
// as arguments. The size of these arrays is passed into size. *
// This function performs a descending order selection sort on *
// the sales array. The elements of the id array are exchanged *
// identically as those of the sales array. size is the number *
// of elements in each array. *
//***************************************************************
void dualSort(product prodArr[], int size)
{
int startScan, maxIndex, tempid;
double maxValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
maxIndex = startScan;
maxValue = prodArr[startScan].getSales();
tempid = prodArr[startScan].getID();
for (int index = startScan + 1; index < size; index++)
{
if (prodArr[index].getSales() > maxValue)
{
maxValue = prodArr[index].getSales();
tempid = prodArr[index].getID();
maxIndex = index;
}
}
prodArr[maxIndex].setSales(prodArr[startScan].getSales());
prodArr[maxIndex].setID(prodArr[startScan].getID());
prodArr[startScan].setSales(maxValue);
prodArr[startScan].setID(tempid);
}
}
//****************************************************************
// Definition of showOrder function. Accepts sales and id arrays *
// as arguments. The size of these arrays is passed into num. *
// The function first displays a heading, then the sorted list *
// of product numbers and sales. *
//****************************************************************
void showOrder(product prodArr[], int num)
{
cout << "Product Number Sales ";
cout << "---------------------------------- ";
for (int index = 0; index < num; index++)
{
cout << prodArr[index].getID() << " $";
cout << setw(8) << prodArr[index].getSales() << endl;
}
cout << endl;
}
//*****************************************************************
// Definition of showTotals function. Accepts sales and id arrays *
// as arguments. The size of these arrays is passed into num. *
// The function first calculates the total units (of all *
// products) sold and the total sales. It then displays these *
// amounts. *
//*****************************************************************
void showTotals(product prodArr[], int num)
{
int totalUnits = 0;
double totalSales = 0.0;
for (int index = 0; index < num; index++)
{
totalUnits += prodArr[index].getUnits();
totalSales += prodArr[index].getSales();
}
cout << "Total units Sold: " << totalUnits << endl;
cout << "Total sales: $" << totalSales << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.