Classes and Objects?

How would I create a .cpp file that calls all of the functions.
Here is my header file:

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
// This is the inventory.h file.
// It contains the Inventory class declaration.

#ifndef INVENTORY_H
#define INVENTORY_H

class Inventory
{
private:
	int itemNumber;
	int quantity;
	double cost;
	double totalCost;
public:
	// Default constructor
	Inventory()
		{ itemNumber = quantity = cost = totalCost = 0; }
	
	// Overloaded constructor
	Inventory(int, int, double);	// Defined in Inventory.cpp

	// Mutators (i.e., "set" functions) defined in Inventory.cpp
	void setItemNumber(int);
	void setQuantity(int);
	void setCost(double);

	// setTotalCost calculates the total cost
	// and stores the result in the totalCost member
	void setTotalCost()
		{ totalCost = cost * quantity; }

	// Accessors (i.e., "get" functions)
	int getItemNumber()
		{ return itemNumber; }
	int getQuantity()
		{ return quantity; }
	double getCost()
		{ return cost; }
	double getTotalCost()
		{ return totalCost; }

	// Input validation functions
	bool validInt(int);
	bool validFloat(double);
};

#endif 

And here is my .cpp file.


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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// This is the inventory.cpp file.
// It contains the Inventory class function definitions. 

#include <iostream>
#include "Inventory.h"
using namespace std;

//************************************************************
// Overloaded constructor
// Accepts arguments to be stored in each member variable.
//************************************************************
Inventory::Inventory(int in, int q, double c)
{ 
	setItemNumber(in);
	setQuantity(q);
	setCost(c);
	setTotalCost();
}

//************************************************************
// setItemNumber accepts an argument to be stored in item number.
//************************************************************
void Inventory::setItemNumber(int in)
{
	while (!validInt(in))
	{
		cout << "Item Number must be positive. Please re-enter: ";
		cin  >> in;
	}
	itemNumber = in; 
}

//************************************************************
// setQuantity accepts an argument to be stored in quantity.
//************************************************************
void Inventory::setQuantity(int q)
{
	while (!validInt(q))
	{
		cout << "Quantity must be positive. Please re-enter: ";
		cin  >> q;
	}
	quantity = q; 
}

//************************************************************
// setCost accepts an argument to be stored in cost.
//************************************************************
void Inventory::setCost(double c)
{
	while (!validInt(c))
	{
		cout << "Cost must be positive. Please re-enter: ";
		cin  >> c;
	}
	cost = c; 
}

//************************************************************
// The validInt member tests its integer argument to see 
// if it is negative. If the argument is negative, the function 
// returns false. Otherwise, the function returns true.
//************************************************************
bool Inventory::validInt(int value)
{
	if (value < 0)    // the value is negative so it is NOT valid
		return false;
	else              // the integer value is valid
		return true;  
}

//************************************************************
// The validFloat member tests its floating-point argument to see
// if it is negative. If the argument is negative, the function 
// returns false. Otherwise, the function returns true.
//************************************************************
bool Inventory::validFloat(double value)
{
	if (value < 0)    // the value is negative so it is NOT valid
		return false;
	else              // the floating-point value is valid
		return true;
}

Now I need to create another .cpp file to prove that the functions work, and I have to create one or more inventory objects to call all of the functions. I'm not quite sure how to go about this...All I have so far are the #include statements and i'm stuck. Can anyone help?
Last edited on
1. Pls use the source code button
2. What do you mean with
to prove that the functions work

Do you need a test main for that?
Last edited on
1. Sorry.
2. I need to call all of the functions.

and yes
Last edited on
I don't see the problem. Why can't you just

0. #include <Inventory.h>
1. Instantiate an inventory object.
2. Call in sequence the set and get methods, cout-ing the results.

Or do you want to automatically generate the code? I don't think so, but in case you could use Boost.Preprocessor.
I am just having problems writing the code for the set and get methods.
If you set a value you should get the same value back from the get function (if it's a validInt).
You can make some tests in a loop and print an error if you get the wrong value.

I know it can feel strange to write unit tests for setters and getters because the code needed is more complicated than the code inside the functions and it's a greater chance you make a mistake in the test than in the real code, but I guess that's just how it is.
Last edited on
Yeah...thanks!
I will try to do that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[code]int Inventory::getItemNumber()
{
	return itemNumber ; 
}

//************************************************************

//************************************************************
int Inventory::getQuantity()
{
	return quantity ; 
}

//************************************************************

//************************************************************
int Inventory::getCost()
{
	return cos
t;
}
[/code]
The accessor methods should be constant i.e.

1
2
3
	// Accessors (i.e., "get" functions)
	int getItemNumber() const
		{ return itemNumber; }
I cannot get that code to work...because it's not letting me use getItemNumber outside of it's class.
Let me do your homework.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int argc,char* argv[])
{
 Inventory inventory(1,2,3.0);

 std::cout << inventory.getItemNumber() << std::endl;
 std::cout << inventory.getQuantity() << std::endl;
 std::cout << inventory.getCost() << std::endl;

 inventory.setItemNumber();
 std::cout << inventory.getItemNumber() << std::endl;

 return 0;
}


And so on.
Last edited on
Topic archived. No new replies allowed.