1) Explain About Inheritance? Explain The Types of Inheritance?
1) Explain About Inheritance? Explain The Types of 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, you 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.
Advantage:
✓ Code reusability: Now you can reuse the members of your parent class. So, there is no
need to define the member again. So less code is required in the class.
✓ It increases the code reliability by definite body to the program.
Types of Inheritance:
• Single inheritance
• Multiple inheritance
• Hierarchical
• Multilevel
• Hybrid inheritance
The colon indicates that the a-class name is derived from the base class name. The access
specifier or the visibility mode is optional and, if present, may be public, private or
protected. By default, it is private. Visibility mode describes the status of derived features.
❖ 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.
//body of subclass
};
o Public: When the member is declared as public, it is accessible to all the functions of
the program.
o Private: When the member is declared as private, it is accessible within the class only.
o Protected: When the member is declared as protected, it is accessible within its own
class as well as the class immediately derived from it.
Multiple Inheritance: Multiple inheritance is the process of deriving a new class that
inherits the attributes from two or more classes.