Input validation with objects

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?

edit: removed stale value totalCost
Last edited on
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
//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.
Last edited on
Topic archived. No new replies allowed.