Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Summary

  • Late binding mean that the decision of which version of a member function is appropriate is decided at runtime. In C++, member functions that use late binding are called virtual functions. Polymorphism is another word for late binding.

  • A pure virtual function is a member function that has no definition. A pure virtual function is indicated by the word virtual and the notation =0 in the member function declaration. A class with one or more pure virtual functions is called an abstract class.

  • An abstract class is a type and it can be used as a base class to derive other classes. However, you cannot create an object of an abstract class type (unless it is an object of some derived class).

  • You can assign an object of a derived class to a variable of its base class (or any ancestor class), but the member variables that are not in the base class are lost. This is known as the slicing problem.

  • If the domain type of the pointer pAncestor is a base class for the domain type of the pointer pDescendent, then the following assignment of pointers is allowed:

    pAncestor = pDescendent;
  • Moreover, none of the data members or member functions of the dynamic variable being pointed to by pDescendent will be lost. Although all the extra fields of the dynamic variable are there, you will need virtual member functions to access them.

    It is a good programming practice to make destructors virtual.

  • + Share This
  • 🔖 Save To Your Account