CIS162AB C - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

CIS162AB C

Description:

void outputSalesInfo(ostream& target); //ostream can handle cout or fileOut (ofstream) void displayName(Employee& emp); displayName(empSalary); displayName(empHourly) ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 34
Provided by: JuanMa4
Category:

less

Transcript and Presenter's Notes

Title: CIS162AB C


1
CIS162AB - C
  • Inheritance Juan Marquez
  • 12_inheritance.ppt

2
Overview of Topics
  • Inheritance
  • Base and Derived Classes
  • Constructor Overloading
  • Base Initialization List
  • Constructors in Derived Class
  • Function Redefinition vs Overloading
  • Function Signatures

3
Inheritance
  • Inheritance is the process by which a new class
    is created from another class, and the new class
    has additional member variables and/or functions.

4
Inheritance Terminology
  • Base Class
  • Parent Class
  • Super Class
  • Employee
  • Derived Class
  • Child Class
  • Sub Class
  • SalariedEmployee
  • HourlyEmployee

5
Labels
  • class Employee
  • private
  • protected \\use protected instead of private
  • \\ in base class
  • public

6
protected
  • Use protected when you might expect private.
  • A protected member is the same as a private
    member to any other class except a class derived
    from the base class, or derived from a class
    derived from the base class.
  • In the derived class it is private.

7
Base Class - Employee
  • class Employee
  • protected
  • string name, ssn
  • double gross
  • public
  • Employee()
  • Employee(string newName, string newSSN)
  • void displayGross()
  • void calcPay()

8
Constructor Overloading
  • Overloading is the same function name, but
    different types or number of parameters.
  • Constructors can be overloaded as well.
  • Default constructor has no parameters by
    definition.

9
Constructor with Parameters
  • Constructors with parameters is allowed.
  • Values to be assigned to members can be passed
    when an object is declared.
  • Employee emp1 //no arguments default
  • Employee emp2 (Joe Cool, 123-45-6789)

10
Derived Class - SalariedEmployee
  • class SalariedEmployee public Employee
  • private
  • double salary
  • public
  • SalariedEmployee()
  • void calcPay()

11
Access Specifier
  • The access specifier options on the inheritance
    directive are public, protected, private.
  • The access specifier, public, in front of the
    base class Employee means that the public,
    protected, and private members in the base class
    are inherited the same way in the derived class.

12
Derived Class
  • All member variables and functions defined in
    Employee are included in the class
    SalariedEmployee.
  • New variable (salary) added.
  • calcPay() is listed again, so it will redefine
    the calcPay() in Employee.

13
Derived Class - HourlyEmployee
  • class HourlyEmployee public Employee
  • private
  • double rate
  • int hours
  • public
  • HourlyEmployee()
  • void calcPay()

14
Derived Class
  • All member variables and functions defined in
    Employee are included in the class
    HourlyEmployee.
  • calcPay() is listed again, so it will redefine
    the calcPay() in Employee.
  • New variables (rate, hours) added.

15
Functions Employee
  • EmployeeEmployee()
  • cout ltlt Enter name
  • cin gtgt name
  • cout ltlt Enter SSN
  • cin gtgt ssn
  • gross 0
  • EmployeeEmployee(string na, string sn)
  • name na ssn sn
  • gross 0

16
  • void EmployeedisplayGross()
  • cout ltlt Gross ltlt gross
  • void EmployeecalcPay()
  • cout ltlt Error calcPay() called for an
  • ltlt undifferentiated employee

17
Base Initialization List
  • We need to initialize the inherited member
    variables of the derived class as well as the
    members defined directly in the derived class.
  • When defining a constructor you can initialize
    member variables in a Base Initialize List. This
    initialization section is part of the heading for
    the function.

18
  • Base Initialization List Option
  • EmployeeEmployee(string na, string sn)
    name(na), ssn(sn), gross(0)
  • //empty body
  • This function definition would replace the
    Employee constructor with parameters previously
    defined as
  • EmployeeEmployee(string na, string sn)
  • name na ssn sn gross 0

19
Default Values
  • Initialize member variables using base
    initialization list.
  • Must be used for const members.
  • class Employee
  • double TAX_RATE .05 // illegal in class
    definition
  • EmployeeEmployee () TAX_RATE(.05)
  • //empty body -- default constructor

20
Constructors in Derived Class
  • A constructor for a derived class should begin
    with an invocation of a constructor for the base
    class.
  • List constructor in base initialization list.
  • SalariedEmployeeSalariedEmployee()
  • Employee()

21
Functions SalariedEmployee
  • SalariedEmployeeSalariedEmployee() Employee()
  • cout ltlt Enter salary
  • cin gtgt salary
  • void SalariedEmployeecalcPay()
  • gross salary

22
Reusable Code
  • Inheritance allows you to reuse code.
  • Employee has the code to get name and SSN.
  • Employee has the code to displayGross()
  • The same code is used by SalariedEmployee and
    HourlyEmployee.

23
Protected Members
  • Protected members are the same as private members
    in the base and derived class.
  • Member functions defined in a derived class can
    reference or call protected members directly
    within the derived class.
  • For example SalariedEmployeecalcPay() can
    reference gross defined in Employee which is
    protected.

24
Private Members
  • Private members in the base class cannot be
    referenced or called directly by members of the
    derived class.
  • Private members are still inherited, but must use
    accessors.

25
Functions HourlyEmployee
  • HourlyEmployeeHourlyEmployee() Employee()
  • cout ltlt Enter pay rate
  • cin gtgt rate
  • cout ltlt Enter hours worked
  • cin gtgt hours
  • void HourlyEmployee calcPay()
  • gross rate hours

26
Redefinition
  • Do not list member functions from base unless you
    want to change the definition.
  • When a member function is redefined in a derived
    class you must list its prototype in the class
    definition, even though the prototype is the same
    as in the base class.
  • See calcPay() in each derived class.

27
Redefinition vs Overloading
  • A function is redefined when the new function in
    the derived class has the same name, and the
    exact number and types of parameters.
  • Overloading occurs when it is the same name, but
    different types or number of parameters.

28
Function Signatures
  • Signature includes function name and the sequence
    of types in the parameter list.Employee(string,
    string)
  • When redefined, the function in the base and
    derived class have the same signature, but the
    derived overrides the base.
  • When overloaded, they have different signatures.

29
Using Derived Classes
  • In the following code, what happens
  • when each object is declared?
  • after each call to calcPay()?
  • after each call to displayGross()?

30
  • void main()
  • Employee emp
  • emp.calcPay()
  • emp.displayGross()
  • Employee emp2 (Joe Cool, 123-45-6789)
  • SalariedEmployee empSalary
  • empSalary.calcPay()
  • empSalary.displayGross()
  • HourlyEmployee empHourly
  • empHourly.calcPay()
  • empHourly.displayGross()

31
Objects as Function Arguments
  • An object of a derived class is also an object of
    the base class.
  • base Employee
  • derived SalariedEmployee,
    HourlyEmployee
  • Object types of SalariedEmployee and
    HourlyEmployee can be passed through a parameter
    type of Employee.

32
Objects as Arguments - Example
  • Just like an ofstream can be passed through an
    ostream (see P11 SalesPersonClass).void
    outputSalesInfo(ostream target)//ostream can
    handle cout or fileOut (ofstream)void
    displayName(Employee emp)displayName(empSalary
    )displayName(empHourly)
  • However, only the members defined in the base are
    available in the function (name, ssn, gross).

33
Summary
  • Inheritance
  • Base and Derived Classes
  • Constructor Overloading
  • Base Initialization List
  • Constructors in Derived Class
  • Function Redefinition vs Overloading
  • Function Signatures
Write a Comment
User Comments (0)
About PowerShow.com