Job preparationC++

C++ interview questions and answers

10 questions · 6 min read

Polymorphism, constructors, friend functions, templates and the STL — questions for software and systems roles.

C++ remains the language of systems programming, games and performance-critical software, and its interviews go correspondingly deep.

The questions circle two areas: the object model, and memory. Everything below sits in one of the two.

The questions

What is polymorphism in C++ and what kinds are there?

What to coverPolymorphism lets different objects behave differently through one interface. It comes in two kinds: compile-time (method overloading) and runtime (method overriding).

What are constructors and destructors in C++ and what are they for?

What to coverA constructor is a member function called automatically when a new object of the class is created. A destructor is a member function called when the object is destroyed.

What is a friend function in C++ and what is it used for?

What to coverA friend function can reach the private and protected members of a class even though it is not a member of that class.

What is an abstract class and a pure virtual function in C++?

What to coverAn abstract class is one with one or more pure virtual functions. A pure virtual function is one a derived class must implement.

What is the difference between overloading and overriding in C++?

What to coverOverloading means several versions of a function with the same name taking different parameters. Overriding means redefining a base class virtual function in a derived class.

What is a template in C++ and what is it used for?

What to coverTemplates are the generic programming feature: they let you write type-independent code for functions and classes.

What is the STL in C++ and what are its main components?

What to coverThe Standard Template Library is a generic library providing template classes and functions for data structures and algorithms. Its main components are containers, iterators and algorithms.

What is the difference between a pointer and a reference?

What to coverA pointer is a separate variable holding the address of another: it can be null, it can be pointed somewhere else later, and you dereference it with *. A reference is another name for an existing variable: it must be bound when it is created, cannot be rebound afterwards, and cannot be null. That is why references are the safer and cleaner choice for function parameters.

How is memory managed in C++, and what is dynamic memory allocation?

What to coverC++ memory lives in two places: the stack, where local variables are created and destroyed for you; and the heap, where you ask for space and must release it yourself. Dynamic memory allocation is taking heap space at runtime — new to take it, delete to give it back. Forget the delete and you have a memory leak, which is why modern C++ uses unique_ptr and shared_ptr, which release memory on their own.

What is inheritance in C++ and what kinds are there?

What to coverInheritance means one class takes on the properties and behaviour of another — a way of extending code instead of rewriting it. In C++ the kinds are single (one base class), multiple (several base classes, which Java does not allow), multi-level (A to B, B to C), hierarchical (many derived classes from one base), and hybrid, a combination of the above.

Tips for this round

  • Confusing overloading with overriding is the commonest slip. Separate them with an example.
  • Pointers and memory will come up — be ready on smart pointers as well as new/delete.

Want to learn this hands-on? Take a look at the related course.

Browse courses