I have to create an Inventory class with the member variables
int itemNumber;
int quantity;
double cost;
that can set and get values for the above. I need to do input validation and I am wondering where I should do that. In the client (main) program, or as part of the object itself?
//File: Inventory.h - class Inventory declaration file
#ifndef INVENTORY_H
#define INVENTORY_H
class Inventory {
private:
int itemNumber;
int quantity;
double cost;
public:
//constructors
Inventory() {
itemNumber = quantity = cost = 0;
}
Inventory(int n, int q, double c) {
itemNumber = n; quantity = q; cost = c;
}
//implemented in Inventory.cpp
void setItemNumber(int);
void setQuantity(int);
void setCost(double);
void setTotalCost(double);
//"gets" implemented as one-liners
int getItemNumber() { return itemNumber; }
int getQuantity() { return quantity; }
double getCost() { return cost; }
double getTotalCost() { return quantity * cost; }
};
#endif
That is my class declaration file. I am wondering if I should write the input validation with my set functions or as part of main, and perhaps how I would do about doing it.