C++ test review

This review exercise builds upon itself. In coding this program, you will exercise most of the facilities you learned in C++.  Please refer to the C++ summary as you code this.

1. Write a small application to track events. First, put it all in one program file.

Create a class Event which has 2 private properties: an event name and  event cost per person. The name should be a standard string and the cost is an integer. Create a constructor with the name and cost as parameters.  Create these public methods:

·         print: print the event (in: nothing; out: nothing)

·         totalcost:

o   Parameters:

§  reference integer variable representing the total cost  

§   integer variable representing the  quantity of guests (not a reference variable)

o   Output: integer representing success ( plus the reference variable to total cost is updated)

o   Process: Set the reference total cost to quantity * event cost. return -1 if the cost is 0.    

·         diff: find the difference of the cost of this event compared to another event. Return the difference between the cost per person of this event and the cost of the other event. (in: other event; out: integer representing difference)

·         getEventName: Just return the event name. (in: nothing; out event name which is a standard string)

Create a main method which does the following:

·         create a wedding event with the name wedding and cost 200

·         create a shower event with the name shower and cost 50

·         ask the user for the number of guests (use cout and cin)

·         create a variable of the total cost of both events combined   

·         Calculate the total cost of both events added together:

o   find the total cost of the wedding for 100 guests  by calling the wedding's total cost method.

o   add that wedding total cost to the variable tracking total cost of both events combined

o   then find the shower total cost by calling the shower's total cost method, for that same number of guests (100).

o   add that shower total cost to the variable tracking total cost of both events

·         Print the total cost of the both events combined.

·         Print the difference in cost per person for the two events. Find the difference by using the diff method of the wedding event.

Run and test this.

2. Add inheritance to this:

·         Create another class called FullBuildingEvent that contains everything an event contains plus a cost for building rental. It needs a constructor that will take in the name of the event, the cost per person and the building cost. It also needs to override the totalcost method to include the building cost.

·         Change your main method to create the wedding as a FullBuildingEvent, and set the building cost to 5000.

·         Run and test this and see that the total cost has risen by 5000.

3. Play with 2 different types of strings, and with character pointers.

·         Print "The event names are the same " if both the wedding and shower have the same name. Use getEventName to get the events and decide whether you can use the == operator to compare.

·         Create a variable to hold a clientname as a character array (char name[100]) and set it to your last name.

·         Print the length of the clientname and the length of the wedding event name. (You need to get the wedding event name by using getEventName.) Remember that you will need to include <cstring> to handle clientname. Either using namespace std or import <string> to handle the wedding event name. Use a cstring function for the clientname and a string function for the event name.

·         Create another cstring variable  eventWithClient, but this one as a character pointer: char * eventWithClient.  Allocate memory for 100 characters for that variable using malloc or new char[100]. release the memory with free or delete before your program ends.

·         set eventWithClient to hold the event name followed by "for" followed by the client name.  You will need to convert the standard string to a cstring in order to concatenate.  Find the string function to do that conversion. Then you can use the cstring functions strcat and strcpy to create the string.  Print the eventWithClient variable when you are done.

4. Add in arrays.

Change your event class:

·         Make it hold an array of guests.

o   Add an instance variable of an array of 100 standard strings 

o   Add an instance variable of the number of guests.

o   Change your constructor to add parameters :

§  number of guests

§  The constructor of the buildingEvent should change as well.

§  (As an extra, you can use malloc to allocate array space for only then number of guests in the event and then remember to free it in the destructor.)

·         Create another method that will allow you to add a guest to a certain place in the array.

o   (in: string guest name, index of the array; out: nothing)

o   process: just change the name of the guest at the given index. (This is not great management of a list, but okay for this exercise.)

·         Add a method to print all guests.

o   (in: nothing; out: nothing).

 

Change your main method:

·         Create your events after learning the number of guests.  

·         Add two guests to one event.

·         Print all the guests of that event.

·         Whenever you test,  enter the number 2 for guests as we have hard coded 2 guests into  the test run's list.

5. Play with polymorphism:

In your main method:

·         Add an array of pointers to events. (Use Event * eventArray[2] to create the event array)

·         Place the wedding and the shower objects into that array.

·         Loop through the array printing the name and cost of the wedding and the shower.

 

In your Event class:

Change the get total cost method to allow FullBuildingEvent to have its gettotalcost function used when on Event variables holding FullBuildingEvents. (This means make it so your array above will print out the correct cost for a full building event.)

       When your program runs:

·         Notice that the wedding, which is a full building event, calculates correctly.

·         How could you have just kept one event class and turned this into an if statement instead?

6. Method overloading and destructors:

In the event class:

·         Create a destructor method for the event class that will print goodbye to the wedding name. 

·         Create another constructor for the event class that takes in only a name and sets the cost to 300 and the number of guests to 0.

·         Create another diff method that takes in an integer instead of another event.

 

In your main method

·         Create one new event, giving it only a name.

·         Print the totalcost for 1 guest

·         Use the diff method to compare that new event’s cost to 1000. Print the result.

When you run the program:

·         Notice that when you run your program, the program ends by saying goodbye to all your events one at a time.

·         Notice that the 300 cost is used for only your new event. The other event continues to match to the name and cost constructor.

·         Notice that the diff method used the method comparing to an integer, though the existing diff methods use the other code that compares to an event.  

7. Separate interface from implementation and make classes reusable:

·         Place each class in its own .cpp with its own .h file

·         Add the required classname:: specifiers

·         Add the required include of .h

·         Add #ifndef condition to the event class.

·         Optionally create a make file

·         Compile and test.