Sample C++ Code to Learn Virtual Functions in a C++ Class

This Sample C++ Code describes how to declare and use Virtual Functions in C++. This post also explains what happens when you do not use virtual functions and use pointers to create objects of classes. In case the program using your class is not going to use pointers or your class is not going to use function overloading, you do not need to be concerned about whether or not to use virtual functions. Have a look at the given below Base C++ Class which has two functions out of which one function is marked with virtual keyboard and other is not marked with virtual keyword.

C++ Class Containing Virtual Function

C++ Class Containing Virtual Function

Now let’s have a look at another class inherited from the above mentioned class and let’s use function overloading. Finally we will be using pointers to objects of these classes to call the virtual and non virtual functions. The given below screenshot displays the body of the inherited class along with both functions overloaded.

C++ Class Inherited from a C++ Class Containing Virtual Function

C++ Class Inherited from a C++ Class Containing Virtual Function

Note that even though the keyword virtual has not been used for the overloaded function in the inherited class, still the function is treated as virtual function. Now let’s use pointers to create objects of above classes and call the functions.

Main Function of C++ Creating Object of Class Containing Virtual Function

Main Function of C++ Creating Object of Class Containing Virtual Function

Note that in the above screenshot the entry point function of the C++ project has created two pointer variables of the base class and is allocating memory to the pointers using new keywords. Look closely at the above screenshot and notice that the first pointer is allocated memory using the base class and the second pointer of the base class is allocated memory using the object of the inherited class.

In case you are wondering why to make so much efforts in using pointers, doing function overloading and using virtual functions, you are not alone and everybody has a logical question as to why to get into so much trouble ? The answer to the above question may come to you as surprise but while creating actual professional software applications, these virtual functions are sort of rescue to the programmer. Let’s take an example of Microsoft Word which can open documents of various types like .doc. .rtf , .txt, etc. Also when Microsoft Word loads small or really big document, it allocates memory and when the document is closed it releases the memory. Yes you can test this yourself by watching memory allocated by Word or any other application using Task manager. This explains the usage of pointers, however usage of virtual functions still remain unanswered. Let’s have a look at the output of the above c++ code to understand what all efforts are required by Microsoft Software Developers to make Microsoft Word support another document type.

 

Console Output of C++ Project Containing Class with Virtual Function
Console Output of C++ Project Containing Class with Virtual Function

From the Entry Point Function of this C++ Project we called the Functions in sequence for both objects. The Function calls of first object worked fine, however the second object’s function call might be unexpected for you. Note that in the Second Pointer, although we have assigned the object of the inherited class, still the non virtual function of the base class has been called.

Similarly whenever Microsoft Word needs to support another document type (like let’s say pdf document), the software developers at Microsoft simply create a new Inherited Class and overload virtual functions of the base Microsoft Word opening class. When end user actually opens document of different types, the virtual functions and usage of pointers make sure that the data in the document is interpreted appropriately and user is provided the correct information. The Full Code of this C++ Project has been provided at the end of this post.

Disclaimer : The Microsoft Word Example above has been taken as an example to teach beginner when and why to use virtual functions along with pointers. Microsoft or any employee of Microsoft has not shared any code with the author and the above example is totally assumption of the Author to give reader of this C++ tutorial an idea of how pointers and virtual functions can work in actual software applications.

// VirtualFunctions.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;class CBaseClass{public:virtual void SayHello(){cout << "Hello from CBaseClassnn";}void SayHi(){cout << "Hi from CBaseClassnn";}};class CInheritedClass : public CBaseClass{public://This is Still a Virtual Function as//SayHello was Virtual Function in CBaseClassvoid SayHello(){cout << "Hello from CInheritedClassnn";}void SayHi(){cout << "Hi from CInheritedClassnn";}};int _tmain(int argc, _TCHAR* argv[]){CBaseClass* vl_pObject1= new CBaseClass();CBaseClass* vl_pObject2= new CInheritedClass();vl_pObject1->SayHi();vl_pObject1->SayHello();vl_pObject2->SayHi();vl_pObject2->SayHello();delete vl_pObject1;delete vl_pObject2;return 0;}