Inheritance is one of the most useful and essential characteristics of an object-oriented programming language. Inheritance allows the programmer to utilize the previously defined classes with newer ones. The new class is assembled using the properties of the existing classes.
The procedure of developing a new class from an old class is termed inheritance.
The new class is termed a derived class, and the old class is called a base class.
Single inheritance: When a new class is derived from only one base class, such a type of inheritance is called a single inheritance.
Multilevel inheritance: The procedure of deriving a class from a derived class is called multilevel inheritance.
Multiple inheritance or hierarchical inheritance: When a class is derived from more than one class, this type of inheritance is called multiple inheritance or hierarchical inheritance.
Hybrid inheritance:When a class is derived from another one or more base classes, this process is termed hybrid inheritance.
When classes are declared virtual, the compiler takes essential caution to avoid duplication of member variables. Thus, we make a class virtual if it is a base class that has been used by more than one derived class as their base class.
Execution of constructors in inheritance: The execution of constructors takes place from base class to derived class.
Execution of destructors in inheritance: The execution of destructors is in opposite order as compared with constructors, that is, from derived class to base class.
Public:If member variables of a class are public, any function can access them. In C++, the members of a struct and union are by default public.
Private: If member variables of a class are private, member functions and friends can only access them, declared in a similar class. The members of a class are by default private.
Protected: If member variables of a class are protected, its scope is similar to that for private functions. In addition, member functions and friends can use the member classes derived from the declared class, but only in objects of the derived type.
When a class is not used for creating objects, it is called an abstract class.
The constructor of the derived class works for its base class; such constructors are called common constructors.
The derived class can have a similar function name as the base class member function. An object of the derived class invokes a member function of the derived class even if a similar function is preset in the base class.
Properties of one class can be used in another class using inheritance or using the object of a class as a member in another class. Declaring the object as a class data member in another class is also known as delegation. When a class has an object of another class as its member, such a class is known as a container class.
Test Your Skills
Test Your Skills Now!
This is only a practice test, it is designed to help you revise your concepts
The test contains questions, only 1 option is correct for each question
This is a timed test.
After you have finished the test, press on the 'Finish Test' button to know your score and get the correct answers