Key Concepts of C++

References and Pointers

1. What are the differences between references and pointers?

  1. A reference must be initialized when declared. No restriction on pointers.

  2. A reference cannot be changed to reference another object. Pointers can do that.

  3. A reference cannot be NULL. Pointers can.

Generally speaking, reference is safer, while pointer is more flexible.

2 What are the similarities of references and pointers?

  1. They can be used to save the copying of large objects when passed to functions or return from functions.

  2. They can be used to modify the variables passing in this function.

Polymorphism (多态) and Virtual Functions


0a. What are virtual functions?

  1. Functions that are declared with a virtual keyword in base class. (这意味着,继承base的子类中对应的函数也是虚函数,但是virtual可以省略)

  2. Mainly used to achieve Runtime Polymorphism(运行时多态,见下).

  3. Ensure the correct function is called for an object, regardless of the type of reference (or pointer). (C++里提供了这样的保证机制,见问题3)

0b. Virtual function的一些Rule, *号代表建议 (思考为什么有这些限制!):

  1. Virtual functions cannot be static and also cannot be a friend function of another class. Why?

    Because it doesn’t make any sense in C++.

    Virtual functions are invoked when you have a pointer/reference to an instance of a class. Static functions aren’t tied to a particular instance, they’re tied to a class. C++ doesn’t have pointers-to-class, so there is no scenario in which you could invoke a static function virtually.

  2. *Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism. Why?

  3. *The prototype of virtual functions should be the same in the base and derived class. Why?

    Because it will result in confusion. See this example.

    If the base virtual function uses default argument values, the derived virtual function should also include the default argument values in the code. This is not required by the compiler, but it’s recommended.

  4. They are always defined in base class and overridden in derived class. It is not mandatory for derived class to override (or re-define the virtual function), in that case base class version of function is used.

  5. A class may have virtual destructor but it cannot have a virtual constructor. Why? (Important question!)

    1. Virtual destructor
      See the example here.

      There are 2 issues if we don’t have virtual destructor.

      First, it will result in undefined behavior when delete using the base type pointer. Depending on the compiler, it might call the derived class destructor or the base class destructor.

      Second, when the derived class dynamically allocates memory (using new), a normal destructor (without virtual) might not free the memory allocated and there will be memory leakage.

    2. No virtual constructor
      Because there is no need of virtual constructor in C++. This is because C++ is static-typed language, meaning that, during compilation, the compiler needs to know the type to create an object.

  6. *Don’t write private virtual functions. Why?

    This is because private virtual member functions can be overridden by derived class, which is very confusing. Normal private member functions cannot be overridden in derived class.

0c. What is pure virtual function, What is abstract class? Why we need abstract class?

Pure virtual functions have the form:

virtual int f() = 0;

The = 0 here is called pure-specifier.
This means the function is not defined here.

An abstract class is a class that cannot be instantiated (You cannot create an object of an abstract class). So, if you have any pure virtual function in a class, then it is abstract.

Abstract class can be used to represent general concepts. For example, Shape is a general concepts, then we have Circle, Rectangle inherit the Shape.

1. What is polymorphism in C++?

C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. 简单的理解,就是“多种形态”。当一个Base类型的指针(这个指针可以指向继承了Base类型的object)call一个虚成员函数的时候,程序会自动根据这个指针指向的具体是什么类型,来运行对应的虚成员函数。可以看看下面这个例子。 (虚函数)

2. Example: See what is happening if we don’t use virtual functions.

—————Without virtual function—————

class Shape {
int width, height;
Shape( int a = 0, int b = 0): width(a), height(b) { }
int area() {
cout << "Parent class area:" <<endl;
return 0;
class Rectangle: public Shape {
Rectangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
int _area = width * height;
cout << "Rectangle class area: " << _area<<endl;
return area;
int main() {
Shape *shape;
Rectangle rec(10,7);
// store the address of Rectangle
shape = &rec;
// call rectangle area.
return 0;


Parent class area:

—————With virtual function—————

(我们只修改 class “Shape” 的area function)

class Shape {
int width, height;
Shape( int a = 0, int b = 0): width(a), height(b) { }
virtual int area() {
cout << "Parent class area :" <<endl;
return 0;


Rectangle class area: 70

3. What is the mechanisms that C++ uses to enable polymorphism?

C++ mainly uses VPTR (virtual pointer) and VTABLE (virtual table) to do that.

During compilation, if a class has virtual function(s), the compiler will add additional code to do the following things:

  1. Create a static array of function pointers called VTABLE. 只要一个class有虚函数(无论是基类,还是子类),那么编译器会在这个class里加一个static的array,这个array里存的是这个class里所有虚函数的指针。

  2. In the constructor of the class, add a data member VPTR to the object being created. the VPTR is a pointer pointing to VTABLE of the class.

  3. In each polymophic function call(调用虚函数), first look for VPTR using the base class reference or pointer(每个object都有这个VPTR的成员变量, then we can access the vtable of the right class and call the right function.


Illustration of VPTR and VTABLE

4. What is static polymorphism and dynamic(runtime) polymorphism?

在C++中,static polymorphism指的是编译时的“多态”,包括templates(模版函数/模版类)和function overloading(函数重载). 在C++里面,我们一般就直说templates和function了,这个static polymorphism的术语用的比较少。 Dynamic Polymorphism就是前面提到的polymorphism。一般来说,不加说明的话,在C++里面,单说polymorphism就是指的通过虚函数实现的运行时(Runtime)多态

Sample Questions:

  1. Can static