Checkout Calculator

Solution for Lecture 1 Q3 Exercise 1

Checkout Calculator

#include <iostream>
using namespace std;
 
class CheckoutCalculator { 
      private:
              int itemCount;
              int *itemPrices;
              int total;
      public:
             CheckoutCalculator(int); //constructor
             ~CheckoutCalculator(); //destructor
             void enterPrices();
             void displayAllPrices();
             void getTotal();
};
 
CheckoutCalculator::CheckoutCalculator(int _itemCount){
    itemCount = _itemCount;
    total = 0;
    //dynamically allocate memory using the itemCount
    itemPrices = new int[itemCount];
}
 
CheckoutCalculator::~CheckoutCalculator(){
    delete [] itemPrices;
}
 
void CheckoutCalculator::displayAllPrices(){
     for (int i = 0; i < itemCount ; i++) {
         cout << "price for item " << (i+1) << ":";
         cout << itemPrices[i] << endl;
     }
}
 
void CheckoutCalculator::enterPrices(){
     for (int i = 0; i < itemCount ; i++) {
         cout << "enter price for item " << (i+1) << ":";
         cin >> itemPrices[i];
         //also calculate the total
         total = total + itemPrices[i];
     }
}
 
void CheckoutCalculator::getTotal(){
    cout << "Total:"<<total<<endl;
}
 
int main() {
    int count = 0;
    cout << "Enter the number of items to enter:";
    cin >> count;
 
    CheckoutCalculator cc(count);
    cc.enterPrices();
    cc.displayAllPrices();
    cc.getTotal();
 
    system("pause");
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License