OOP

Sunshine Furniture Sdn Bhd has approached you to
develop an application to track all the items in the
company.
(a) Define a class named Item to store the details of an
object in the world. Each Item should store the
following information:
- Item code
- Description
- Quantity on hand
- Cost price
- Selling price
- Status (e.g. available or unavailable)
- Discount (5%, 10%, 20%, … )
The Item class should include:
- A no-argument constructor
- A constructor with parameters
- A copy constructor
- Appropriate setter and getter methods
- The method equals that returns true if two
objects contains the same information.

Create an application program that declares an array of
100 components of type Item. Your program should
provide the following operations which the user may
invoke from a menu:
- Add a new item into the array (duplicate entries
are not allowed).
- Search for an item.
- Amend the details of an item.
- Update the item quantity (reorder quantity and
return spoilt item).
- Stop selling an item (status set to unavailable).
- List details of all items, each on a different line
with line numbering.
(b) Write a driver program that tests the various
operations of the class.

Question:
1) "The method equals that returns true if two objects contains the same information." Is the function must be designed in Item class? If so, how to do it?
2)Is the operations which the user may invoke from a menu and driver program that tests the various operations of the class are the same things?
3) "Update the item quantity (reorder quantity and
return spoilt item)". What actually reorder quantity and
return spoilt item do?
Last edited on
"The method equals that returns true if two objects contains the same information." Is the function must be designed in Item class? If so, how to do it?


I think this question is just asking for you to build a member function that returns true if the item is equal to another item. I'm not sure if (or how) you have designed your Item class yet, but since the question calls for a "method," it's asking for a member function (not a friend function) that overloads the == operator. So the implementation would be something simple like:

1
2
3
4
5
6
7
8
// member function approach
bool operator==(Item& aItem)
{
     if (*this == aItem)
          return true;
     else
          return false;
}


Of course, if your Item class is a template class, then you would need to change the interface and implementation to reflect that.

Is the operations which the user may invoke from a menu and driver program that tests the various operations of the class are the same things?


Hmm. That is a bit odd that you are asked to develop an application program and a driver program. Not sure what the distinction would be in this context. I would think you could certainly create a program that could meet the requirements for "Sunshine Furniture" that uses all of the interface for the Item class, which should be sufficient.


Update the item quantity (reorder quantity and return spoilt item)". What actually reorder quantity and return spoilt item do?


Again a bit opaque for me but the way I interpret this is that you need to build in member functions that represent the acquisition of additional items and the removal of items. For acquiring some quantity of items, since the word "reorder" is used instead of "re-order" it implies "acquiring more of something" instead of some type of sorting. The idea of "returning a spoilt item" is even more confusing to me, since returning has special meaning in a programming since. But I actually think the statement is intended to suggest that some items are returned because they are bad. In this case it would simply mean reducing the quantity of items by a given number. The interface would be something like this for both methods:

1
2
void reorderMoreItems(int numItems);   // could return an int also
void returnSpoiltItems(int numItems);    // could return an int also 


Each would essentially modify the presumably private data member you use to represent the "quantity on hand." The former would increase the quantity; the latter would reduce the quantity.
Last edited on
How to apply member function approach? I tried but failed.
Error:
1) Error 3 error C2355: 'this' : can only be referenced inside non-static member functions
2) Error 1 error C2805: binary 'operator ==' has too few parameters


1
2
3
4
5
6
7
8
// member function approach
bool operator==(Item& aItem)
{
     if (*this == aItem)
          return true;
     else
          return false;
}



My Item 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
48
49
50
51
#ifndef ITEM_H
#define ITEM_H
#include <string>
using namespace std;

class Item
{
public:
	Item();
	Item(string ItemCode, string Description, int Quantity, double CostPrice, double SellPrice, string Status, double Discount);
	~Item(); 
	
	// Setters
	void setItemCode(string ItemCode);
	void setDescription(string Description);
	void setQuantity(int Quantity);
	void setCostPrice(double CostPrice);
	void setSellPrice(double SellPrice);
	void setStatus(string Status);
	void setDiscount(double Discount);
	
	// Getters
	string getItemCode();
	string getDescription();
	int	   getQuantity();
	double getCostPrice();
	double getSellPrice();
	string getStatus();
	double getDiscount();
	
	// Methods
	//bool ifBothSame(Item obj1, Item obj2);
	void addItem();
	void amendDetails();
	void stopSelling();
	void listDetails();
	void updateQuantity();
	void stopSellingMessage();
	void amendDetailsMessage();
	void listDetailsMessage();

private:
	int quantity;
	double discount;
	double sellPrice;
	double costPrice;
	string itemCode;
	string description;
	string status;
};
#endif 


Last edited on
I do like that and then stack overflow comes out.

Item.h

bool operator==(Item& aItem);

Item.cpp
1
2
3
4
5
6
7
bool Item::operator==(Item& aItem)
{
	if (*this == aItem)
		return true;
	else
		return false;
}


Main.cpp
1
2
3
4
void compareItem()
{
	cout << item[0].operator==(item[1])<< endl;
}
Last edited on
You had a stack overflow because you had an infinite loop.
1
2
3
4
5
6
7
bool Item::operator==(Item& aItem)
{
	if (*this == aItem) //Think about what function this would call
		return true;
	else
		return false;
}


Within Item::operator==(Item&), you test if one aItem == another aItem. But wait, in order to do that test, the function must call Item::operator==(Item&). You basically made a recursive call without a way to break out of the cycle.

One way to write the equality comparison for Item is:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool Item::operator==(const Item& aItem)
      //note the const; prevents accidental modification
{
   if(
      quantity == aItem.quantity &&
      discount == aItem.quantity &&
   // ...
      status == aItem.status
   )
      return true;
   else
      return false;
}


Also, for operators, you do not need to call their name like aItem.operator==(another). You should be able to write simply aItem == another.

Also, for operators, you do not need to call their name like aItem.operator==(another). You should be able to write simply aItem == another.


I can't make it when I chg aItem.operator==(another) to aItem == another inside int main(). Am I miss or misunderstand something?
And where can I find documentation about bool operator==(Item& aItem)?
Topic archived. No new replies allowed.