So I am working on a program that asks the following:
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
The array would be in a separate shoppingcart.hpp and shoppingcart.cpp and I am not sure how to do this or what it should look like.
I have read over this
http://www.cplusplus.com/forum/general/52820/
and this
http://www.cplusplus.com/forum/beginner/49103/
however I am not much further along.
.cpp code
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
|
#include <iostream>
#include <string>
#include <cmath>
#include "Item.hpp"
using namespace std;
Item::Item()
{
setName("");
setPrice(0.0);
setQuantity(0);
}
Item::Item(string lable, double cost, int number)
{
setName(lable);
setPrice(cost);
setQuantity(number);
}
void Item::setName (string lable)
{
name = lable;
}
void Item::setPrice(double cost)
{
price = cost;
}
void Item::setQuantity(int number)
{
quantity = number;
}
string Item::getName()
{
return name;
}
double Item::getPrice()
{
return price;
}
int Item::getQuantity()
{
return quantity;
}
|
.hpp code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <string>
#ifndef ITEM_HPP
#define ITEM_HPP
class Item
{
private:
std::string name;
double price;
int quantity;
public:
Item();
Item(std::string, double, int);
void setName (std::string);
void setPrice(double);
void setQuantity(int);
std::string getName();
double getPrice();
int getQuantity();
};
#endif
|
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
|
#include "ShoppingCart.hpp"
#include <iostream>
ShoppingCart::ShoppingCart(Item,Item)
{
arrayEnd = 0;
for(int i = 0; i<100;i++)
{
array[i] = NULL;
}
}
/******************************
* Description: adds object to the array
* ***************************/
Item addItem(Item itemArg)
{
array[arrayEnd] = itemArg;
arrayEnd++;
}
/*****************************************
* Description: calculates the total price of Items * price
* ****************************************/
Item totalPrice(Item)
{
Item sum = 0; // keep the running total
for(int i = 0; i < arrayEnd; i++)
{
sum += array[i].getPrice * array[i].getQuantity;
}
return sum;
}
|
cart.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#ifndef ITEM_HPP
#include "Item.hpp"
#include <algorithm>
class ShoppingCart
{
private:
Item* array[100];
int arrayEnd;
int count;
public:
shoppingCart() : count(0)
{
std::fill(std::begin(array), std::end(array), nullptr);
}
ShoppingCart(Item, Item);
Item addItem(Item);
Item totalPrice(Item);
};
#endif
|
Last edited on
so I am not sure if that helped or if I am missing something. I looked over other project and the setup is slimier. but at the moment I am getting
1 2
|
ShoppingCart.cpp:26:4: error: ‘array’ was not declared in this scope
array[arrayEnd] = itemArg;
|
I will update the code in my first post above.
If they are declared in the constructor why is the rest of the program not seeing them?
1 2 3 4 5 6 7 8
|
ShoppingCart::ShoppingCart(Item,Item)
{
arrayEnd = 0;
for(int i = 0; i<100;i++)
{
array[i] = NULL;
}
}
|
Last edited on
addItem(...)
and totalPrice(...)
are free function not members of the class. Add the sope ShoppingCart::
before the function name.
arrayEnd
and count
are the same, so get rid of one of them.
ShoppingCart(Item,Item) // What is the use of this Item?
You should either use the paramter passed or remove them.