3.24 Exercise


 

There is not now and never will be a language in which it is the least bit difficult to write bad programs.

 
 --Unknown, 1991

This exercise will build a set of classes to describe the staff/student hierarchy in DCU. You should follow the various exercises in order, as one is dependent on the next.

The Person class Exercise

Task: Write a generic Person class, as illustrated in Figure 3.27, “The Person class.” that builds the basic functionality of the general person role in DCU.

Figure 3.27. The Person class.

The Person class.

The output of my version can be seen below, where the setTelephone() method is called:

C:\Temp>Exercise1

Name: Derek Molloy
Address: DCU, Glasnevin, D9
Telephone: 7005355

Setting Telephone Number

Name: Derek Molloy
Address: DCU, Glasnevin, D9
Telephone: 01-7005355

Solution: My version can be seen in Exercise1.cpp. It is at the bottom of the page - Please do not look at the solution until you have had a good attempt at the exercise.


The Staff class Exercise

"Within C++, there is a much smaller and cleaner language struggling to get out."

 
 --Bjarne Stroustrup
Task: Update the previous exercise to create a child of the Person class, called Staff. This child class should have the functionality as illustrated in Figure 3.28, “The Person and Staff class hierarchy.” that builds the basic functionality of the general person role in DCU.

Figure 3.28. The Person and Staff class hierarchy.

The Person and Staff class hierarchy.

The output of my version can be seen below, where the addPayRise() method is called to give a pay rise of 5%:

C:\Temp>Exercise2
Name: Derek Molloy
Address: DCU, Glasnevin, D9
Telephone: 7005355

Creating Staff Member:

Name: Derek Molloy
Address: DCU, Glasnevin, D9
Telephone: 7005355
Office: S356
ID Number: 94971056
Salary: 12000 Euro

Payrise of 5%:

Name: Derek Molloy
Address: DCU, Glasnevin, D9
Telephone: 7005355
Office: S356
ID Number: 94971056
Salary: 12600 Euro

Solution: My version can be seen in Exercise2.cpp. Please do not look at the solution until you have had a good attempt at the exercise.

 




These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
ċ
Exercise1.cpp
(1k)
Derek Molloy,
19 Oct 2013, 06:38
ċ
Exercise2.cpp
(2k)
Derek Molloy,
19 Oct 2013, 06:39
Comments