State a runtime polymorphism example program in C++

Answer includes runtime polymorphism example program in C++ OOPs and about virtual and pure virtual function in C++.

Example of run time polymorphism in C++ object oriented programming is function overriding where functions get resolved at run time i.e. when we execute the application. This is different than compile time polymorphism where function gets resolved at compile time itself e.g. function overloading in C++ language and operator overloading.

When we override a function in CPP program, that function exists in the base class and derived class both with the same name and signature and depending upon the objects it gets resolved at run time from VTABLE and gets executed. (I will describe VTABLE in another post, please stay tuned.)

In the below example, class Files (base class) and PdfFiles (child class) both have the same function i.e. download.

If we create object of derived class to base class pointer e.g. Files *f = new PdfFiles(); then download function of child class will be called.

Note that what function we want to override from a base class, we need to make that function virtual in base class.If we don’t write virtual and use the statement Files *f = new PdfFiles(); the base class function will be called and not from child class.

As a Conclusion two points should be noted to override a function in inheritance relationship in C++ program.

  • function name in both class should be same
  • The function in base class should be virtual.

C++ Program Example – function overriding ( Runtime polymorphism)

#include<iostream>
using namespace std;

class Files{
public:
	void virtual download(){
		cout<<"Download Files"<<endl;
	}
};

class PdfFiles:public Files{
public:
	void download(){
		cout<<"Download Pdf"<<endl;
	} 
};

int main() {
	Files *f = new PdfFiles();
	f->download();

	delete f;

	return 0;
}

Output:

Download Pdf

As a next question, it was asked to write syntax of pure virtual function and why do we use it?

As a brief, syntax of pure virtual function is virtual void func() = 0; in C++ oops and it is use to create an interface and an abstract class C++ object oriented programming. Note that there is no interface keyword in C++ language as in C# or java.

You can read in detail about pure virtual function in C++ and how to use pure virtual function to create an interface in C++ and to create an abstract class with C++ program example.



2 thoughts on “State a runtime polymorphism example program in C++”

  1. There is an error (albeit a warning only) in the solution above:

    warning: deleting object of polymorphic class type ‘Files’ which has non-virtual destructor might cause undefined behavior [-Wdelete-non-virtual-dtor]|

    This means the following: as the base destructor hasn’t been made virtual then when deleting a derived dynamic object with delete through a pointer to its base class, no polymorphism takes place and only base class destructor gets executed. potentially leaving resources allocated in derived class allocated past the lifetime of derived class object.
    Thus:
    ALWAYS make base class destructors virtual!

    Reply

Leave a Comment