Start learning free

LESSON 407 OF 409 · PROJECTS: OBJECT-ORIENTED

Shopping Cart in C++ in C++

Practice

A shop keeps its orders in a cart. Build two classes that work together: a LineItem is one row of the cart (a product, its price and how many were bought), and a Cart holds many rows and adds them up. The cart has line items (composition), it is not one.

LineItem

  • Public fields string name, double price, int quantity.
  • LineItem(string name, double price, int quantity) stores the three values using this->.
  • double subtotal() returns price * quantity.

Cart

  • It keeps its items in a private vector<LineItem> items.
  • void add(LineItem item) pushes the item onto the vector.
  • double total() returns the sum of the subtotals of all items: start double sum = 0, loop with for (auto& item : items), return sum.

Rules:

  • The total is worked out every time it is asked for, from the items in the cart at that moment. Adding an item later changes the next total.
  • An empty cart has a total of 0.
  • An item with price 5 and quantity 3 has a subtotal of 15.
  • Prices in the driver are whole numbers.

Remember the public: and private: labels and the ; after each class. cout prints the double 75 as 75. The driver lives in Solution::mainFunction().

The driver (already in the starter code, don't change it) creates an empty cart, checks one subtotal, then fills a cart and prints the total twice:

class Solution {
public:
    void mainFunction() {
        Cart empty;
        cout << "Empty: " << empty.total() << endl;
        LineItem apple("Apple", 5, 3);
        cout << "Subtotal: " << apple.subtotal() << endl;
        Cart cart;
        cart.add(apple);
        cart.add(LineItem("Bread", 20, 3));
        cout << "Total: " << cart.total() << endl;
        cart.add(LineItem("Milk", 10, 2));
        cout << "Total: " << cart.total() << endl;
    }
};

Expected output:

Empty: 0
Subtotal: 15
Total: 75
Total: 95

Write class LineItem { public: ... }; first, then class Cart.

for (auto& item : items) sum += item.subtotal();

class LineItem {
public:
    string name;
    double price;
    int quantity;

    LineItem(string name, double price, int quantity) {
        this->name = name;
        this->price = price;
        this->quantity = quantity;
    }

    double subtotal() {
        return price * quantity;
    }
};

class Cart {
private:
    vector<LineItem> items;
public:
    void add(LineItem item) {
        items.push_back(item);
    }

    double total() {
        double sum = 0;
        for (auto& item : items) sum += item.subtotal();
        return sum;
    }
};

class Solution {
public:
    void mainFunction() {
        Cart empty;
        cout << "Empty: " << empty.total() << endl;
        LineItem apple("Apple", 5, 3);
        cout << "Subtotal: " << apple.subtotal() << endl;
        Cart cart;
        cart.add(apple);
        cart.add(LineItem("Bread", 20, 3));
        cout << "Total: " << cart.total() << endl;
        cart.add(LineItem("Milk", 10, 2));
        cout << "Total: " << cart.total() << endl;
    }
};

STEP 1 OF 19
    Ctrl + Enter to check

    Shopping Cart in C++

    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free