You are on page 1of 36

Inheritance and Polymorphism

Programming Process:
Algorithm and Flowcharts
Inheritance
• A method in OOP that allows a programmer to create a class derived
from an existing class.
• The derived class inherits the characteristics of the base class.
Example
Explanation
• The Dog class is derived from the Animal class.
• Since Dog derived from Animal , members of the base class is
accessible to Dog.
eat()
sleep()
Illustration
is-a relationship
• Use inheritance only if there is an is-a relationship is present between
the two classes.
• Example:
1. A cat is an animal.
2. A plane is a vehicle.
3. Pasta is a food.
4. Surgeon is a doctor.
Example code 1 (Inheritance)
Output
Explanation of 1st Example
• dog1 (object from derived from the class Dog) accessed the members
of the base class Animal.
dog1.eat();
dog1.sleep();
C++ Protected Members
• Access modifier protected is relevant when it comes to C++
inheritance.
• Members of the class under the access specifier protected is
inaccessible outside of the class. But they can be accessed by derived
classes.
• Need the protected access specifier if we want to hide data of a class
but still want them to be inherited by derived classes.
Example 2
Output
Explanation of 2nd Example
• The variable type is protected and can be accessed from the derived
class Dog.
• Initialized type in the derived class Dog using the function setType().
• While the variable color has a private access specifier that can only be
accessed in the class Animal (base class).
Access Modes in C++ Inheritance
• Also private and protected access specifiers can be used in inherited
classes.
Example:
Access Modes
• public- If a derived class is declared in public mode, then the
members of the base class are inherited by the derived class as they
are.
• private- all the members of the base class become private members
in the derived class.
• protected- The public members of the base class become protected
members in the derived class.
Example 3 (Public Inheritance)
Output
Explanation
• PublicDerived is derived from base class Base in public mode.
• As a result, in Public Derived:

Since private and protected members are not accessible from the
main(), we need to create public functions getPVT() and getProt() to
access them.
Example 4 (Protected Inheritance)
Output
Explanation
Example 5 (Private Inheritance)
Output
Explanation
Polymorphism
• Means many forms
• Occurs when many classes that are related to each other by
inheritance.
• Inheritance inherit attributes and methods from another class;
Polymorphism uses those methods to perform different tasks. Allows
to perform a single action in different ways.
Example
Using the class in an OOP structure
Output
Types of Polymorphism
Function Overloading
Output
Operator Overloading
Output
Run Time Polymorphism
• Function Overriding - Function Overriding occurs when a derived class
has a definition for one of the member functions of the base class.
That base function is said to be overridden.
Example
Output

You might also like