SlideShare a Scribd company logo
Introduction to Inheritance
Object Oriented Programming Copyright © 2012 IM
Group. All rights
reserved
Inheritance
• Inheritance refers to derived classes
▫ Derived classes are obtained from another class
by adding features
▫ The class of input-file streams is derived from the
class of all input streams by adding member
functions such as open and close
▫ cin belongs to the class of all input streams, but not
the class of input-file streams
Copyright © 2012 IM
Group. All rights
reserved
Inheritance and Streams
• cin and an input-file stream are input streams
▫ Input-file streams are members of the class
ifstream
 Can be connected to a file
▫ cin is a member of the class istream (no 'f ')
 Cannot be connected to a file
▫ The ifstream class is a derived class of the
istream class
Copyright © 2012 IM
Group. All rights
reserved
Stream Parameters Review
• Example:
void two_sum(ifstream& source_file)
{
int n1, n2;
source_file >> n1 >> n2;
cout << n1 " + " << n2
<< " = " << (n1 + n2) << endl;
}
• This code could be called using
ifstream fin;
fin.open("input.dat");
two_sum (fin);
Copyright © 2012 IM
Group. All rights
reserved
two_sum Is Not Versatile
• Suppose you wished to use function two_sum
with cin
▫ Since cin and input-file streams are both input
streams, this call to two_sum seems to make
sense:
two_sum(cin);
but it will not work!
Copyright © 2012 IM
Group. All rights
reserved
Fixing two_sum
• This version of two_sum works with cin:
void better_two_sum(istream& source_file)
{
int n1, n2;
source_file >> n1 >> n2;
cout << n1 " + " << n2
<< " = " << (n1 + n2) << endl;
}
▫ better_two_sum can be called with:
better_two_sum(cin);
Copyright © 2012 IM
Group. All rights
reserved
Derived Classes and Parameters
• better_two_sum can also be called with:
ifstream fin;
fin.open("input.dat");
better_two_sum(fin);
• fin is of two types
▫ fin is an input-file stream
▫ fin is also of type istream
▫ fin has all the features of the input stream class,
plus added capabilities
• A formal parameter of type istream can be replaced by
an argument of type ifstream
Copyright © 2012 IM
Group. All rights
reserved
Inheritance Relationships
• If class B is derived from class A
▫ Class B is a derived class of class A
▫ Class B is a child of class A
▫ Class A is the parent of class B
▫ Class B inherits the member functions of class A
Copyright © 2012 IM
Group. All rights
reserved
Inheritance and Output
• ostream is the class of all output streams
▫ cout is of type ostream
• ofstream is the class of output-file streams
▫ The ofstream class is a child class of ostream
▫ This function can be called with ostream or
ofstream arguments
void say_hello(ostream& any_out_stream)
{
any_out_stream << "Hello";
}
Copyright © 2012 IM
Group. All rights
reserved
Default Arguments
• It is not necessary to have two versions of
the new_line function
▫ A default value can be specified in the parameter
list
▫ The default value is selected if no argument is
available for the parameter
• The new_line header can be written as
new_line (istream & in_stream = cin)
▫ If new_line is called without an argument, cin is
used
Copyright © 2012 IM
Group. All rights
reserved
Multiple Default Arguments
• When some formal parameters have default
values and others do not
▫ All formal parameters with default values must be
at the end of the parameter list
▫ Arguments are applied to the all formal parameters
in order just as we have seen before
▫ The function call must provide at least as many
arguments as there are parameters without default
values
Copyright © 2012 IM
Group. All rights
reserved
Default Argument Example
• void default_args(int arg1, int arg2 = -3)
{
cout << arg1 << ' ' << arg2 << endl;
}
• default_args can be called with one or
two parameters
▫ default_args(5); //output is 5 -3
▫ default_args(5, 6); //output is 5 6
Copyright © 2012 IM
Group. All rights
reserved
Copyright ©
2012 IM
Group. All
rights reserved
Inheritance
Object Oriented Programming
Inheritance Basics
• Inheritance is the process by which a new class,
called a derived class, is created from another
class, called the base class
▫ A derived class automatically has all the member
variables and functions of the base class
▫ A derived class can have additional member
variables and/or member functions
▫ The derived class is a child of the base or parent
class
Copyright © 2012 IM
Group. All rights
reserved
Employee Classes
• To design a record-keeping program with
records for salaried and hourly employees…
▫ Salaried and hourly employees belong to a class of
people who share the property "employee"
▫ A subset of employees are those with a fixed wage
▫ Another subset of employees earn hourly wages
• All employees have a name and SSN
▫ Functions to manipulate name and SSN are the same
for hourly and salaried employees
Copyright © 2012 IM
Group. All rights
reserved
• We will define a class called Employee for all
employees
• The Employee class will be used to define
classes for hourly and salaried employees.
A Base Class
Copyright © 2012 IM
Group. All rights
reserved
Training
• Let’s create the Employee class.
Copyright © 2012 IM
Group. All rights
reserved
Function print_check
• Function print_check will have different
definitions to print different checks for each type
of employee
▫ An Employee object lacks sufficient information to
print a check
▫ Each derived class will have sufficient information
to print a check
Copyright © 2012 IM
Group. All rights
reserved
Training
• Let’s create print_check function for class
Employee.
Copyright © 2012 IM
Group. All rights
reserved
• HourlyEmployee is derived from Class Employee
▫ HourlyEmployee inherits all member functions and
member variables of Employee
▫ The class definition begins
class HourlyEmployee : public Employee
 :public Employee shows that HourlyEmployee is
derived from class Employee
▫ HourlyEmployee declares additional member
variables wage_rate and hours
Class HourlyEmployee
Copyright © 2012 IM
Group. All rights
reserved
Inherited Members
• A derived class inherits all the members of the
parent class
▫ The derived class does not re-declare or re-define
members inherited from the parent, except…
▫ The derived class re-declares and re-defines
member functions of the parent class that will
have a different definition in the derived class
▫ The derived class can add member variables and
functions
Copyright © 2012 IM
Group. All rights
reserved
• Any member functions added in the derived
class are defined in the implementation file for
the derived class
▫ Definitions are not given for inherited functions
that are not to be changed
Implementing a Derived Class
Copyright © 2012 IM
Group. All rights
reserved
Training
• Let’s create the HourlyEmployee class.
Copyright © 2012 IM
Group. All rights
reserved
• The class SalariedEmployee is also derived from
Employee
▫ Function print_check is redefined to have a
meaning specific to salaried employees
▫ SalariedEmployee adds a member variable salary
Class SalariedEmployee
Copyright © 2012 IM
Group. All rights
reserved
Parent and Child Classes
• Recall that a child class automatically has all the
members of the parent class
• The parent class is an ancestor of the child class
• The child class is a descendent of the parent
class
• The parent class (Employee) contains all the
code common to the child classes
▫ You do not have to re-write the code for each child
Copyright © 2012 IM
Group. All rights
reserved
Derived Class Types
• An hourly employee is an employee
▫ In C++, an object of type HourlyEmployee can be
used where an object of type Employee can be
used
▫ An object of a class type can be used wherever any
of its ancestors can be used
▫ An ancestor cannot be used wherever one of its
descendents can be used
Copyright © 2012 IM
Group. All rights
reserved
• A base class constructor is not inherited in a
derived class
▫ The base class constructor can be invoked by the
constructor of the derived class
▫ The constructor of a derived class begins by invoking
the constructor of the base class in the initialization
section:
HourlyEmployee::HourlyEmployee : Employee( ),
wage_rate( 0),
hours( )
{ //no code needed }
Derived Class Constructors
Copyright © 2012 IM
Group. All rights
reserved
Default Initialization
• If a derived class constructor does not invoke a
base class constructor explicity, the base class
default constructor will be used
• If class B is derived from class A and class C
is derived from class B
▫ When a object of class C is created
 The base class A's constructor is the first invoked
 Class B's constructor is invoked next
 C's constructor completes execution
Copyright © 2012 IM
Group. All rights
reserved
Private is Private
• A member variable (or function) that is private
in the parent class is not accessible to the
child class
▫ The parent class member functions must be used
to access the private members of the parent
▫ This code would be illegal:
void HourlyEmployee::print_check( )
{
net_pay = hours * wage_rage;
 net_pay is a private member of Employee!
Copyright © 2012 IM
Group. All rights
reserved
The protected Qualifier
• protected members of a class appear to be
private outside the class, but are accessible by
derived classes
▫ If member variables name, net_pay, and ssn are
listed as protected (not private) in the Employee
class, this code, illegal on the previous slide,
becomes legal:
HourlyEmployee::print_check( )
{
net_pay = hours * wage_rage;
Copyright © 2012 IM
Group. All rights
reserved
Programming Style
• Using protected members of a class is a
convenience to facilitate writing the code of
derived classes.
• Protected members are not necessary
▫ Derived classes can use the public methods of
their ancestor classes to access private members
• Many programming authorities consider it
bad style to use protected member variables
Copyright © 2012 IM
Group. All rights
reserved
• When defining a derived class, only list the
inherited functions that you wish to change
for the derived class
▫ The function is declared in the class definition
▫ HourlyEmployee and SalariedEmployee each have
their own definitions of print_check
Redefinition of Member Functions
Copyright © 2012 IM
Group. All rights
reserved
Redefining or Overloading
• A function redefined in a derived class has the
same number and type of parameters
▫ The derived class has only one function with the
same name as the base class
• An overloaded function has a different number
and/or type of parameters than the base class
▫ The derived class has two functions with the same
name as the base class
 One is defined in the base class, one in the derived
class
Copyright © 2012 IM
Group. All rights
reserved
Access to a Redefined Base Function
• When a base class function is redefined in a
derived class, the base class function can still
be used
▫ To specify that you want to use the base class
version of the redefined function:
HourlyEmployee sally_h;
sally_h.Employee::print_check( );
Copyright © 2012 IM
Group. All rights
reserved
Inheritance Details
• Some special functions are, for all practical
purposes, not inherited by a derived class
▫ Some of the special functions that are not
effectively inherited by a derived class include
 Destructors
 Copy constructors
 The assignment operator
Copyright © 2012 IM
Group. All rights
reserved
Copyright ©
2012 IM
Group. All
rights reserved
Polymorphism
Object Oriented Programming
Polymorphism
• Polymorphism refers to the ability to associate
multiple meanings with one function name
using a mechanism called late binding
• Polymorphism is a key component of the
philosophy of object oriented programming
Copyright © 2012 IM
Group. All rights
reserved
A Late Binding Example
• Imagine a graphics program with several types
of figures
▫ Each figure may be an object of a different class,
such as a circle, oval, rectangle, etc.
▫ Each is a descendant of a class Figure
▫ Each has a function draw( ) implemented with
code specific to each shape
▫ Class Figure has functions common to all figures
Copyright © 2012 IM
Group. All rights
reserved
A Problem
• Suppose that class Figure has a function center
▫ Function center moves a figure to the center of the
screen by erasing the figure and redrawing it in
the center of the screen
▫ Function center is inherited by each of the derived
classes
 Function center uses each derived object's draw
function to draw the figure
 The Figure class does not know about its derived
classes, so it cannot know how to draw each figure
Copyright © 2012 IM
Group. All rights
reserved
Virtual Functions
• Because the Figure class includes a method to
draw figures, but the Figure class cannot know
how to draw the figures, virtual functions are
used
• Making a function virtual tells the compiler that
you don't know how the function is implemented
and to wait until the function is used in a
program, then get the implementation from the
object.
▫ This is called late binding
Copyright © 2012 IM
Group. All rights
reserved
Virtual Details
• To define a function differently in a derived class
and to make it virtual
▫ Add keyword virtual to the function declaration in
the base class
▫ virtual is not needed for the function declaration in
the derived class, but is often included
▫ virtual is not added to the function definition
Copyright © 2012 IM
Group. All rights
reserved
Overriding
• Virtual functions whose definitions are changed
in a derived class are said to be overridden
• Non-virtual functions whose definitions are
changed in a derived class are redefined
Copyright © 2012 IM
Group. All rights
reserved
Any Questions
Session 5 Copyright © 2012 IM
Group. All rights
reserved

More Related Content

PPTX
OOP - Introduction
PPT
Object Oriented Programming with Java
PDF
Object-Oriented Design: Multiple inheritance (C++ and C#)
PPSX
Short notes of oop with java
PDF
Distributed Programming (RMI)
PPTX
Oop c++class(final).ppt
KEY
Practical OOP In Java
PDF
Internationalization
OOP - Introduction
Object Oriented Programming with Java
Object-Oriented Design: Multiple inheritance (C++ and C#)
Short notes of oop with java
Distributed Programming (RMI)
Oop c++class(final).ppt
Practical OOP In Java
Internationalization

What's hot (6)

PDF
Exception handling
PDF
String handling(string buffer class)
PPTX
Object Oriented Programming Concepts
PDF
Object Oriented Programming using C++ Part I
PDF
OOPs Concepts - Android Programming
PPT
Introduction to-programming
Exception handling
String handling(string buffer class)
Object Oriented Programming Concepts
Object Oriented Programming using C++ Part I
OOPs Concepts - Android Programming
Introduction to-programming
Ad

Similar to OOP - Introduction to Inheritance (20)

PPTX
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
PDF
Chapter 8 Inheritance
PPT
Savitch Ch 15
PPT
Savitch ch 15
PPTX
OOPS IN C++
PPTX
Introduction to inheritance and different types of inheritance
PDF
Chapter 6 and inheritance OOP C++ tu ioe
PPT
Inheritance in C++
PPT
10.Inheritance.ppt for oops programinggg
PPTX
OOPS Basics With Example
PPTX
Inheritance.pptx
PPTX
INHERITANCE.pptx
PPTX
Object Oriented Programming using C++(UNIT 1)
PDF
2 BytesC++ course_2014_c11_ inheritance
PPTX
Object Oriented Programming using c++.pptx
PPTX
Inheritance
PDF
C++ L10-Inheritance
PDF
Inheritance
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
Chapter 8 Inheritance
Savitch Ch 15
Savitch ch 15
OOPS IN C++
Introduction to inheritance and different types of inheritance
Chapter 6 and inheritance OOP C++ tu ioe
Inheritance in C++
10.Inheritance.ppt for oops programinggg
OOPS Basics With Example
Inheritance.pptx
INHERITANCE.pptx
Object Oriented Programming using C++(UNIT 1)
2 BytesC++ course_2014_c11_ inheritance
Object Oriented Programming using c++.pptx
Inheritance
C++ L10-Inheritance
Inheritance
Ad

More from Mohammad Shaker (10)

PPTX
Android Development - Session 5
PPTX
Android Development - Session 4
PPTX
Android Development - Session 2
PPTX
Android Development - Session 1
PPTX
Introduction to Qt
PPTX
OOP - STL
PPTX
OOP - Templates
PPTX
OOP - Friend Functions
PPTX
NoSQL - A Closer Look to Couchbase
PPTX
Introduction to Couchbase
Android Development - Session 5
Android Development - Session 4
Android Development - Session 2
Android Development - Session 1
Introduction to Qt
OOP - STL
OOP - Templates
OOP - Friend Functions
NoSQL - A Closer Look to Couchbase
Introduction to Couchbase

Recently uploaded (20)

PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
System and Network Administration Chapter 2
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Transform Your Business with a Software ERP System
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPT
Introduction Database Management System for Course Database
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Digital Strategies for Manufacturing Companies
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Nekopoi APK 2025 free lastest update
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
ai tools demonstartion for schools and inter college
PDF
Design an Analysis of Algorithms I-SECS-1021-03
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Which alternative to Crystal Reports is best for small or large businesses.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
System and Network Administration Chapter 2
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
How to Choose the Right IT Partner for Your Business in Malaysia
Transform Your Business with a Software ERP System
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Upgrade and Innovation Strategies for SAP ERP Customers
Odoo Companies in India – Driving Business Transformation.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Introduction Database Management System for Course Database
Computer Software and OS of computer science of grade 11.pptx
Digital Strategies for Manufacturing Companies
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Nekopoi APK 2025 free lastest update
How to Migrate SBCGlobal Email to Yahoo Easily
ai tools demonstartion for schools and inter college
Design an Analysis of Algorithms I-SECS-1021-03

OOP - Introduction to Inheritance

  • 1. Introduction to Inheritance Object Oriented Programming Copyright © 2012 IM Group. All rights reserved
  • 2. Inheritance • Inheritance refers to derived classes ▫ Derived classes are obtained from another class by adding features ▫ The class of input-file streams is derived from the class of all input streams by adding member functions such as open and close ▫ cin belongs to the class of all input streams, but not the class of input-file streams Copyright © 2012 IM Group. All rights reserved
  • 3. Inheritance and Streams • cin and an input-file stream are input streams ▫ Input-file streams are members of the class ifstream  Can be connected to a file ▫ cin is a member of the class istream (no 'f ')  Cannot be connected to a file ▫ The ifstream class is a derived class of the istream class Copyright © 2012 IM Group. All rights reserved
  • 4. Stream Parameters Review • Example: void two_sum(ifstream& source_file) { int n1, n2; source_file >> n1 >> n2; cout << n1 " + " << n2 << " = " << (n1 + n2) << endl; } • This code could be called using ifstream fin; fin.open("input.dat"); two_sum (fin); Copyright © 2012 IM Group. All rights reserved
  • 5. two_sum Is Not Versatile • Suppose you wished to use function two_sum with cin ▫ Since cin and input-file streams are both input streams, this call to two_sum seems to make sense: two_sum(cin); but it will not work! Copyright © 2012 IM Group. All rights reserved
  • 6. Fixing two_sum • This version of two_sum works with cin: void better_two_sum(istream& source_file) { int n1, n2; source_file >> n1 >> n2; cout << n1 " + " << n2 << " = " << (n1 + n2) << endl; } ▫ better_two_sum can be called with: better_two_sum(cin); Copyright © 2012 IM Group. All rights reserved
  • 7. Derived Classes and Parameters • better_two_sum can also be called with: ifstream fin; fin.open("input.dat"); better_two_sum(fin); • fin is of two types ▫ fin is an input-file stream ▫ fin is also of type istream ▫ fin has all the features of the input stream class, plus added capabilities • A formal parameter of type istream can be replaced by an argument of type ifstream Copyright © 2012 IM Group. All rights reserved
  • 8. Inheritance Relationships • If class B is derived from class A ▫ Class B is a derived class of class A ▫ Class B is a child of class A ▫ Class A is the parent of class B ▫ Class B inherits the member functions of class A Copyright © 2012 IM Group. All rights reserved
  • 9. Inheritance and Output • ostream is the class of all output streams ▫ cout is of type ostream • ofstream is the class of output-file streams ▫ The ofstream class is a child class of ostream ▫ This function can be called with ostream or ofstream arguments void say_hello(ostream& any_out_stream) { any_out_stream << "Hello"; } Copyright © 2012 IM Group. All rights reserved
  • 10. Default Arguments • It is not necessary to have two versions of the new_line function ▫ A default value can be specified in the parameter list ▫ The default value is selected if no argument is available for the parameter • The new_line header can be written as new_line (istream & in_stream = cin) ▫ If new_line is called without an argument, cin is used Copyright © 2012 IM Group. All rights reserved
  • 11. Multiple Default Arguments • When some formal parameters have default values and others do not ▫ All formal parameters with default values must be at the end of the parameter list ▫ Arguments are applied to the all formal parameters in order just as we have seen before ▫ The function call must provide at least as many arguments as there are parameters without default values Copyright © 2012 IM Group. All rights reserved
  • 12. Default Argument Example • void default_args(int arg1, int arg2 = -3) { cout << arg1 << ' ' << arg2 << endl; } • default_args can be called with one or two parameters ▫ default_args(5); //output is 5 -3 ▫ default_args(5, 6); //output is 5 6 Copyright © 2012 IM Group. All rights reserved
  • 13. Copyright © 2012 IM Group. All rights reserved Inheritance Object Oriented Programming
  • 14. Inheritance Basics • Inheritance is the process by which a new class, called a derived class, is created from another class, called the base class ▫ A derived class automatically has all the member variables and functions of the base class ▫ A derived class can have additional member variables and/or member functions ▫ The derived class is a child of the base or parent class Copyright © 2012 IM Group. All rights reserved
  • 15. Employee Classes • To design a record-keeping program with records for salaried and hourly employees… ▫ Salaried and hourly employees belong to a class of people who share the property "employee" ▫ A subset of employees are those with a fixed wage ▫ Another subset of employees earn hourly wages • All employees have a name and SSN ▫ Functions to manipulate name and SSN are the same for hourly and salaried employees Copyright © 2012 IM Group. All rights reserved
  • 16. • We will define a class called Employee for all employees • The Employee class will be used to define classes for hourly and salaried employees. A Base Class Copyright © 2012 IM Group. All rights reserved
  • 17. Training • Let’s create the Employee class. Copyright © 2012 IM Group. All rights reserved
  • 18. Function print_check • Function print_check will have different definitions to print different checks for each type of employee ▫ An Employee object lacks sufficient information to print a check ▫ Each derived class will have sufficient information to print a check Copyright © 2012 IM Group. All rights reserved
  • 19. Training • Let’s create print_check function for class Employee. Copyright © 2012 IM Group. All rights reserved
  • 20. • HourlyEmployee is derived from Class Employee ▫ HourlyEmployee inherits all member functions and member variables of Employee ▫ The class definition begins class HourlyEmployee : public Employee  :public Employee shows that HourlyEmployee is derived from class Employee ▫ HourlyEmployee declares additional member variables wage_rate and hours Class HourlyEmployee Copyright © 2012 IM Group. All rights reserved
  • 21. Inherited Members • A derived class inherits all the members of the parent class ▫ The derived class does not re-declare or re-define members inherited from the parent, except… ▫ The derived class re-declares and re-defines member functions of the parent class that will have a different definition in the derived class ▫ The derived class can add member variables and functions Copyright © 2012 IM Group. All rights reserved
  • 22. • Any member functions added in the derived class are defined in the implementation file for the derived class ▫ Definitions are not given for inherited functions that are not to be changed Implementing a Derived Class Copyright © 2012 IM Group. All rights reserved
  • 23. Training • Let’s create the HourlyEmployee class. Copyright © 2012 IM Group. All rights reserved
  • 24. • The class SalariedEmployee is also derived from Employee ▫ Function print_check is redefined to have a meaning specific to salaried employees ▫ SalariedEmployee adds a member variable salary Class SalariedEmployee Copyright © 2012 IM Group. All rights reserved
  • 25. Parent and Child Classes • Recall that a child class automatically has all the members of the parent class • The parent class is an ancestor of the child class • The child class is a descendent of the parent class • The parent class (Employee) contains all the code common to the child classes ▫ You do not have to re-write the code for each child Copyright © 2012 IM Group. All rights reserved
  • 26. Derived Class Types • An hourly employee is an employee ▫ In C++, an object of type HourlyEmployee can be used where an object of type Employee can be used ▫ An object of a class type can be used wherever any of its ancestors can be used ▫ An ancestor cannot be used wherever one of its descendents can be used Copyright © 2012 IM Group. All rights reserved
  • 27. • A base class constructor is not inherited in a derived class ▫ The base class constructor can be invoked by the constructor of the derived class ▫ The constructor of a derived class begins by invoking the constructor of the base class in the initialization section: HourlyEmployee::HourlyEmployee : Employee( ), wage_rate( 0), hours( ) { //no code needed } Derived Class Constructors Copyright © 2012 IM Group. All rights reserved
  • 28. Default Initialization • If a derived class constructor does not invoke a base class constructor explicity, the base class default constructor will be used • If class B is derived from class A and class C is derived from class B ▫ When a object of class C is created  The base class A's constructor is the first invoked  Class B's constructor is invoked next  C's constructor completes execution Copyright © 2012 IM Group. All rights reserved
  • 29. Private is Private • A member variable (or function) that is private in the parent class is not accessible to the child class ▫ The parent class member functions must be used to access the private members of the parent ▫ This code would be illegal: void HourlyEmployee::print_check( ) { net_pay = hours * wage_rage;  net_pay is a private member of Employee! Copyright © 2012 IM Group. All rights reserved
  • 30. The protected Qualifier • protected members of a class appear to be private outside the class, but are accessible by derived classes ▫ If member variables name, net_pay, and ssn are listed as protected (not private) in the Employee class, this code, illegal on the previous slide, becomes legal: HourlyEmployee::print_check( ) { net_pay = hours * wage_rage; Copyright © 2012 IM Group. All rights reserved
  • 31. Programming Style • Using protected members of a class is a convenience to facilitate writing the code of derived classes. • Protected members are not necessary ▫ Derived classes can use the public methods of their ancestor classes to access private members • Many programming authorities consider it bad style to use protected member variables Copyright © 2012 IM Group. All rights reserved
  • 32. • When defining a derived class, only list the inherited functions that you wish to change for the derived class ▫ The function is declared in the class definition ▫ HourlyEmployee and SalariedEmployee each have their own definitions of print_check Redefinition of Member Functions Copyright © 2012 IM Group. All rights reserved
  • 33. Redefining or Overloading • A function redefined in a derived class has the same number and type of parameters ▫ The derived class has only one function with the same name as the base class • An overloaded function has a different number and/or type of parameters than the base class ▫ The derived class has two functions with the same name as the base class  One is defined in the base class, one in the derived class Copyright © 2012 IM Group. All rights reserved
  • 34. Access to a Redefined Base Function • When a base class function is redefined in a derived class, the base class function can still be used ▫ To specify that you want to use the base class version of the redefined function: HourlyEmployee sally_h; sally_h.Employee::print_check( ); Copyright © 2012 IM Group. All rights reserved
  • 35. Inheritance Details • Some special functions are, for all practical purposes, not inherited by a derived class ▫ Some of the special functions that are not effectively inherited by a derived class include  Destructors  Copy constructors  The assignment operator Copyright © 2012 IM Group. All rights reserved
  • 36. Copyright © 2012 IM Group. All rights reserved Polymorphism Object Oriented Programming
  • 37. Polymorphism • Polymorphism refers to the ability to associate multiple meanings with one function name using a mechanism called late binding • Polymorphism is a key component of the philosophy of object oriented programming Copyright © 2012 IM Group. All rights reserved
  • 38. A Late Binding Example • Imagine a graphics program with several types of figures ▫ Each figure may be an object of a different class, such as a circle, oval, rectangle, etc. ▫ Each is a descendant of a class Figure ▫ Each has a function draw( ) implemented with code specific to each shape ▫ Class Figure has functions common to all figures Copyright © 2012 IM Group. All rights reserved
  • 39. A Problem • Suppose that class Figure has a function center ▫ Function center moves a figure to the center of the screen by erasing the figure and redrawing it in the center of the screen ▫ Function center is inherited by each of the derived classes  Function center uses each derived object's draw function to draw the figure  The Figure class does not know about its derived classes, so it cannot know how to draw each figure Copyright © 2012 IM Group. All rights reserved
  • 40. Virtual Functions • Because the Figure class includes a method to draw figures, but the Figure class cannot know how to draw the figures, virtual functions are used • Making a function virtual tells the compiler that you don't know how the function is implemented and to wait until the function is used in a program, then get the implementation from the object. ▫ This is called late binding Copyright © 2012 IM Group. All rights reserved
  • 41. Virtual Details • To define a function differently in a derived class and to make it virtual ▫ Add keyword virtual to the function declaration in the base class ▫ virtual is not needed for the function declaration in the derived class, but is often included ▫ virtual is not added to the function definition Copyright © 2012 IM Group. All rights reserved
  • 42. Overriding • Virtual functions whose definitions are changed in a derived class are said to be overridden • Non-virtual functions whose definitions are changed in a derived class are redefined Copyright © 2012 IM Group. All rights reserved
  • 43. Any Questions Session 5 Copyright © 2012 IM Group. All rights reserved