inventory class question
I'm trying to calculate the total cost of inventory items using the inventory class, i'm getting a few error messages and i can't figure out why, the errors are undeclared identifiers in the imp file of my ... Here's what i have so far.
Inventory.h
#include <string>
#include <iostream>
using namespace std;
class Inventory
{
public:
void print() const;
void setItemNumber(int num);
void setQuantity(int qty);
void setCost(double cst);
void setTotalCost(double total);
int getItemNumber() const;
int getQuantity() const;
double getCost() const;
double getTotalCost () const;
Inventory(int num = 0, int qty = 0, double cst = 0, double total = 0);
Inventory(int num, int qty, double cst, double total);
private:
int itemNumber;
int quantity;
double cost;
double totalCost;
};
InventoryImp.cpp
#include <iostream>
#include "InventoryClass.h"
using namespace std;
void Inventory::print() const
{
if (itemNumber > 0 && quantity > 0 && cost > 0)
cout << itemNumber << quantity << cost
<< totalCost;
}
void Inventory::setCost(double cst)
{
cost = cst;
}
void Inventory::setItemNumber(int num)
{
itemNumber = num;
}
void Inventory::setQuantity(int qty)
{
quantity = qty;
}
void Inventory::setTotalCost(double total)
{
totalCost = total;
}
double Inventory::getCost() const
{
return cst;
}
int Inventory::getItemNumber() const
{
return num;
}
int Inventory::getQuantity() const
{
return qty;
}
double Inventory::getTotalCost() const
{
return qty * cst;
}
Inventory::Inventory(int num, int qty, double cst, double total)
{
cost = cst;
quantity = qty;
totalCost = total;
itemNumber = num;
}
Inventory::Inventory(int num, int qty, double cst, double total)
{
cost = cst;
quantity = qty;
totalCost = total;
itemNumber = num;
}
Main.cpp
#include <iostream>
#include "InventoryClass.h"
using namespace std;
int main()
{
Inventory Item1(101, 6, 3.00);
Inventory Item2(102, 1, 1.00);
Inventory Item3(103, 8, 7.00);
Inventory Item4(104, 4, 12.00);
Inventory Item5(105, 6, 5.00);
Inventory Item6(106, 3, 9.00);
Item1.print();
cout << endl;
Item2.print();
cout << endl;
Item3.print();
cout << endl;
Item4.开发者_Go百科print();
cout << endl;
Item5.print();
cout << endl;
Item6.print();
cout << endl;
return 0;
}
For a start, you only need one instance of this function...
Inventory(int num = 0, int qty = 0, double cst = 0, double total = 0);
The second one, with the same parameters and no default values is redundant, the compiler isn't going to be able to differentiate between them.
Aside from the main problem that forsvarir addressed, your other errors seem to revolve around undefined variables. You actually define them but misspell them in a couple of places.
Start training yourself to use some convention (it really doesn't matter what so long as you are consistent) that differentiates between member variables, global variables, parameters, etc. For instance naming member variables "_member" or "mMember" or whatever. It will save yourself and others grief down the road.
精彩评论