logo

C++ Interview Questions

What's the difference between C and C++?

Read more: C vs C++

What's the difference between struct and class?

  • struct: data members are public by default.
  • class: data members are private by default.

What is an inline function?

The compiler places a copy of the inline function at each point where the function is called at compile time.

Why use inline functions: they eliminate the function calling overhead of a traditional function.

What is a friend class / function?

A friend class can access private, protected, and public members of other classes in which it is declared as friends.

A friend function can access private, protected, and public members. But friend functions are not member functions.

What is a virtual function?

A virtual function is a member function in the base class that you redefine in a derived class. C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer.

What is a pure virtual function?

A pure virtual function is a function that has no implementation and is declared by assigning 0. It has no body.

virtual void fun() = 0;

What's the difference between compile-time polymorphism and runtime polymorphism?

  • compile-time polymorphism (a.k.a. static polymorphism, early binding): the compiler decides which method to call at the compile time. Achieved by function overloading and operator overloading.
  • runtime polymorphism (a.k.a dynamic polymorphism, late binding, or overriding): decide which method to call at runtime (NOT resolved by the compiler). Achieved by virtual functions and pointers.

What is operator overloading in C++?

To change the meaning of the operators like +, -, *, /, (), etc.

What are destructors?

Destructor: a function that is called when an object is destroyed.

Is deconstructor overloading possible?

Deconstructor overloading is NOT possible. Destructors take no arguments, so there's only one way to destroy an object.

What's the difference between pass-by-value and pass-by-reference

Read more: Pass-by-value vs Pass-by-reference

What's the unistd header?

unistd.h is the name of the header file that provides access to the POSIX operating system API.

How to Pause?

Under POSIX systems, the best solution seems to use:

#include <unistd.h>

pause ();

The process will be terminaled by signals (e.g. Ctrl-C). A more advanced usage is to use a signal-catching function, called when the corresponding signal is received, after which pause returns, resuming the process.