Monday, March 7, 2011

C++ - Virtual functions

I came across an interesting question in an interview today. It was about virtual functions. I wanted to improve my understanding of virtual functions, so here are some interesting findings, some new and some already known to me.
  • Destructors are called in the order - derived to base(if base destructor is virtual)
  • A virtual function is member function of a class , whose functionality can be overridden by its derived class.
  • The virtual function call is resolved at run time. Unlike non-virtual member functions which are resolved at compile time.(static binding/dynamic binding)
  • If a same 'name' function is implemented in both base class and derived class, the base class function would be called.
  • A class with one or more pure virtual functions becomes an abstract base class. This means that it has to be inherited.
  • An interface class has no members variables, and all of the functions are pure virtual.

No comments: