Inheritance in C++
Inheritance in C++
Access Modifiers
• One of the main features of object-oriented
programming languages such as C++ is data
hiding.
• Data hiding refers to restricting access to data
members of a class.
• This is to prevent other functions and classes
from tampering with the class data.
Access Modifiers
• However, it is also important to make some
member functions and member data
accessible so that the hidden data can be
manipulated indirectly.
• The access modifiers of C++ allows us to
determine which class members are accessible
to other classes and functions, and which are
not.
Access Modifiers Illustration
• Here, the variables
patientNumber and
diagnosis of the
Patient class are hidden
using the private
keyword, while the
member functions are
made accessible using
the public keyword.
Types of Access Modifiers
1. Discuss 3 types of access modifiers. In addition, illustrate
your with examples,
2. Explain 3 types of member visibilities/access modifiers
• .
Types of inheritance
1. What is inheritance
2. Importance/Use/Benefits of Inheritance in OOP
3. Advantages of inheritance
• Single Inheritance
• Multilevel Inheritance
• Multiple Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
Single Inheritance
• In this type of inheritance one derived class
inherits from only one base class.
• It is the most simplest form of Inheritance.
Single Inheritance Syntax
• .
Single Inheritance Example
• .
Single Inheritance Example
• .
Single Inheritance
• The derived class inherits the features from
the base class and can have additional
features of its own. For example,
Single Inheritance(Animal and Dog)
• Here, the Dog class is derived from the Animal
class.
• Since Dog is derived from Animal, members of
Animal are accessible to Dog.
Single Inheritance(Animal and Dog)
is-a relationship
• Dog is-a animal relationship
– A car is a vehicle.
– Orange is a fruit.
– A surgeon is a doctor.
– A dog is an animal.
Single Inheritance(Animal and Dog)
Single Inheritance(Animal and Dog)
PRIVATE access
C++ protected Members
• The access modifier protected is especially
relevant when it comes to C++ inheritance.
• Like private members, protected members are
inaccessible outside of the class.
• However, they can be accessed by derived
classes and friend classes/functions.
• We need protected members if we want to hide
the data of a class, but still want that data to be
inherited by its derived classes.
C++ protected Members
C++ protected Members
• int addNumber() { }
• int addNumber(int a) { }
• int addNumber(int a, float b) { }
• int addNumber(double a, int b) { }
Polymorphism
• Polymorphism (a Greek word meaning having
multiple forms) is the ability of an entity such
as a function or a message to be processed in
more than one form.
• It can also be defined as the property of an
object belonging to a same or different class
to respond to the same message or function in
a different way.
Polymorphism
• Polymorphism is refers to the property by
which objects belonging to different classes
are able to respond to the same message but
in different forms.
• Real life example of polymorphism, a person
at a same time can have different
characteristic.
• Like a man at a same time is a father, a
husband, a employee. So a same person
posses have different behavior in different
situations. This is called polymorphism.
Types of Polymorphism
• In C++ polymorphism is mainly divided into
two types:
– Compile time Polymorphism
– Runtime Polymorphism
Types of Polymorphism
• In C++, polymorphism can be achieved either
at compile-time or at run-time.
• At compile-time, polymorphism is
implemented using operator overloading and
function overloading.
• However, at run-time, it is implemented using
virtual functions
Operator Overloading
• Operator overloading is the process that enables
an operator to exhibit different behavior,
depending on the data provided.
• An operator is a symbol that tells the compiler to
perform specific task. Every operator have their
own functionality to work with built-in data
types.
• For example, when the '+' operator is used with
two numbers, it adds the two numbers and
produces the sum.
• However, if it is used with two strings, it
concatenates the two strings and produces the
third concatenated string
Operator Overloading
Function Overloading
• When there are multiple functions with same
name but different parameters then these
functions are said to be overloaded.
• Functions can be overloaded by change in
number of arguments or/and change in type
of arguments.
• The compiler determines which function to
invoke during compile time based on the
number and types of argument sent.
Function Overloading
Examples of Function overloading
int addNumber() { }
int addNumber(int a) { }
int addNumber(int a, float b) { }
int addNumber(double a, int b) { }
Function Overloading Example
Function Overloading Example
Function Overloading Example
• In the above example, area() function is
overloaded three times. If 1 argument is
passed, the area() function with one argument
is called and returns area of circle.
• Similarly, if 2 and 3 arguments are passed,
the area() function with two arguments and
three arguments is called respectively.
• If two or more functions with same name and
same and types of argument is created then
error will occur as the compiler can't separate
these functions.
Runtime polymorphism
• This type of polymorphism is achieved by
Function Overriding.
• Function overriding on the other hand 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.
Runtime polymorphism example
Runtime polymorphism example
Function overriding
• Giving new implementation of base class
method into derived class and the calling of
this new implemented function with derived
class's object is called function overriding.
• Giving new implementation of derived class
method into base class and the calling of this
new implemented function with base class's
object is done by making base class function as
virtual function.
C++ Virtual Function
• A virtual function is a special form of member
function that is declared within a base class
and redefined by a derived class. OR
• A virtual function is a member function in
base class that you expect to redefine in
derived classes.
• It is declare using the virtual keyword.
C++ Virtual Function
• Virtual function is used in situation, when we
need to invoke derived class function using
base class pointer.
• We must declare base class function as virtual
using virtual keyword preceding its normal
declaration.
C++ Virtual Function
• The base class object must be of pointer type
so that we can dynamically replace the address
of base class function with derived class
function.
• This is how we can achieve "Runtime
Polymorphism".
• If we don't use virtual keyword in base class,
base class pointer will always execute function
defined in base class.
Example of virtual function
Example of virtual function
Example of virtual function
C++ Virtual Function