Exercises to practice C++ skills

  1. Create a class to represent lengths measured in yards, feet and inches. The class should allow the input and output of distances and addition of two distances. Write a program which calculates the average of up to 100 distances.

  2. Create a class called time that has separate int data members for hours, minutes and seconds. Provide two constructors, one to initialise all data to 0 another to allow a user specified time. Provide member functions to display a time and add two times together. Test your class.

  3. Cars passing over the Severn Bridge are expected to pay a 3.50 toll. Usually they do but occasionally a car goes through without paying. The tollbooth keeps track of the number of cars that have gone by and the total money taken. Model this with a class called Ctollbooth. The two data items are an unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected. Two member functions, called payingcar and nopaycar, are used to record paying and nonpaying cars crossing the bridge. Another member function is required to display the count of cars and money taken. Test your class with a program that allows the user to press one key to represent a paying car crossing the bridge and another key to represent a nonpaying car. Pressing a third key of your choice should print out the total cars and total cash and exit.

  4. Create a class which represents a tic-tac-toe board (noughts and crosses). Include data in your class to hold the status of each square (and enumeration of EMPTY, NOUGHT or CROSS ). Include a constructor to initialise all squares to EMPTY and a constructor to set all squares to user defined values. Write a member function to allow an EMPTY square to be changed to either NOUGHT or CROSS, Write a member function which returns true if the board is full and false if it contains any empty squares. Write a main program to test your class.

  5. Extend the tic-tac-toe board class to include an equality operator ( == ). Two boards are equal if all corresponding squares have the same status. Test your operator.

  6. Imagine a publishing company that markets both book and audio cassette versions of its works. Create a class Cpublication that stores the title (a string) and price (type float) of a publication. From this class derive two classes: cbook which adds a page count (type int) and ctape which adds a playing time in minutes (type float). Each of these three classes should have a get() function to input data from the keyboard and a put() function to display its data. Write a main() program to test the book and tape classes by creating instances of them, asking the user to fill in data with get and then displaying the data with put.

  7. Write as function which accepts 3 float arrays and their size (the same for all three arrays). The function should add corresponding elements in the first two arrays and place the result in the third array. Use pointer notation throughout - [] will not appear in the function. Write a main program to test your function.

Download