Professional Documents
Culture Documents
10 Inheritance
10 Inheritance
• Syntax:
class DerivedClassName : access-level BaseClassName
where
• access-level specifies the type of derivation
• private by default, or
• public
• protected
• Any class can serve as a base class
• Thus a derived class can also be a base class
5
Access Rights of Derived Classes
• Modes of Inheritance:
• The mode of inheritance defines the access level for the members of derived
class that are inherited from the base class
Modes of Inheritance
private protected public
for Members
Access Control
private - - -
protected private protected protected
public private protected public
Constructor Rules for Derived Classes
The default constructor and the destructor of the base class are always called
when a new object of a derived class is created or destroyed.
output: A:default
B test(1); 7
B
Types of Inheritance
• Single Inheritance:
• A class inheriting from only one class
is called Single Inheritance
• Multiple Inheritance:
• A class can inherit from more than
one classes is called multiple
Inheritance
Types of Inheritance Continues…
• Multi-level Inheritance:
• A class is inherited from another
already derived(inherited) class
• Hybrid Inheritance:
• Combining more than one type of
inheritance is called Hybrid
Inheritance