LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 13 of 27

Inheritance

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

Inheritance · Study material

C++ Inheritance – Interview Questions

1. What is inheritance?

A mechanism for creating a new class from an existing one, reusing and extending its members; it models an IS-A relationship.

2. What are the types of inheritance in C++?

Single, multilevel, hierarchical, multiple and hybrid.

3. What are the inheritance modes and their effect?

public keeps public/protected as they are; protected turns both into protected; private turns both into private. Private base members are never accessible in the derived class.

4. What is the default inheritance mode?

private for class, public for struct.

5. In what order are constructors and destructors called?

Constructors: virtual bases, then direct bases in declaration order, then members in declaration order, then the constructor body. Destructors: the exact reverse.

6. How does a derived class call a specific base constructor?

In its member initializer list: Derived(int x) : Base(x) {}.

7. Are constructors inherited?

Not automatically; using Base::Base; inherits them (C++11).

8. What is name hiding?

A derived-class member with the same name as a base member hides all base members of that name, regardless of parameters. using Base::name; brings the base overloads back.

9. What is upcasting and downcasting?

Upcasting converts a derived pointer/reference to a base one – implicit and safe. Downcasting converts base to derived – explicit, with dynamic_cast (checked) or static_cast (unchecked).

10. What is object slicing?

Copying a derived object into a base object by value, which discards the derived part.

11. What is multiple inheritance and what problems can it cause?

A class with several direct bases. It can cause name ambiguity and, with a common base, the diamond problem.

12. What is the diamond problem?

When two classes derive from the same base and a fourth class derives from both, it contains two copies of the common base, causing duplication and ambiguity.

13. How does virtual inheritance solve it?

Classes that inherit virtually from the common base share a single base sub-object in the most derived class.

14. Who constructs a virtual base class?

The most derived class, before any non-virtual bases; constructor calls to the virtual base from intermediate classes are ignored.

15. Which members are not inherited?

Constructors (unless using-declared), destructors (but they are always called), friend relationships, and in effect the copy/move assignment operators, which are regenerated for the derived class.

16. Can a derived class access private members of its base?

No. They exist in the derived object but only the base's member functions and friends can access them.

17. What does final do?

On a class it prevents inheritance from it; on a virtual function it prevents further overriding.

18. When should you use inheritance versus composition?

Inheritance for genuine IS-A relationships where substitutability and polymorphism are needed; composition (HAS-A) for reuse otherwise.

19. What does the Liskov substitution principle say about inheritance?

A derived object must be usable wherever a base object is expected without breaking the program's correctness.

20. What is the size of a derived object?

At least the size of its base sub-objects plus its own members (plus padding, and pointers for virtual bases or virtual functions).

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links