Viva Questions & Answers: Inheritance & Polymorphism
1. What is inheritance in C++?
Inheritance is a mechanism in C++ where a class (derived) acquires properties and behaviors (members and
methods) of another class (base), promoting code reuse and logical hierarchy.
2. What are the types of inheritance in C++?
The types are: Single inheritance, Multiple inheritance, Multilevel inheritance, Hierarchical inheritance, Hybrid
inheritance.
3. What is a friend class?
A friend class can access private and protected members of another class in which it is declared as a friend.
4. What is multiple inheritance?
Multiple inheritance is when a class is derived from more than one base class.
5. What is the ambiguity problem in multiple inheritance?
When two base classes have a function or member with the same name, the derived class cannot resolve
which one to use, leading to ambiguity.
6. How do you solve ambiguity in hybrid inheritance?
Using the virtual base class keyword while inheriting the common base class resolves ambiguity by ensuring
only one shared instance.
7. What is a virtual base class?
A base class declared with the keyword virtual to prevent multiple copies of it being inherited by derived
classes in complex hierarchies.
Viva Questions & Answers: Inheritance & Polymorphism
8. What are access specifiers and their effect in inheritance?
Public: Members retain their access levels. Protected: Public and protected members become protected.
Private: All inherited members become private.
9. What is a static data member?
A class member that is shared across all instances of the class. It has a single memory location.
10. What is polymorphism?
Polymorphism means 'many forms.' It allows the same function name to behave differently based on the
object context.
11. What are the types of polymorphism in C++?
Compile-time (static) polymorphism - via function overloading and operator overloading. Runtime (dynamic)
polymorphism - via virtual functions.
12. What is a virtual function?
A function declared with the virtual keyword in the base class, allowing it to be overridden in derived classes
for runtime polymorphism.
13. What is a pure virtual function?
A virtual function with no implementation in the base class, defined as = 0. It forces derived classes to
implement it.
14. What is an abstract base class?
A class that has at least one pure virtual function. It cannot be instantiated and acts as a blueprint.
Viva Questions & Answers: Inheritance & Polymorphism
15. What is a polymorphic class?
A class with at least one virtual function is called a polymorphic class.
16. What is the difference between early and late binding?
Early binding: Function call is resolved at compile time (normal functions). Late binding: Function call is
resolved at runtime using virtual functions.
17. What is the purpose of a virtual destructor?
It ensures that when a derived object is deleted via a base class pointer, the derived class destructor is also
called.
18. What is a pointer to an object?
A pointer variable that holds the address of an object. Useful for accessing members via ->.
19. What are container classes?
Classes designed to hold and manage collections of data types (e.g., vector, list, etc.).
20. What is a contained class?
When one class contains an object of another class as a member, it is called composition or contained class.
21. What is a singleton class?
A class designed to allow only one object instance at any time, usually implemented using a private
constructor and a static method.