I am having trouble understanding why some of my programs do not work. From what I understand, .h files are for declarations, and the .cpp files are for implementation. When I #include the .h file in the main, it should take the implementation from the .cpp file with it, shouldn't it?
Here is an example of something I am working on, that does not work unless I #include ShoppingCart.cpp in the main..
If someone could clear this up for me for the future it would be greatly appreciated
#include "ShoppingCart.h"
// Creates an empty shopping cart
ShoppingCart::ShoppingCart() {
shoppingCart = vector<Item>();
}
// Adds an item to shopping cart
void ShoppingCart::addItem(const Item& item) {
shoppingCart.push_back(item);
}
// Returns the total price of all items in cart
double ShoppingCart::grandTotal() {
double total = 0;
for (int i = 0; i < shoppingCart.size(); i++) {
total += shoppingCart[i].getPrice();
}
return total;
}