Tutorial 2 - Encapsulation, Inheritance, Polymorphism
Tutorial 2 - Encapsulation, Inheritance, Polymorphism
Tutorial 2
Topic: Encapsulation, Inheritance, Polymorphism, Abstraction
Objectives
Students should acquire the following knowledge by the end of this lab
Understand the concept of inheritance
Draw the ORD representing inheritance
Defining subclasses
Access Privileges
Protected keyword
Exercises 1
Imagine a company that has multiple levels of employees, from the managers, executives
down to clerk. Draw the object relationship diagram that shows a class of Employees,
where data like Employee No (integer), name, address and tel. no are stored. Directly
derived from the Employee class, are three other classes, which are Managers, Executives
and Clerks.
a) From the object relationship diagram in the question above, program the class
Employee. It should have:
- (public) constructor: set the employeeNo to 0, name to “John Doe”,
address to “nowhere” and tel no to “n/a”.
- (public) insertData() function to insert all the data members from the user
- (public) and a displayData() function to display the value of all the
data members.
b) Now program the class Managers that is a derived class from the Employee class.
Data members
a. (private) jobTitle : the title of the manager’s job i.e. “General Manager”
b. (private) companyCar: the company’s car the manager entitled to.
Function members
c. (public) constructor, set the jobTitle and companyCar to “unknown”.
d. (public) insertData (), sets the jobTitle and companyCar.
e. (public) displayData(), display all the data.
Programming II ITC 225 TUTORIAL 2
c) In the main function, create an object E of the class Employee. Use the
displayData() function and you should have this displayed on the screen:
d) Now create an object M of the class Managers. Use the insertData() function and
later the displayData function, refer to the screenshot below:
e) Create a class Executives, which is also derived from the class Employee. For this
class, include:
Data member
- (private) dept: the dept where the executive is assigned to i.e. “IT”
Function members
- (public) constructor, set the dept to “unknown”.
- (public) insertData (), sets the dept.
- (public) displayData(), display all the data.
f) Create a class Clerks, which is also derived from the class Employee. For this
class, include:
Data members
- (private) overtimeRate: rate per hour for overtime i.e $5
- (private) overtimeHour: number of hours of overtime.
Function members
- (public) contructor, set the overtimeRate and overtimeHour to 0.
- (public) insertData (), sets the overtimeRate and overtimeHour.
- (public) displayData(), display all the data, including total Overtime
which is equal to overtimeRate * overtimeHour.
Programming II ITC 225 TUTORIAL 2
g) In the main function, create object Ex of the class Executives and object C of the
Clerks object. Display the data members of these objects right after you have
created them. Example of result:
h) Now use the insertData() function to insert data into the object Ex and C.
Executives -----
Enter employee's number:
1
Enter employee's name:
Ahmed
Enter employee's address:
Kabul
Enter employee's phone number:
0709
Enter employee's department:
IT
Clerks -----
Enter employee's number:
99
Enter employee's name:
Ali
Enter employee's address:
88
Enter employee's phone number:
33
Enter overtime rate:
90
Enter overtime hour:
9
Executives -----
Employee's Number : 1
Employee's Name : Ahmed
Employee's Name : Kabul
Employee's Tel Number : 0709
Employee's department: IT
Clerks -----
Employee's Number : 99
Employee's Name : Ali
Employee's Name : 88
Employee's Tel Number : 33
Employee's Overtime Rate: 90.0
Employee's Overtime Rate: 9
Total overtime: 810.0