References and Pointers
1. What are the differences between references and pointers?
A reference must be initialized when declared. No restriction on pointers.
A reference cannot be changed to reference another object. Pointers can do that.
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?
They can be used to save the copying of large objects when passed to functions or return from functions.
They can be used to modify the variables passing in this function.
Polymorphism (多态) and Virtual Functions
0a. What are virtual functions?
Functions that are declared with a
virtualkeyword in base class. (这意味着，继承base的子类中对应的函数也是虚函数，但是virtual可以省略)
Mainly used to achieve Runtime Polymorphism（运行时多态，见下）.
Ensure the correct function is called for an object, regardless of the type of reference (or pointer). (C++里提供了这样的保证机制，见问题3)
0b. Virtual function的一些Rule, *号代表建议 （思考为什么有这些限制！）：
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.
*Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism. Why?
*The prototype of virtual functions should be the same in the base and derived class. Why?
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.
A class may have virtual destructor but it cannot have a virtual constructor. Why? (Important question!)
See the example here.
There are 2 issues if we don’t have virtual destructor.
First, it will result in undefined behavior when
deleteusing 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.
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.
*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:
= 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—————
—————With virtual function—————
(我们只修改 class “Shape” 的area function)
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:
Create a static array of function pointers called
In the constructor of the class, add a data member
VPTRto the object being created. the
VPTRis a pointer pointing to
VTABLEof the class.
In each polymophic function call(调用虚函数), first look for
VPTRusing the base class reference or pointer（每个object都有这个VPTR的成员变量, then we can access the vtable of the right class and call the right function.
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)多态
- Can static