# 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&#x20;

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.&#x20;

## Exercise 2 - Bank Accounts&#x20;

### 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&#x20;

Create a base class `BankAccount` that has:

* the owner of the account, stored as an `std::string`
* the 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 balance&#x20;
* `BankAccount::deposit(double)` - this function adds the amount to the balance, assuming that the value is non-negative&#x20;
* `BankAccount::getBalance()` - this function retrieves the balance of the account&#x20;
* `BankAccount::getName()` - this function retrieves the name of the owner of the account&#x20;

Create a short driver program that tests for the functionality of the `BankAccount` class.&#x20;

### Part 2&#x20;

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.&#x20;

Following this, the class should have a member variable to keep track of the number of withdrawals thus far.&#x20;

Override the `BankAccount::withdraw(double)` function to implement the functionality above.&#x20;

### Part 3&#x20;

Create a `CDAccount` class (to model a Certificate of Deposit) that derives from the `BankAccount` class.&#x20;

This account, in addition to a name and balance from its base class, should have an interest rate, stored as a `double`.&#x20;

`CDAccounts` incur a penalty each time a withdrawal is made. This is 25% of the interest earned on the account:

$$
fee = 0.25 \* interest\_rate \* balance
$$

This fee, and the value to be withdrawn, is to be deducted from the account's balance each time a withdrawal is made.&#x20;

Override the `BankAccount::withdraw(double)` function in the `CDAccount` class to implement this functionality.&#x20;

### Part 4&#x20;

For all three classes, the withdraw function should return an integer indicating the status of the withdrawal:

* `0` - indicates that the withdrawal was successful&#x20;
* `1` - indicates that the withdrawal was not successful due to insufficient funds&#x20;

### Part 5&#x20;

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.&#x20;

You will need to properly declare any base class methods as `virtual` to ensure that the correct `::withdraw(double)` functions are being invoked.&#x20;

## Exercise 3 - RFID Chips

### Preamble&#x20;

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.

![](https://4122513583-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSrqC8yBLK9rXNyJ6ol%2F-M_DWPKIkiPGwTePgfW8%2F-M_DWftcnOvy98yh9fT8%2Fimage.png?alt=media\&token=a9599a9e-df58-4217-822d-079908d55ded)

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&#x20;

Create a class `ShippingContainer` that has:

* A container ID number, stored as an `int`

Create `ShippingContainer::setContainerId(int)` and `ShippingContainer::getContainerId()` methods.&#x20;

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.&#x20;

### Part 2&#x20;

Create a class `ManualShippingContainer` which inherits from `ShippingContainer`. This class represents the manual way of inventorying a container.&#x20;

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::string` to store the manifest that will be manually attached to the container.
* Create a function `ManualShippingContainer::setManifest(std::string)` that simply sets this member variable.&#x20;
* Override the `getManifest()` function so that it returns the value of this member variable.&#x20;

### Part 3&#x20;

Create a class `RFIDShippingContainer` that represents the RFID method of inventorying the container.&#x20;

Use the following definition of the `RFID` class:

```cpp
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.&#x20;

For example:

```cpp
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.&#x20;

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.&#x20;

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.&#x20;

### Part 4&#x20;

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.

{% hint style="info" %}
You may need to convert an integer into a string. A simple way to do this is:&#x20;

`string s = to_string(intVariable)`
{% endhint %}
