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

Inheritance Oop

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

Inheritance Oop

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

SCHOOL OF COMPUTER SCIENCE AND

ENGINEERING
FACULTY: AVINASH TASKAR SIR
SUBJECT: OBJECT ORIENTED PROGRAMMING

DIVISION : C

PREPARED BY:KRISHNA KANSARA - 230105131304


Inheritance in
C++ Object-
Oriented
Programming
Inheritance is a fundamental concept in object-oriented
programming (OOP) that allows you to create new classes (derived
classes) based on existing classes (base classes). It's like building
upon a foundation. The derived class inherits properties and
behaviors from the base class, creating a hierarchy of classes. This
principle of code reusability and extension is a core strength of
OOP in C++.
Definition of Inheritance
Inheritance in C++ allows a derived class to inherit members (data members
and member functions) from a base class. The derived class is often referred to
as a subclass or child class, while the base class is called the superclass or
parent class. Think of it like a family tree where the derived class inherits traits
from its ancestor. This relationship establishes a clear connection between
classes, promoting code organization and reducing redundancy.

1 Reusability 2 Code Organization


Inheritance promotes code It helps you structure your code
reuse, as you can extend logically by establishing clear
existing classes rather than relationships between classes,
writing everything from scratch. making your program easier to
This saves time and effort. understand and maintain.

3 Polymorphism
Inheritance is a key enabler for polymorphism, the ability of objects of
different classes to respond to the same message (function call) in
different ways. This is a powerful technique for writing flexible and
adaptable code.
Types of Inheritance
C++ supports different types of inheritance, allowing for varying levels of complexity and flexibility. Each type has
its own advantages and considerations:

Single Inheritance Multiple Inheritance Multilevel Inheritance

A single derived class inherits from A derived class inherits from A derived class inherits from
only one base class. This is the multiple base classes. This allows another derived class. This creates
most basic type of inheritance. for more complex relationships and a chain of inheritance where a
can be used to model real-world class can inherit properties from
scenarios where an object can multiple levels of ancestry.
have multiple roles or
characteristics.
Advantages of Inheritance
Inheritance offers several advantages that make it a powerful tool for designing robust and well-structured object-oriented programs.

Code Reusability Extensibility


Inheritance promotes code reuse by allowing you to build upon existing You can easily extend the functionality of existing classes by creating
code, reducing the need to write everything from scratch. This saves derived classes that add new features or modify existing ones. This
time, effort, and reduces the risk of errors. allows you to adapt your code to changing requirements without
altering the original class.

Code Organization Polymorphism


Inheritance helps structure your code logically by grouping related Inheritance facilitates polymorphism, a fundamental aspect of OOP that
classes together in a hierarchy. This makes your code easier to allows objects of different classes to respond to the same message in
understand, maintain, and debug. different ways. This is essential for creating adaptable and flexible code
that can handle different situations effectively.
Implementing Inheritance in C++
Implementing inheritance in C++ is straightforward. You use the colon (:)
followed by the keyword "public," "protected," or "private" to specify the
access specifier for the inheritance. This determines how members of the
base class are accessible in the derived class.

class BaseClass {
// ... members
};

class DerivedClass : public BaseClass {


// ... members
};

The "public" access specifier is the most common, as it allows all public
members of the base class to be accessible in the derived class.
Base Class and Derived Class
Understanding the distinction between base classes and derived
classes is crucial for understanding inheritance. The base class is the
parent class, and the derived class is the child class that inherits from
it.
Base Class Derived Class

The foundation or the starting Extends the base class. It


point. It defines the core inherits members (data
characteristics and behaviors members and member
that are inherited by derived functions) from the base class
classes. and can add new members or
modify existing ones.

Example: A "Vehicle" class Example: A "Car" class could


could be a base class. be a derived class that inherits
from the "Vehicle" class and
adds specific car features.
Access Specifiers in Inheritan
Access specifiers control how members of a class are accessed from other
classes. They play a critical role in inheritance:

Public
Public members of the base class remain public in the derived
class. They are accessible from anywhere.

Protected
Protected members of the base class are accessible only
within the base class and its derived classes. They are not
accessible directly from outside the class hierarchy.

Private
Private members of the base class are completely hidden from
the derived class. They cannot be accessed by the derived
class, ensuring data encapsulation.
Constructors and
Destructors in Inherited
Classes
Constructors and destructors are special member functions that are
automatically invoked when an object is created or destroyed,
respectively. Their behavior in inherited classes is important to
understand.

1 Constructors 2 Destructors
The constructor of the Destructors are called in
derived class calls the reverse order of
constructor of the base construction, meaning the
class to initialize the destructor of the derived
inherited members. You class is called first,
can explicitly call the followed by the destructor
base class constructor of the base class.
using the "base class
name" syntax.
Overriding Member
Functions in Derived Classes
Overriding member functions allows you to provide a specific implementation for
a member function inherited from the base class. This is a powerful technique for
adapting behavior in derived classes.

1 Function Signature 2 Polymorphism


The overridden function in the Overriding is crucial for
derived class must have the achieving polymorphism. When
same name, return type, and you call a function on a derived
parameters as the function in class object, the appropriate
the base class. overridden version of the
function will be executed.

3 Virtual Functions
To ensure that the correct overridden function is called at runtime, use the
"virtual" keyword in the base class declaration. This allows for dynamic
binding, where the function to be called is determined at runtime based
on the actual type of the object.
Conclusion and Best Practices
Inheritance is a powerful tool in C++ for creating reusable and extensible code. By
understanding the different types of inheritance, access specifiers, and the role of
constructors and destructors, you can design well-structured and adaptable object-oriented
programs.

Use Inheritance Judiciously Favor Composition Over Inheritance


Only use inheritance when there's a clear In some cases, using composition (having
"is-a" relationship between classes. one class contain an instance of another
Inheritance should reflect real-world class) can be a better alternative to
relationships to maintain code clarity. inheritance, especially for complex
relationships.

Protect Base Class Data Use Virtual Functions for Polymorphism


Use the "protected" access specifier for Use virtual functions to achieve dynamic
members that need to be accessible within binding and ensure the correct overridden
the class hierarchy but not outside of it. function is called at runtime.
THANK YOU

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