Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task 4: Classes. Create a new project. -Modify the code from Task 3 to use a class instead of a structure. Dont initialize the values.

Task 4: Classes.

Create a new project.

-Modify the code from Task 3 to use a class instead of a structure. Dont 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\tSales ";

cout << "---------------------------------- ";

for (int index = 0; index < num; index++)

{

cout << prodArr[index].id << "\t\t$";

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;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions

Question

What is DDL?

Answered: 1 week ago