(C++) Define a class called Item that consists of a string called name, a double
ID: 3668896 • Letter: #
Question
(C++)
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 calledaddItem 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 andShoppingCart.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
main.cpp
#include "ShoppingCart.hpp"
#include <iostream>
using namespace std;
int main()
{
Item a("affidavit", 179.99, 12);
Item b("Bildungsroman", 0.7, 20);
Item c("capybara", 4.5, 6);
Item d("dirigible", 0.05, 16);
ShoppingCart myCart;
myCart.addItem(&a);
myCart.addItem(&b);
myCart.addItem(&c);
myCart.addItem(&d);
double total = myCart.totalPrice();
cout << total << endl;
}
ShoppingCart.cpp
#include "ShoppingCart.hpp"
/********************************************************
** ShoppingCart::ShoppingCart()
**
** Default constructor for ShoppingCart class. Sets each
** element of the items array to NULL and sets arrayEnd
** to 0.
********************************************************/
ShoppingCart::ShoppingCart()
{
//Set the elements of item_pointer to NULL
*item_pointer = new Item[100]();
arrayEnd = 0;
}
/********************************************************
** void ShoppingCart::addItem(Item *item)
**
** Adds an Item pointer to the item_pointer array and
** inrements arrayEnd.
********************************************************/
void ShoppingCart::addItem(Item *item)
{
item_pointer[arrayEnd] = item;
arrayEnd++;
}
/********************************************************
** double ShoppingCart::totalPrice()
**
** Sums up the total price off all items in the Shopping
** cart.
*********************************************************/
double ShoppingCart::totalPrice()
{
double total;
for (int i = 0 ; i < arrayEnd; i++)
{
total += (*item_pointer[i]).getPrice()*(*item_pointer[i]).getQuantity();
}
return total;
}
ShoppingCart.hpp
#include "Item.hpp"
#ifndef SHOPPINGCART_HPP
#define SHOPPINGCART_HPP
class ShoppingCart
{
private:
Item *item_pointer[100];
int arrayEnd;
public:
ShoppingCart();
void addItem(Item*);
double totalPrice();
};
#endif
Item.cpp
#include "Item.hpp"
/******************************************************
** Item::Item()
**
** The default constructor for the Item class.
** Created an Item with name "", price 0.0, and
** quantity of 0.
******************************************************/
Item::Item()
{
setName("");
setPrice(0.0);
setQuantity(0);
}
/******************************************************
** Item::Item(string name, double price, int quantity)
**
** Constructor that takes 3 parameters (name, price,
** and quantity) and passes them to the respective class
** set methods
******************************************************/
Item::Item(string name, double price, int quantity)
{
setName(name);
setPrice(price);
setQuantity(quantity);
}
/******************************************************
** string IteM::getName()
**
** Accessor for the Item's name. Returns a string.
******************************************************/
string Item::getName()
{
return name;
}
/******************************************************
** double IteM::getPrice()
**
** Accessor for the Item's price. Returns a double.
******************************************************/
double Item::getPrice()
{
return price;
}
/******************************************************
** int IteM::getQuantity()
**
** Accessor for the quantity of the item in stock.
** Returns an int.
******************************************************/
int Item::getQuantity()
{
return quantity;
}
/******************************************************
** void IteM::setName(string nombre)
**
** Mutator for the Item's name.
******************************************************/
void Item::setName(string nombre)
{
name = nombre;
}
/******************************************************
** void IteM::setPrice(double precio)
**
** Mutator for the Item's price.
******************************************************/
void Item::setPrice(double precio)
{
price = precio;
}
/******************************************************
** void IteM::setQuantity(int cuantos)
**
** Mutator for the Item's quantity.
******************************************************/
void Item::setQuantity(int cuantos)
{
quantity = cuantos;
}
Item.hpp
#include <string>
#ifndef ITEM_HPP
#define ITEM_HPP
using namespace std;
//Item class declaration
class Item
{
private:
string name;
double price;
int quantity;
public:
Item();
Item(string, double, int);
string getName();
double getPrice();
int getQuantity();
void setName(string);
void setPrice(double);
void setQuantity(int);
};
#endif
output
2201.68
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.