Inventory Class problems
Mar 28, 2008 at 10:38pm UTC
For this program, I have to create an Inventory class with the private member variables itemNumber, quantity, cost, and totalCost. I have to have public functions including a default constructor, a second constructor that accepts itemNumber, quantity, and cost as arguments; mutators; and accessors. Then I have to write a simple program that demonstrates the class.
Here are the files:
Problem 13.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include "Inventory.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int itemNumber,
quantity;
double cost,
totalCost;
cout << "Enter the Item Number: " ;
cin >> itemNumber;
while (itemNumber < 0)
{
cout << "Error - Enter a positive value for the Item Number: " ;
cin >> itemNumber;
}
cout << "Enter the Quantity of the item: " ;
cin >> quantity;
while (quantity < 0)
{
cout << "Error - Enter a positive value for the Quantity of the item: " ;
cin >> quantity;
}
cout << "Enter the Cost of the item: $" ;
cin >> cost;
while (cost < 0)
{
cout << "Error - Enter a positive value for the Cost of the item: $" ;
cin >> cost;
}
cout << "----------------------------------" << endl;
Inventory inventoryA(itemNumber, quantity, cost);
totalCost = inventoryA.getTotalCost();
itemNumber = inventoryA.getItemNumber();
cost = inventoryA.getCost();
quantity = inventoryA.getQuantity();
cout << "Item Number: " << itemNumber << endl;
cout << "Quantity: " << quantity << endl;
cout << "Cost: $" << cost << endl;
cout << "Total Cost: $" << fixed << setprecision(2) << totalCost << endl << endl;
return 0;
}
Inventory.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#ifndef INVENTORY_H
#define INVENTORY_H
class Inventory
{
private :
int itemNumber,
quantity;
double cost,
totalCost;
public :
Inventory()
{
itemNumber = 0;
quantity = 0;
cost = 0;
totalCost = 0;
}
Inventory(int itemNumber, int quantity, double cost)
{
itemNumber = getItemNumber();
quantity = getQuantity();
cost = getCost();
setTotalCost(quantity, cost);
}
void setItemNumber(int )
{
itemNumber = itemNumber;
}
void setQuantity(int )
{
quantity = quantity;
}
void setCost(double )
{
cost = cost;
}
void setTotalCost(int , double )
{
totalCost = quantity * cost;
}
int getItemNumber()
{
return itemNumber;
}
int getQuantity()
{
return quantity;
}
double getCost()
{
return cost;
}
double getTotalCost()
{
return totalCost;
}
};
#endif
The output I get always shows some really random numbers, such as 7883933849400000000000000.00 or something for the totalCost.
It's probably something really stupid on my part, but I can't see what I've done... any help would be really appreciated.
Topic archived. No new replies allowed.