Download presentation
Presentation is loading. Please wait.
1
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance and Polymorphism Outline 17.1 Test-Driving the Payroll Application 17.2 Inheritance Overview 17.3 Creating the Payroll Application 17.4 Using a Derived Class in the Payroll Application 17.5 Using Multiple Derived Classes in the Payroll Application 17.6 Polymorphism 17.7 Completing the Payroll Application 17.8 Wrap-Up Files Payroll.cpp <- Applicaton Employee.h, Employee.cpp <- Base Class Salaried.h, Salaried.cpp <- Derived Class Payroll.h, Payroll.cpp <- Derived Class Commission.h, Commission.cpp <- Derived Class
2
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Understand inheritance. –Form new classes quickly from existing classes using inheritance. –Use polymorphism to create an application that conveniently processes related objects as though they are the same.
3
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.1 Test-Driving the Payroll Application
4
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.1 Test-Driving the Payroll Application (Cont.) Figure 17.1 Running the completed Payroll application. Figure 17.2 Entering employee information in the Payroll application.
5
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.1 Test-Driving the Payroll Application (Cont.) Figure 17.3 Entering salary information in the Payroll application. Figure 17.4 Entering a second employee’s information into the Payroll application.
6
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.1 Test-Driving the Payroll Application (Cont.) Figure 17.5 Entering a third employee’s information into the Payroll application. Figure 17.6 Displaying the payroll.
7
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.2 Inheritance Overview Inheritance –A derived class absorbs data members from its base class The derived class normally adds its own data members and member functions A derived class is typically more specialized than its base class –Is a relationship An object of the derived class is an object of the base class –Class Hierarchy Defines inheritance relationships among classes Direct base classes are one level above their derived classes Indirect base classes are two or more levels above their derived classes
8
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.2 Inheritance Overview (Cont.)
9
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.2 Inheritance Overview (Cont.) Figure 17.8 Inheritance hierarchy for university students.
10
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.2 Inheritance Overview (Cont.) Figure 17.9 Inheritance hierarchy for university CommunityMembers.
11
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.2 Inheritance Overview (Cont.) public inheritance –Use a colon, the public keyword and the base class name –Non private members retain their original member access –private members are also inherited, but are not directly accessible in the derived class The derived class must use the base class’s get and set functions –Base classes can redefine the functions that they inherit
12
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.2 Inheritance Overview (Cont.) Figure 17.10 Inheritance hierarchy for Shapes.
13
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.3 Creating the Payroll Application /* Later in the tutorial we will add Hourly and Commission based Employees. */
14
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.3 Creating the Payroll Application (Cont.) Figure 17.12 Employee class definition.
15
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.3 Creating the Payroll Application (Cont.) Figure 17.13 Including the base-class definition. Including the Employee.h header file
16
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.3 Creating the Payroll Application (Cont.) Figure 17.14 Defining class SalariedEmployee. SalariedEmployee inherits from Employee Creating a data member for the weekly salary Default arguments The default value is assigned to the argument if the function call does not specify that argument
17
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.3 Creating the Payroll Application (Cont.) Figure 17.15 SalariedEmployee constructor definition. Defining the SalariedEmployee class’s constructor Base-class initializer syntax Call base-class constructor with a member initializer Member initializer list initializes a class’s data members Comma-separated list, separated from parameter list by a colon
18
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.3 Creating the Payroll Application (Cont.) Figure 17.16 Set and get functions for the data member weeklySalary. Defining get and set functions for the weeklySalary data member
19
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.3 Creating the Payroll Application (Cont.) Figure 17.17 Redefining the print function. Displaying SalariedEmployee information Redefine (do not override) base class print method. Calling a base-class function from a derived class Use the base class’s name and the binary scope resolution operator before the function name
20
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.4 Using a Derived Class in the Payroll Application Figure 17.18 Including the SalariedEmployee class definition. Including the Salaried.h header file Figure 17.19 Creating an array of pointers to SalariedEmployee objects. Defining employees to store 10 pointers to SalariedEmployee objects
21
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.4 Using a Derived Class in the Payroll Application (Cont.) Figure 17.20 Payroll application’s createEmployee function. Defining variables to store user input Prompting the user for and inputting an employee’s name, social security number and salary
22
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.4 Using a Derived Class in the Payroll Application (Cont.) Figure 17.21 Creating a new SalariedEmployee object. Creating a new SalariedEmployee object
23
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.4 Using a Derived Class in the Payroll Application (Cont.) Figure 17.22 Displaying information and wages for each SalariedEmployee. Repeating for each SalariedEmployee pointer in the array Displaying the name, social security number and employee type Displaying the weekly salaryAdding the salary to the total Displaying the total payroll
24
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.4 Using a Derived Class in the Payroll Application (Cont.) Figure 17.23 Deleting each SalariedEmployee object. Deleting each SalariedEmployee object Figure 17.24 Entering information for the Payroll application using SalariedEmployee.
25
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.4 Using a Derived Class in the Payroll Application (Cont.) Figure 17.25 Displaying the payroll in the Payroll application using SalariedEmployee.
26
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application Figure 17.26 UML class diagram for the Employee inheritance hierarchy in the Payroll application. /* We will now add Hourly and Commission based Employees. This section demonstrates the redefining a function no longer works when there is more than one derived class. */
27
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.27 Declaring the earnings function in the Employee class. Declaring the earnings function Figure 17.28 earnings function definition for the Employee class. Defining the Employee class function earnings
28
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.29 HourlyEmployee Class definition. HourlyEmployee class definition
29
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.30 earnings and print function definitions in the Employee class. Redefining the earnings function in the HourlyEmployee class Redefining the print function in the HourlyEmployee class /* Here is where the errors are introduced. The why question answered shortly. */
30
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.31 Including the CommissionEmployee and HourlyEmployee class definitions. Including the Commission.h and Hourly.h header files
31
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.32 Creating an array of pointers to Employee objects. employee stores ARRAY_SIZE (10) pointers to Employee objects /* An array can not store pointers to objects of different types. However, with inheritance, each of our objects (Salary, Commission, Hourly) is an Employee. As a result we can store pointers to the derived classes as pointers to the base class Instances of all our derived classes (Salary, Commission, Hourly) are now saved as pointers of the base class type, but NOT pointing to the base class. */
32
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.33 Viewing the modified createEmployee function. Prompting the user for and inputting the employee name and social security Displaying a menu that prompts the user for the employee type Prompting the user for and inputting the employee’s salary and creating a new SalariedEmployee object
33
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.34 Creating a new CommissionEmployee object. Creating a new CommissionEmployee object
34
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.35 Creating a new HourlyEmployee object. Creating a new HourlyEmployee object
35
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.36 Displaying information and wages for each Employee. Display the earnings Adding the earnings to the total
36
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.37 Entering hourly employee information in the Payroll application. Entering earnings information for an hourly employee
37
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.38 Entering salaried employee information in the Payroll application. Entering earnings information for a salaried employee
38
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) Figure 17.39 Payroll application displaying incorrect output. Application does not display employee type and displays incorrect earnings information /* Redefining both earnings and print have failed.*/
39
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.5 Using Multiple Derived Classes in the Payroll Application (Cont.) C++ uses the type of the pointer to an object, not the type of the object, to determine which function to call when a derived class redefines a base class function /* Here is the problem. The answer to the why question.*/
40
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.6 Polymorphism Polymorphism allows a function call to act differently depending on the type of the object invoking it –Polymorphism means “many forms” virtual functions –Use the type of the object being pointed to determine which version of the virtual function to call –virtual functions are overridden by derived classes –To declare a function virtual precede its declaration with the virtual keyword /* Here is the solution. Virtual functions come in two flavors (normal and pure).*/
41
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.6 Polymorphism (Cont.) Abstract classes are too general to be instantiated –Make a class abstract by declaring a pure virtual function A pure virtual function has virtual before and = 0 after its declaration A pure virtual function cannot be defined –Abstract classes can be used to define pointers The pointers can then be pointed to objects of any concrete class derived from the abstract class Concrete classes are classes that can be instantiated –A concrete class must override all pure virtual functions it inherits from its base class (if that base class is abstract)
42
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.7 Completing the Payroll Application Figure 17.40 Declaring a virtual function and a pure virtual function in the Employee class. Declaring functions as virtual so that they can be overridden Declaring function as pure virtual. Figure 17.41 Updating comments in the displayEmployees function.
43
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.7 Completing the Payroll Application (Cont.) Figure 17.42 Entering employee information in the completed Payroll application.
44
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17.7 Completing the Payroll Application (Cont.) Figure 17.43 Payroll application using virtual functions to access objects polymorphically. Displaying employee type and correct earnings
45
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Employee.h (1 of 2)
46
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Employee.h (2 of 2) Declare functions as virtual so that they can be processed polymorphically
47
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Employee.cpp (1 of 4)
48
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Employee.cpp (2 of 4)
49
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Employee.cpp (3 of 4)
50
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Employee.cpp (4 of 4)
51
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Salaried.h Overridden virtual functions
52
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Salaried.cpp (1 of 3)
53
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Salaried.cpp (2 of 3)
54
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Salaried.cpp (3 of 3) earnings function definition in the SalariedEmployee class print function definition in the SalariedEmployee class
55
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Hourly.h (1 of 2)
56
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Hourly.h (2 of 2) Overridden virtual functions
57
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Hourly.cpp (1 of 5)
58
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Hourly.cpp (2 of 5)
59
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Hourly.cpp (3 of 5)
60
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Hourly.cpp (4 of 5) earnings function definition in the HourlyEmployee class
61
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Hourly.cpp (5 of 5) print function definition in the HourlyEmployee class
62
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Commission.h (1 of 2)
63
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Commission.h (2 of 2) Overridden virtual functions
64
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Commission.cpp (1 of 4)
65
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Commission.cpp (2 of 4)
66
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Commission.cpp (3 of 4)
67
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Commission.cpp (4 of 4) earnings function definition in the CommissionEmployee class print function definition in the CommissionEmployee class
68
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Payroll.cpp (1 of 9) Including header files for classes derived from Employee Function prototypes declaring parameters using an array of Employee pointers
69
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Payroll.cpp (2 of 9) Array of pointers to Employee s that enables the application to process employees polymorphically
70
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Payroll.cpp (3 of 9)
71
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Payroll.cpp (4 of 9) Use delete to destroy each object referenced by a pointer in the employees array
72
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Payroll.cpp (5 of 9) Switch based on the selected employee type Begin the case for a salaried employee Prompt the user for and input the employee type
73
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Payroll.cpp (6 of 9) Use new to create a new SalariedEmployee object and store a pointer to it in the employees array Begin the case for a commissioned employee Use new to create a new CommissionEmployee object and store a pointer to it in the employees array
74
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Payroll.cpp (7 of 9) Begin the case for an hourly employee Use new to create a new HourlyEmployee object and store a pointer to it in the employees array
75
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Payroll.cpp (8 of 9) Process each object derived from Employee polymorphically; the overridden earnings function defined by each subclass is called Add earnings to total Display each employee’s name and type using polymorphism
76
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Payroll.cpp (9 of 9) Display total earnings
77
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 17 − Payroll Application. Turn in annotated source file with your own comments for Exercise 17.13 or 17.11 if you did not complete 17.13. Answer and Turn-in Tutorial 17 Questions 17.1 to 17.10. Note: The author defines “synonym” on page 371. Always write the question followed by the answer. Remember to highlight the answer. Exercises 17.11 and 17.13. For both exercises start from your completed Tutorial. Due next Wednesday
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.