Chapter 11 - Worked Exercises 1
Exercise 1 - Vehicles
Part 0
Create a base class called Vehicle that has:
the manufacturer's name (type
std::string)number of cylinders in the engine (type
int)and owner (type
std::string)
Part 1
Create a class Truck which derives from Vehicle and has additional properties:
the load capacity in tons (type
double)the towing capacity in pounds (type
int)
Create a member function of your choice that utilises the above properties. Then, create a program that tests this member function.
Exercise 2 - Bank Accounts
Preamble
Banks have many different types of accounts, often with different rules for fees associated with transactions such as withdrawals. Customers are allowed to transfer funds between accounts incurring the appropriate fees associated with withdrawal of funds from one account.
Part 1
Create a base class BankAccount that has:
the owner of the account, stored as an
std::stringthe balance of the account, stored as a
double
Create the following member functions:
BankAccount::withdraw(double)- this function subtracts the amount from the balance, assuming that the amount is non-negative and less than or equal to the balanceBankAccount::deposit(double)- this function adds the amount to the balance, assuming that the value is non-negativeBankAccount::getBalance()- this function retrieves the balance of the accountBankAccount::getName()- this function retrieves the name of the owner of the account
Create a short driver program that tests for the functionality of the BankAccount class.
Part 2
Create a class MoneyMarketAccount that derives from BankAccount. In this account, the user gets two free withdrawals, after which a withdrawal fee of $1.50 is applied per subsequent withdrawal.
Following this, the class should have a member variable to keep track of the number of withdrawals thus far.
Override the BankAccount::withdraw(double) function to implement the functionality above.
Part 3
Create a CDAccount class (to model a Certificate of Deposit) that derives from the BankAccount class.
This account, in addition to a name and balance from its base class, should have an interest rate, stored as a double.
CDAccounts incur a penalty each time a withdrawal is made. This is 25% of the interest earned on the account:
This fee, and the value to be withdrawn, is to be deducted from the account's balance each time a withdrawal is made.
Override the BankAccount::withdraw(double) function in the CDAccount class to implement this functionality.
Part 4
For all three classes, the withdraw function should return an integer indicating the status of the withdrawal:
0- indicates that the withdrawal was successful1- indicates that the withdrawal was not successful due to insufficient funds
Part 5
Create a function with the signature int transfer(BankAccount* first, BankAccount* second) that transfers all the funds from the first account to the second account.
You will need to properly declare any base class methods as virtual to ensure that the correct ::withdraw(double) functions are being invoked.
Exercise 3 - RFID Chips
Preamble
Radio Frequency IDentification (RFID) chips are small tags that can be placed on a product. They behave like wireless barcodes and can wirelessly broadcast an identification number to a receiver.

One application of RFID chips is to use them to aid in the logistics of shipping freight. Consider a shipping container full of items. Without RFID chips, a human has to manually inventory all of the items in the container to verify the contents.
With an RFID chip attached to the shipping container, the RFID chip can electronically broadcast to a human the exact contents of the shipping container without human intervention.
Part 1
Create a class ShippingContainer that has:
A container ID number, stored as an
int
Create ShippingContainer::setContainerId(int) and ShippingContainer::getContainerId() methods.
Create a pure virtual function called getManifest. The purpose of this function will be to return a label representing the contents of the shipping container.
Part 2
Create a class ManualShippingContainer which inherits from ShippingContainer. This class represents the manual way of inventorying a container.
In this method, a human simply attaches a textual description of all contents of the container. For example, the description might be “4 crates of apples. 10 crates of pears.”
Add a new member variable of type
std::stringto store the manifest that will be manually attached to the container.Create a function
ManualShippingContainer::setManifest(std::string)that simply sets this member variable.Override the
getManifest()function so that it returns the value of this member variable.
Part 3
Create a class RFIDShippingContainer that represents the RFID method of inventorying the container.
Use the following definition of the RFID class:
struct RFIDTag {
const std::string label;
RFIDTag(std::string label): label(label) {};
};On the RFIDShippingContainer class, create an add function to simulate adding an RFID tag to the container. The class should store a list of all the RFIDTags added, and their quantities.
For example:
rfidContainer.add("box of apples");
rfidContainer.add("box of pears");
rfidContainer.add("box of apples");The data structure(s) should store a list of two RFID tags, with the labels "box of apples" and "box of pears" respectively. The quantity of the first tag is 2, and the quantity of the second tag is 1.
Override the getManifest() function such that it returns a string of all the items added to the shipping container, constructing by traversing the list of items and getting its quantity.
In the example above, the manifest to be returned would be:
2 box of apples. 1 box of pears.
The order need not to be specific so long as the contents are accurate.
Part 4
Finally, write a main program that creates an array of pointers to six ShippingContainer objects.
Instantiate the array with three ManualShippingContainer objects and three RFIDShippingContainer objects.
For the ManualShippingContainer objects, you will have to invoke setManifest to set the contents. For the RFIDShippingContainer objects, you will have to invoke add to set the contents (although, if this were real, the contents of the container would “add” themselves via the RFID chips instead of requiring a human to type them in).
Finally, write a loop that iterates through all ShippingContainer pointers and outputs each object’s manifest along with the shipping container ID. This is the output that the receiver of the shipping containers would like to see.
Last updated
Was this helpful?