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

Purpose: To gain experience in writing a simple class testing its functionality,

ID: 3726634 • Letter: P

Question

Purpose: To gain experience in writing a simple class testing its functionality, and separating class specification from implementation. Design an Inventory class that can hold information and calculate data for items in a retail store's inventory. The data members should be private along with the setTotalCost member function Data Member Description i temNumber guantity cost totalCost An int that holds the item's "item number" An int for holding the amount of items in stock A double for holding the wholesale per-unit cost of the item A double for holding the total inventory cost of the item (calculated as quantity times cost) don't let it get stale Description Member Function Constructors Default Constructor Constructor #2 Sets all member variables to 0 Accepts an item's number, cost and quantity as arguments. The function should copy these values to the appropriate member variable and call the set total cost function Mutators setItemNumber Accepts an int argument that is copied into the itemNumber member variable Accepts an int argument that is copied into the quantity member variable Accepts a double argument that is copied into the cost member variable Calculates the total inventory cost for the item (calculated as quantity times cost) Don't allow public access to this setQuantity setCost setTotalCost Accessors getItemNumber getQuantity getCost getTotalCos Returns to value stored in itemNumber Returns to value stored in quantity Returns to value stored in cost Returns to value stored in totalCost You can build this all in one file for testing purposes, but you will separate your build as described below. Be sure to use the preprocessor directives (tifndef & endif) as described in the chapter for specification. Make the class specification so the setTotalCost member function is private. You will need to call this at appropriate times when you build (construct) or modify (mutate) your object(s) so that we will never have stale or bad data in the totalcost private data member Page I of 2

Explanation / Answer

// This is Inventory.h file......

#ifndef INVENTORY_H_INCLUDED

#define INVENTORY_H_INCLUDED

#include <string>

#include <iostream>

using namespace std;

class Inventory

{

public:

Inventory();

Inventory(int itemNumber, double cost,double quantity);

string getName() const;

void setItemNumber( int itemNumber );

void setQuantity( int quantity );

void setCost(double cost);

int getItemNumber() const;

int getQuantity() const;

double getCost() const;

void print() const;

double getTotalCost() const;

void displayInvItem(Inventory obj);

private:

int itemNumber;

int quantity;

double cost;

double totalCost;

void setTotalCost(int quantity,double cost);

};

#endif // INVENTORY_H_INCLUDED  

// This file is Inventory.cpp WITH main() function.........

#include "Inventory.h"

#include <iostream>

#include <stdexcept>

#include<stdlib.h>

#include <windows.h>

using namespace std;

Inventory::Inventory()

{

this->itemNumber=0;

this->quantity=0;

this->cost=0;

this->totalCost=0;

}

Inventory::Inventory(int itemNumber, double cost,double quantity)

{

this->itemNumber=itemNumber;

this->cost=cost;

this->quantity=quantity;

setTotalCost(quantity,cost);

}

int Inventory::getItemNumber() const

{

return itemNumber;

}

int Inventory::getQuantity() const

{

return quantity;

}

double Inventory::getCost() const

{

return cost;

}

double Inventory::getTotalCost() const

{

return totalCost;

}

void Inventory::setItemNumber( int itemNumber )

{

this->itemNumber = itemNumber;

}

void Inventory::setQuantity( int quantity )

{

this->quantity = quantity;

}

void Inventory::setCost( double cost )

{

this->cost = cost;

}

void Inventory::setTotalCost(int quantity,double cost)

{

this->totalCost=quantity*cost;

}

void Inventory::displayInvItem(Inventory obj)

{

cout << "ItemNumber " << obj.itemNumber << endl;

cout << "quantity " << obj.quantity << endl;

cout << "Cost " << obj.cost << endl;

cout << "TotalCost " << obj.totalCost << endl;

}

//This is the InventoryDriver.cpp file...

#include <iostream>

#include<conio.h>

#include "Inventory.cpp"

#include<stdlib.h>

using namespace std;

int main()

{

Inventory inv;

inv.displayInvItem(inv);

Inventory inv2(777,10,12.50);

inv2.displayInvItem(inv2);

inv.setCost(20.50);

inv.setQuantity(10);

inv.getTotalCost();

printf("***** Modified Inventory***** ");

inv.displayInvItem(inv);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote