Define a class called Item that consists of a string called name, a double calle
ID: 666695 • Letter: D
Question
Define a class called Item that consists of a string called name, a double called price, and an int called quantity. The price represents the price per unit, so if you have an Item with the name "apple", the price "0.80" and the quantity 4, then it means that this Item represents 4 apples, with each apple costing 80 cents. It should have get and set methods for each field (following the normal naming convention - getName, setName, getPrice, setPrice, getQuantity and setQuantity). It should have a constructor that takes three parameters (in the order given above) and passes them to the set methods. It should have a default constructor that sets name to "", price to 0.0 and quantity to 0.
Define a ShoppingCart class which contains as a data member an array of pointer-to-Item (Item*) that can contain up to 100 Item pointers. It should also have an int data member called arrayEnd that keeps track of the index of the next empty spot in the array. You should have a default constructor that initializes each element of the array to NULL and initializes arrayEnd to zero. It should have a function called addItem that takes as a parameter a pointer to an Item and adds it to the array (and updates arrayEnd). It should have a function called totalPrice that returns the total cost of all Items in the ShoppingCart (for which you must take into account the quantity of each Item). Your classes may get used as follows:
Potential parenthesis pitfalls: Remember not to use empty parentheses when declaring an object. If you are invoking a constructor that takes parameters, then you would use parentheses with parameters inside, but if you are invoking the default constructor, you don't use parentheses at all. Also remember that all function calls must have parentheses - if a function doesn't take any parameters, then you must put an empty pair of parentheses.
The files must be called: Item.hpp, Item.cpp, ShoppingCart.hpp and ShoppingCart.cpp.
Item.cpp and ShoppingCart.hpp should both #include Item.hpp. ShoppingCart.cpp should #include ShoppingCart.hpp. The main method you write for testing will also need to include ShoppingCart.hpp. If you named the file with your main method "cartMain.cpp", then you can compile your program with "g++ Item.cpp ShoppingCart.cpp cartMain.cpp -o cart".
Explanation / Answer
results.txt
Total score: 50.0
testcode.py
ShoppingCart.hpp
#include "Item.hpp"
#ifndef SHOPPINGCART_HPP
#define SHOPPINGCART_HPP
class ShoppingCart
{
private:
static const int ARRSIZE = 100; // Array size restrictor
Item *itemArray[ARRSIZE]; // Item Array
int arrayEnd; // the arrayEnd
public:
void addItem(Item*); // Adds the pointed-to-item to the array
double totalPrice(); // Calculates the total price
ShoppingCart(); // default constructor
};
#endif
shoppingcart.cpp
#include "ShoppingCart.hpp"
//using namespace std;
ShoppingCart::ShoppingCart() // Default Constructor
{
arrayEnd = 0;
for (int i = 0; i < ARRSIZE; i++)
{
itemArray[i] = NULL;
}
}
void ShoppingCart::addItem(Item *newItem)
{
itemArray[arrayEnd] = newItem;
arrayEnd++;
}
double ShoppingCart::totalPrice()
{
double totalPrice;
for (int i = 0; i < arrayEnd; i++)
{
totalPrice += itemArray[i]->getQuantity() * itemArray[i]->getPrice();
}
return totalPrice;
}
item.hpp
#include<string.h>
//using namespace std;
#ifndef ITEM_HPP
#define ITEM_HPP
class Item
{
private: // Private Data Variables.
string name;
double price;
int quantity;
public:
// Setter Functions
void setName(string);
void setPrice(double);
void setQuantity(int);
// Getter Functions
string getName();
double getPrice();
int getQuantity();
Item(); // Default Constructor
Item(string, double, int); // Constructor
};
#endif
item.cpp
#include "Item.hpp"
//using namespace std;
// Default Constructor
Item::Item()
{
setName("");
setPrice(0.0);
setQuantity(0);
}
// Constructor
Item::Item(string s, double x, int y)
{
setName(s);
setPrice(x);
setQuantity(y);
}
// Sets the name of the item from input.
void Item::setName(string n)
{
name = n;
}
// Sets the price of the item from input.
void Item::setPrice(double prc)
{
price = prc;
}
// Sets the quantity of the item from input.
void Item::setQuantity(int quant)
{
quantity = quant;
}
// Returns the name from the input
string Item::getName()
{
return name;
}
// Returns the price from the input
double Item::getPrice()
{
return price;
}
// Returns the quantity from the input
int Item::getQuantity()
{
return quantity;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.