Inheritance Oop
Inheritance Oop
ENGINEERING
FACULTY: AVINASH TASKAR SIR
SUBJECT: OBJECT ORIENTED PROGRAMMING
DIVISION : C
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:
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.
class 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
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.
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.