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

cpp unit 3

Inheritance is a key concept in object-oriented programming that allows a derived class to inherit properties and behaviors from a base class, promoting code reuse and organization. There are several types of inheritance including single, multiple, multilevel, hierarchical, and hybrid inheritance, each defining different relationships between classes. Additionally, memory management is crucial in OOP, involving allocation and deallocation of memory using operators like new and delete, along with error handling for memory allocation failures.

Uploaded by

kunalkumbhar0985
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

cpp unit 3

Inheritance is a key concept in object-oriented programming that allows a derived class to inherit properties and behaviors from a base class, promoting code reuse and organization. There are several types of inheritance including single, multiple, multilevel, hierarchical, and hybrid inheritance, each defining different relationships between classes. Additionally, memory management is crucial in OOP, involving allocation and deallocation of memory using operators like new and delete, along with error handling for memory allocation failures.

Uploaded by

kunalkumbhar0985
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT 3

INHERITANCE
Definition:
Inheritance is a fundamental concept in object-oriented programming
(OOP) where a new class (derived class) is created by extending an
existing class (base class).
It allows the derived class to inherit properties and behaviors (member
variables and member functions) from the base class, promoting code
reuse and hierarchical organization of classes
Concept of Inheritance

Inheritance is a fundamental concept in object-oriented programming


(OOP) that allows a new class, referred to as the derived class, to inherit
properties and behaviors from an existing class, known as the base
class. This mechanism promotes code reuse, extensibility, and
organization by allowing the derived class to inherit the attributes and
methods of the base class.
Key points about the concept of inheritance include:
Base Class and Derived Class:
Inheritance involves the creation of at least two classes: a base class
and a derived class.
The base class contains common attributes and behaviors that are
shared among multiple derived classes.
The derived class inherits these attributes and behaviors from the base
class and may also have its own unique attributes and behaviors.
Types of Inheritance:
 Single Inheritance: A derived class inherits from only one base
class.
 Multiple Inheritance: A derived class inherits from more than one
base class.
 Multilevel Inheritance: A derived class is derived from another
derived class.
 Hierarchical Inheritance: Multiple derived classes inherit from a
single base class.
Access Specifiers:
Inheritance can involve different access specifiers (public, protected, or
private), which control the visibility of inherited members in the
derived class.
 public: Inherited members retain their original access level in the
derived class.
 protected: Inherited members become protected members in the
derived class.
 private: Inherited members become private members in the
derived class.
Types of Inheritance – Single, Multiple, Hierarchical, Multilevel,
Hybrid Inheritance
Single Inheritance:
 In single inheritance, a class inherits properties and behaviors
from a single base class.
 This is the most common form of inheritance.
Example:
Multiple Inheritance:
 In multiple inheritance, a class inherits properties and behaviors
from more than one base class.
 This allows a derived class to have multiple parent classes.
Example:

Hierarchical Inheritance:
 In hierarchical inheritance, multiple derived classes inherit from
a single base class.
 This creates a hierarchical structure where multiple classes share
common properties and behaviors.
Example:
Multilevel Inheritance:
 In multilevel inheritance, a derived class inherits properties and
behaviors from a base class, and then another class derives from
that derived class.
 This creates a chain of inheritance.
Example:

Hybrid Inheritance:
Hybrid inheritance combines different types of inheritance, such as
single, multiple, hierarchical, and multilevel inheritance.
This allows for more complex class relationships.
Example:
Dynamic Memory Allocation:
 The new operator allocates memory for a single object or an
array of objects of a specified type.
 It returns a pointer to the allocated memory.
Syntax:
 To allocate memory for a single object, use new followed by the
data type:

 To allocate memory for an array of objects, specify the number


of elements inside square brackets:

Initialization:
 The new operator also allows for initialization of allocated
memory using parentheses:

Memory Deallocation:
 Memory allocated using the new operator must be explicitly
deallocated using the delete operator to avoid memory leaks.
For single objects:

For ARRAY objects:

Error Handling:
 It's essential to handle the scenario where memory allocation
fails.If memory allocation fails, the new operator throws a
std::bad_alloc exception.
 To prevent the program from terminating unexpectedly, you can
use exception handling or check for nullptr after memory
allocation.

You might also like