Module 4
Objects classes and methods
Activity 4.1
1. Taking the in-class example of the car, continue coding this example but include one or more additional variables for the speed of the car. Include mutator and accessor methods for the car’s speed and use them in the start(), accelerate(),
ake() and stop() methods to extend their functionality. When the car starts, initialise its speed to 0. Allow the accelerator method to increase the speed by a given increment and the
ake method to decrease by a given decrement. Print the cu
ent speed to the screen.
2. Race time: take the same example and separate the class into a header file. Write another program that instantiates two car objects, one small and the other large (if you don’t know cars, look up examples for
ands of cars and their fuel tank capacities). In your code, start each car and then accelerate each car at different rates. Do this in a loop three times for each car and then reverse the rates for
aking. Print the name of the car that comes to a complete stop first and declare the other car the winner.
Constructors and inheritance
Activity 4.2
1. Taking your work from the first half of the module, create constructor and destructor methods for the ‘Car’ class.
2. Write a program using the object-oriented paradigm that keeps track of bank accounts. Each class should have constructor and destructor methods but the choice of members will be up to you. Start with a class for ‘account’ and then allow for different types of derived classes, such as ‘savings account’ and ‘credit card account.’ A bank account should allow funds to be added to it as well as withdrawn but not to be overdrawn. A credit card account should allow funds to be charged to it but not more than its limit. In the main() function, open a savings account with $10,000 and then make three withdrawals of $4,000, if there are enough funds. Open a credit card account, with a limit of $3000 and make two charges of $2000 if there are enough funds. If you
each the conditions for each, display an appropriate e
or message.