C++ inheritance mcq with answers and explanation. 50% of the objective questions are asked in interviews and rest are to cover C++ inheritance concepts.
Q)base class and derived class relationship comes under
Answer : A
Explanation: Base class and derived class come under inheritance, one of the C++ oops principle. Also, it is known as parent child relationship.
Q) C++ Inheritance relationship is
Explanation: IS A Relationship is related to inheritance in C++.
Lets consider a relation ship between classes i.e. Dog IS A Animal, Apple IS A Fruit, Car IS A Vehicle etc. Here, IS A relationship is valid, but, if we say Animal IS A Dog, then it is wrong. So, Animal class should be the base class and Dog class will be the derived class.
Q) Types of inheritance in C++ are
- All the above
Explanation: All are types of inheritance relationship in C++ oops.
Multilevel Inheritance: When a class is derived from a class which is also derived from another class.
Multiple Inheritance: A class inherits multiple class. or say, A class has more than one base class.
Hierarchical Inheritance: When multiple classes derived from a same base class.
Q) Inheritance allow in C++ Program?
- Class Re-usability
- Creating a hierarchy of classes
Explanation: Advantage of inheritance are like re-usability- You can re-use existing class in a new class that avoid re-writing same code and efforts.
We can make an application easily extensible.
Q) Functions that can be inherited from base class in C++ program
- Static function
In C++, constructor and destruction of a class cannot be inherited to derived class. However, when we create the object of derived class then constructor of the base class is called automatically. You can read order of execution of constructors and destructors call in c++ inheritance.
Static function is not the part of an object, Hence, it cannot be inherited in derived classes. Note that all the class member functions that are the part of an object, only they can be inherited. Read what is static function in C++ programming