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");
}
```
page revision: 0, last edited: 04 Oct 2009 11:45