C++ Inheritance

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

C++ Inheritance

C++ Inheritance
 In C++, inheritance is a process in which one object
acquires all the properties and behaviors of its
parent object automatically.
 In such way, we can reuse, extend or modify the
attributes and behaviors which are defined in other
class.
 In C++, the class which inherits the members of
another class is called derived class and the class
whose members are inherited is called base class.
 The derived class is the specialized class for the
base class.
Advantages of Inheritance in C++
1. Code Reusability – Inheritance in C++ allows you to
create new classes that are based on existing classes,
so you can reuse existing code and avoid rewriting the
same functionality. This can save a lot of time and
effort when you’re creating new programs.

2. Organization – Inheritance in C++ helps you organize


your code in a logical and structured way. By creating
classes that inherit from other classes, you can create
a hierarchy of related classes that makes it easy to
understand how your code works and how different
parts of your program are related
Advantages of Inheritance in C++
3. Polymorphism – Inheritance in C++ allows you to
create objects that can take on different forms,
depending on the class they inherit from. This is called
polymorphism, and it can make your code more
flexible and adaptable to changing requirements.
4. Abstraction – Inheritance in C++ allows you to
create abstract classes that define the basic behavior
of a group of related classes, while leaving the
implementation details to the derived classes. This is
called abstraction, and it can make your code more
modular and easier to understand.
Disadvantages of Inheritance in C++
1. Complexity – Inheritance in C++ can make your code more
complex, especially when you have multiple levels of
inheritance or when you’re inheriting from classes that have a
lot of methods and properties. This can make it harder to
understand how your code works and can lead to bugs and
other issues.
2. Dependency – Inheritance in C++ can create dependencies
between classes, which can make it harder to reuse your code
in other programs or to make changes to your code without
affecting other parts of your program.
3. Performance – Inheritance in C++ can impact the performance
of your code, as it requires additional memory and processing
power to maintain the relationship between classes. This can
slow down your program and make it less efficient.
Types Of Inheritance
Types Of Inheritance
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance
C++ Single Inheritance
• Single inheritance is defined as
the inheritance in which a
derived class is inherited from
the only one base class.

• Where 'A' is the base class, and


'B' is the derived class.
Syntax single inheritance in c++
class A
{

/////////
};
class B: visibility_mode A
{

///////////
}
C++ Multilevel Inheritance
• Multilevel inheritance is a
process of deriving a class from
another derived class.
• one class inherits another class
which is further inherited by
another class, it is known as multi
level inheritance in C++.
• Inheritance is transitive so the last
derived class acquires all the
members of all its base classes.
Syntax
class A
{

};
class B: visibility_mode A
{

};
class C: visibility_mode B
{

};
C++ Multiple Inheritance
• Multiple inheritance is the process of deriving a
new class that inherits the attributes from two
or more classes.
Syntax
class A
{

};
class B
{

};
class C: visibility_mode_1 A, visibility_mode_2 B
{

};
C++ Hierarchical Inheritance
• Hierarchical inheritance is defined as the
process of deriving more than one class from a
base class.
Syntax
class A
{

};
class B: visibility_mode A
{

};
class C: visibility_mode A
{

};
class D: visibility_mode A
{

};
C++ Hybrid Inheritance
• Hybrid inheritance is a combination of more
than one type of inheritance.
Syntax
class A
{

};
class B: visibility_mode A
{

};
class C: visibility_mode A
{

};
class D: visibility_mode B, visibility_mode C
{

};

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