0% found this document useful (0 votes)
5 views87 pages

Inheritance in C++

The document discusses inheritance in C++, focusing on access modifiers (public, private, protected) that control data access within classes. It outlines various types of inheritance, including single, multilevel, multiple, hierarchical, and hybrid inheritance, explaining their characteristics and examples. Additionally, it covers concepts of function overloading and polymorphism, detailing compile-time and runtime polymorphism, along with the use of virtual functions.

Uploaded by

dhalhadopeabdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views87 pages

Inheritance in C++

The document discusses inheritance in C++, focusing on access modifiers (public, private, protected) that control data access within classes. It outlines various types of inheritance, including single, multilevel, multiple, hierarchical, and hybrid inheritance, explaining their characteristics and examples. Additionally, it covers concepts of function overloading and polymorphism, detailing compile-time and runtime polymorphism, along with the use of virtual functions.

Uploaded by

dhalhadopeabdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

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

• In C++, there are 3 access modifiers:


–public
–private
–protected
public Access Modifier
• The public keyword is used to create public
members (data and functions).
• The public members are accessible from any
part of the program.
Example 1: C++ public Access Modifier
• In main(), we have
created an object of the
Sample class named obj1.
We then access the public
elements directly by using
the codes obj1.age and
obj1.displayAge().

• Notice that the public


elements are accessible
from main(). This is
because public elements
are accessible from all
parts of the program.
private Access Modifier
• The private keyword is used to create private
members (data and functions).
• The private members can only be accessed
from within the class.
• However, friend classes and friend functions
can access private members.
Example: C++ private Access Specifier
• In main(), the object
obj1 cannot directly
access the class
variable age
// error
cin >> obj1.age;
• We can only indirectly
manipulate age through
the public function
displayAge(), since
this function initializes
age with the value of the
argument passed to it
i.e. the function
parameter int a.
protected Access Modifier
• The protected keyword is used to create
protected members (data and function).
• The protected members can be accessed
within the class and from the derived class.
Example: C++ protected Access Specifier
• Here, SampleChild is an
inherited class that is
derived from Sample. The
variable age is declared in
Sample with the protected
keyword.
• This means that SampleChild
can access age since Sample is
its parent class.
• We see this as we have
assigned the value of age in
SampleChild even though age
is declared in the Sample
class.
Summary: public, private, and protected

• public elements can be accessed by all other


classes and functions.
• private elements cannot be accessed outside

the class in which they are declared, except by


friend classes and functions.
• protected elements are just like the private,

except they can be accessed by derived


classes.
Summary: public, private, and protected

• .
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

• Here, the variable type is protected and is


thus accessible from the derived class Dog.
• We can see this as we have initialized type in
the Dog class using the function setType().
• On the other hand, the private variable color
cannot be initialized in Dog.
Multilevel Inheritance in C++ Programming
• When a class is derived from a class which is
also derived from another class, i.e. a class
having more than one parent classes, such
inheritance is called Multilevel Inheritance.
• The level of inheritance can be extended to
any number of level depending upon the
relation.
• Multilevel inheritance is similar to relation
between grandfather, father and child.
Multilevel Inheritance in C++ Programming
Multilevel Inheritance in C++ Programming
Multilevel Inheritance in C++ Programming
Multilevel Inheritance in C++ Option 2
Multilevel Inheritance in C++ Programming
Multilevel Inheritance in C++ Programming
Multilevel Inheritance in C++ Programming
• This program is an example of multiple
inheritance.
• Here, programmer class is derived from
employee which is derived from person.
• Each class has required attributes and methods.
• The public features of person is inherited by
employee and the public features of employee is
inherited by programmer.
• The method getdata() asks user to input data,
while display() displays the data.
Multiple Inheritance in C++ Programming
• In this type of inheritance a single derived class
may inherit from two or more than two base
classes.
• When a class is derived from two or more base
classes, such inheritance is called Multiple
Inheritance.
• It allow us to combine the features of several
existing classes into a single class.
• For example,
– Petrol is derived from both liquid and fuel.
– A child has character of both his/her father and
mother, etc
Multiple Inheritance in C++ Programming
Multiple Inheritance in C++ Programming
Multiple
Inheritance in
C++
Programming
Multiple Inheritance Example
Multiple Inheritance Example
C++ Hierarchical Inheritance

• If more than one class is inherited from the


base(same) class, it's known as hierarchical
inheritance.
• In hierarchical inheritance, all features that are
common in child classes are included in the
base class.
– For example: Physics, Chemistry, Biology are
derived from Science class.
C++ Hierarchical Inheritance
C++ Hierarchical Inheritance
C++ Hierarchical Inheritance
C++ Hierarchical Inheritance
Hierarchical Inheritance Example
Hierarchical Inheritance Example
Hierarchical Inheritance Example
• In this example, there is only one base class A
from which two class B and C are derived.
• Both derived class have their own members as
well as base class members.
• The product is calculated in the derived class
B, whereas, the sum is calculated in the
derived class C but both use the values of x
and y from the base class.
C++ Hierarchical Inheritance Example
C++ Hierarchical Inheritance Example
C++ Hierarchical Inheritance Example
Hybrid Inheritance
• This is combination of more than one
inheritance. Hence, it may be a combination
of Multilevel and Multiple inheritance or
Hierarchical and Multilevel inheritance or
Hierarchical and Multipath inheritance or
Hierarchical, Multilevel and Multiple
inheritance.
• Since .NET Languages like C#, F# , Java etc.
does not support multiple and multipath
inheritance.
Hybrid Inheritance
• Hybrid Inheritance is combination of
Hierarchical and Mutilevel Inheritance.
Hybrid Inheritance
• Hybrid Inheritance is combination of
Hierarchical and Mutilevel Inheritance.
Hybrid Inheritance Syntax
Hybrid Inheritance example
Hybrid Inheritance example
Hybrid Inheritance(Example)
Hybrid Inheritance(Example)
Hybrid Inheritance(Example)
Function Overloading in C++
Programming
• If more than one functions having same name
but differing in terms of number and types of
argument it takes is used in a class, it is known
as function overloading.
• It is an example of compile time
polymorphism (static/early binding).
• The compiler determines which function is
called during compile time based on the
number and types of argument sent.
Examples of Function overloading

• 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

• It is used to tell the compiler to


perform dynamic linkage or late binding on
the function.

You might also like