Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task 4: Classes. Create a new project. -Modify the code from Task 3 to use a cla

ID: 3744205 • 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, include screenshots

Task 3:

// This program produces a sales report for DLC, Inc.

#include <iostream>
#include <iomanip>

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 products[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}};

// Calculates each product
calcSales(products, NUM_PRODS);
  
// Sorts the elements in descending order
dualSort(products, NUM_PRODS);

// Formats
cout << setprecision(2) << fixed << showpoint;
  
// Displays
showOrder(products, NUM_PRODS);

// Displays totals
showTotals(products, 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 prods[], int num)
{
for (int i = 0; i < num; i++)
prods[i].sales = prods[i].units * prods[i].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 prods[], int size)
{
int s, index, temp;
double maxValue;
for (s = 0; s < (size - 1); s++)
{
index = s;

maxValue = prods[s].sales;

temp = prods[s].id;

for(int i = s + 1; i < size; i++)
{
if (prods[i].sales > maxValue)
{
maxValue = prods[i].sales;
temp = prods[i].id;
index = i;

}
}

prods[index].sales = prods[s].sales;
prods[index].id = prods[s].id;
prods[s].sales = maxValue;
prods[s].id = temp;
  
}

}

//****************************************************************

// 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 prods[], int num)
{
cout << "Product Number Sales ";
cout << "---------------------------------- ";

for (int i = 0; i < num; i++)
{
cout << prods[i].id << " $";
cout << setw(8) << prods[i].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 prods[], int num)
{
int totalUnits = 0;
double totalSales = 0.0;
for (int i = 0; i < num; i++)
{
totalUnits += prods[i].units;
totalSales += prods[i].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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote