Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

virtual Functions and Polymorphism

 With virtual functions and polymorphism it’s possible to design and implement systems that
are easy to expand. It’s possible to write programs that process objects of types that may not exist
when it’s still in development.
 Polymorphic programming with virtual functions can eliminate the usage of switch logic.
Developers can use the virtual function mechanism to create automatically an equivalent logic,
avoiding error types associated with switch logic. Code that takes decision based on object types
and representation has a poor class design.
 Derived classes can provide their own implementation of a virtual function of a base class when
needed, or use the base class implementation otherwise.
 When a virtual function is invoked referencing an specific object by name and using the
operator of member selection, the reference it’s solved in compilation time (static linking) and
the virtual function called is the one defined (or inherited) for that one particular object.
 There are situations where it’s useful to define classes that the programmer doesn’t think of
create instances of. This classes are called abstract classes. It’s not possible to instantiate any
object of them.
 The classes that can have objects instantiated of are called concrete classes.
 A class becomes abstract by the declaration of one or more pure virtual functions. A pure
virtual function is one that has a =0 initialization in its declaration.
 If a class derives of another one that contains pure virtual functions without any definition in
the derived class, then such function is still considered pure virtual in the derived class. This
means that the derived class is also an abstract class.
 C++ allows polymorphism –the ability that objects from different classes related by inheritance
respond in different ways to the same function member.
 Polymorphism is implemented by virtual functions.
 When a call requested by a pointer or reference to the base class to use a virtual function
happens, C++ chooses the correct overlapped function in the derived class associated with the
object.
 By using virtual functions and polymorphism a calling of a function member can cause different
actions, depending on the type of the object receiving the call.
 Even if it is not possible to instantiate objects of abstract base classes, it’s possible to declare
pointers to abstract classes. Such pointers can be used to allow polymorphic managing of objects
of derived classes when such objects are instantiated from concrete classes.
 New types of classes are added to systems in a regular basis. This new classes are molded through
dynamic linking. It is not needed for an object type to be known in compilation time for a virtual
callback function to be compiled.
 Dynamic linking allows that ISV can distribute software without revealing any of their own secrets.
Software distributions may consist only of header files and object files. The source code doesn’t
have to be revealed. Software developers can later use inheritance to create new child classes
from those provided by ISV. Software that works with those classes will still work with the new
child classes, and it will use the overlapped virtual functions provided in those classes.
 Dynamic linking requires that, in execution time, a calling to a virtual function member is sended
to the most suitable version of said virtual function for a given class. A table of virtual
function, called vtable, is implemented as an array with pointers to functions. Each class with
virtual functions contains a vtable. For each virtual function of said class, the vtable has an
element with a pointer to the version of the virtual function that is to be used for an object of
that class. The virtual function that has to be used for a particular class might be defined in that
class, or might be an inherited function from a base class in a higher level of its hierarchy.
 When a base class provides a virtual function member, derived classes may overlap it but they
are not obligated to do so. Therefore, a derived class might use the version of the virtual
function member from the base class, and it would be declared as such in the vtable.
 Each object of a class with virtual functions has a pointer to the vtable of its class. The
appropriate pointer to function in the vtable is gotten and dereferenced to complete the calling
in execution time. This searching in the vtable and dereference of the pointer require a minimum
overloading of the execution time, that is often lower that the best possible code client.
 Declare as virtual the destructor of the base class if the class has other virtual functions. This
makes all of the destructors of the derived classes to be virtual even if they doesn’t have the
same name as the destructor of the base class. If an object in the hierarchy is destroyed explicitly
by applying the operator delete to the pointer of the base class to an object of the derived class,
the destructor of the appropriate class will be called.
 Any class with one or more pointers 0 in their vtable is an abstract class. The classes with no
pointers to vtable equal to 0 are concrete class.

You might also like