Description
The purpose of this challenge is to use basic classes with constructors and destructors. Additionally, this challenge implements dynamic memory allocation. This challenge mimics an event ticket kiosk.
Requirements
- Create a class called Kiosk
- In class Kiosk, add the following private member data
- double adult_cost; // cost of adult ticket
- double child_cost; // cost of child ticket
- double senior_cost; // cost of senior ticket
- string * names; // dynamic array of strings
- double total;
- int attendees;
- int index;
- Create a constructor that will pass in three numeric values to set the ticket values for adult_cost, child_cost, and senior_cost. Also, initialize index to 0 (zero) and set names to NULL.
- Create a public function double get_total(). Do NOT cout in this function. This is a simple getter function that returns the value of total.
- Create a public function int set_counts(int adults, int children, int seniors). Set attendees to the sum of adults, seniors and adults. This function will initialize the names dynamic array with a size equivalent to attendees. In the vein of defensive programming, first check to see if names is NULL before initializing it to the dynamic array. This function will also calculate the total cost of the tickets. The return value is the total number of attendees.
- Make sure to dispose of the dynamic variable names in a destructor using this call: delete [] names;
- Create a public function void add_name(string name). This function will add a name into the names array. Use the attendees variable to ensure that adding names does not exceed the size of the array we created. Hint: use the index variable to track the current or next position in the array.
- Create a public function string get_names(). This function returns all the names in the names array as one concatenated string separated by spaces.
- In main(), declare a dynamic Kiosk variable. This will mean that all references to public functions made by Kiosk objects must use the arrow operator.
Sample main()
int main() { // prices for adults, children, seniors Kiosk * machine = new Kiosk(15, 10, 8); int act, cct, sct, count = 0; string name; cout << "How many adults? "; cin >> act; cout << "How many children? "; cin >> cct; cout << "How many seniors? "; cin >> sct; count = machine->set_counts(act, cct, sct); for (int i = 0; i < count; i++) { cout << "Name of attendee: "; cin >> name; machine->add_name(name); } cout << "Total cost: " << machine->get_total() << endl; cout << "Enjoy the show, " << machine->get_names() << "!\n"; // dispose of the machine dynamic variable delete machine; return 0; }
Sample Interaction
How many adults? 1 How many children? 0 How many seniors? 1 Name of attendee: Feo Name of attendee: Pepe Total cost: 23.00 Enjoy the show, Feo Pepe!
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL0039
Print Requirements