0% found this document useful (0 votes)
17 views

Inheritance 2

Uploaded by

gayathris3884
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)
17 views

Inheritance 2

Uploaded by

gayathris3884
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/ 31

Inheritance

Inheritance is one of the pillars of Object-Oriented


Programming (OOPs). It is a feature that enables a
class to acquire properties and characteristics of
another class.

• Inheritance is a relationship
• The class being inherited from is called the parent class, base class, or
superclass, and the class doing the inheriting is called the child class,
derived class, or subclass.
Inheritance
• A child class inherits both behaviors (member functions) and properties
(member variables) from the parent
• During inheritance, the data members of the base class get copied in the
derived class and can be accessed depending upon the visibility mode
used. The order of the accessibility is always in a decreasing order i.e.,
from public to protected.
• Inheritance allows programmers to create new classes built upon existing
ones, enabling code reuse and efficient software development.
Inheritance:is-a relationship

• A apple is a fruit
• A Banana is a fruit
• Triangle is a shape
• A car is a vehicle.
• Orange is a fruit.
• A surgeon is a doctor.
• A dog is an animal.
Syntax
Example
Access Specifiers in inheritance
• Private members, protected members are inaccessible outside of
the class.
• Private members of the base class cannot be used by the derived
class unless friend declarations within the base class explicitly
grant access to them.
• The protected members can be accessed within the class and
from the derived class.
Access Specifiers in inheritance
• The base class will contain data members and methods that are
common to all of the derived classes.
• Public members are accessible by all functions in the program
• Private members are accessible only by member functions and
friends of the base class.
• Protected members of a base class are accessible by members and
friends of the base class AND by members and friends of the derived
classes
C++ protected Access Specifier
Example: Area of Rectangle using inheritance
• Class definition
• Derived Class definition
• Main function
(i) Class Definition
(ii) Derived Class definition
Types of Inheritance

• Single Inheritance
• Multilevel Inheritance
• Multiple Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
Single Inheritance

• When the derived class inherits only one base class, it is known
as Single Inheritance.
Example of Single Inheritance:
Example of Single Inheritance:
• Base is the class name and the parent class, which contains the
property named salary and the value 900.
• Another class named Derived, which is the child class, which inherits
the property of the parent class and has its property named as a bonus
which contains the value of 100.
• In the child class, there is a function named sum(), which is used to add
the salary and bonus. In the main function, an object is created named
“x” of the “Derived” class which is a child class, and using that object,
the properties, and the sum function are called from the derived class,
which will add the salary and bonus and gives it as output.
Multiple Inheritance in C++

• When a derived class(child/sub class) inherits more than one base


class(parent/super class), it is called multiple inheritance.
Example of Multiple Inheritance:
Example of Multiple Inheritance:
Multilevel Inheritance
• When a derived(child) class inherits the base class and acts as the base
class(parent class) to the other class, it is called Multilevel Inheritance.
There can be any number of levels i.e any number of derived classes in
multilevel inheritance.
Hierarchical Inheritance in C++:
When more than one class is inherited from a single base class, it is
called Hierarchical Inheritance.
Hybrid (Multipath) Inheritance in C++:
• It is a combination of one or more types of inheritance.
Real Life Examples
Hybrid Inheritance in C++:
Advantages of Inheritance in C++

• Inheritance in C++ promotes Code reusability. When a derived


class inherits the base class, then the derived class can access all
the functionality, and the base class's code can be reused in the
derived class.
• It improves code readability as you don’t have to rewrite the
same code repeatedly; hence, the code looks cleaner and
readable.
• It saves time and effort as the code is already written and is
inherited; therefore, it saves time to write code again.
• Inheritance supports extensibility as new classes can be easily
added to existing classes.
• https://www.programiz.com/cpp-programming/inheritance

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy