My Blog
Articles related to programming, computer science, technology and research.

27/09/2016 Categories: C++ Programming. No Comments on Polymorphism in C++

Introduction

Polymorphism is one of the key features of object orientation. It means many forms or single interface multiple implementations. Polymorphism is of two types: 1) Compile-time polymorphism and 2) Run-time polymorphism as illustrated in the following figure:

polymorphism-cpp

Function Overriding

When a base class and sub class contains a function with the same signature, and the function is called with base class object, then the function in derived class executes and the function in base class is said to be overridden. This is known as function overriding. Following program demonstrates function overriding:

 

In the above program, both super class A and sub class B contains the same function show() with same signature (function name plus parameters). So, when sub class object is used to call show(), function in sub class B executes overriding the function in super class A.

 

Note: Overloading of functions is not supported across classes (in inheritance) in C++.

 

Virtual Functions

 

We know that when a base class pointer refers to a derived class object, the extra features in derived class are not available. To access the extra features in the derived class, we make the functions in the base class as virtual. Syntax for creating a virtual function is as follows:

 

A class which contains one or more virtual functions is known as a polymorphic class. Following program demonstrates accessing derived class features using virtual functions:

 

Rules for Virtual Functions

 

Following points must be remembered while working with virtual functions:

  • Virtual functions must be members of a class.
  • Virtual functions must be created in public section so that objects can access them.
  • When virtual function is defined outside the class, virtual keyword is required only in the function declaration. Not necessary in the function definition.
  • Virtual functions cannot be static members.
  • Virtual functions must be accessed using a pointer to the object.
  • A virtual function cannot be declared as a friend of another class.
  • Virtual functions must be defined in the base class even though it does not have any significance.
  • The signature of virtual function in base class and derived class must be same.
  • A class must have a virtual destructor but it cannot have a virtual constructor.

 

Pure Virtual Functions

 

When the code for virtual function in a base class is insignificant, we can make such virtual functions as pure virtual functions. A pure virtual function is a virtual function without any definition. Syntax for creating a pure virtual functions is as follows:

virtual return-type function-name(params-list) = 0;

 

A class which contains at least one pure virtual function is called a abstract class and the class which provides the definition for the pure virtual function is called a concrete class.

 

Late Binding (Dynamic Polymorphism)

 

In inheritance when a derived class object is assigned to a base class pointer, and a polymorphic function is invoked, the function call is linked to the function definition at run-time. Such postponement of linkage to run-time instead of compile-time is called as late binding or dynamic polymorphism.

 

Abstract Class

 

A class which contains at least one pure virtual function is called an abstract class. Since abstract class is incomplete, another class should derive it and provide definitions for the pure virtual functions in the abstract class.

 

Following are the features of an abstract class:

  • Abstract class must contain at least one pure virtual function.
  • Objects cannot be created for an abstract class. But pointers can be created.
  • Classes inheriting an abstract class must provide definitions for all pure virtual functions in the abstract class. Otherwise, the sub class also becomes an abstract class.
  • An abstract class can have non-virtual functions and data members.

 

Following are the uses of an abstract class:

  • Abstract class provides a common standard interface for all the sub classes.
  • Abstract classes allow new features to be easily added to an existing application.

 

Following program demonstrates pure virtual functions, late binding and an abstract class:

 

Virtual Constructors and Destructors

C++ allows programmers to create virtual destructors. But, it doesn’t allow virtual constructors to be created because of various reasons. To know why a virtual destructor is needed, consider the following program:

 

From the above output you can see that derived class destructor didn’t execute. This might lead to problems like memory leakage. To avoid such problems we make destructor in base class as virtual destructor.

 

Virtual destructor ensures that the derived class destructor is executed. To create a virtual destructor, precede the destructor definition with virtual keyword. Following program demonstrates a virtual destructor:

 

Now you can see that derived class constructor is also executed.

 

Advantages and Disadvantages of Inheritance

 

Advantages of inheritance are as follows:

  • Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of inherited class.
  • Reusability enhanced reliability. The base class code will be already tested and debugged.
  • As the existing code is reused, it leads to less development and maintenance costs.
  • Inheritance makes the sub classes follow a standard interface.
  • Inheritance helps to reduce code redundancy and supports code extensibility.
  • Inheritance facilitates creation of class libraries.

 

Disadvantages of inheritance are as follows:

  • Inherited functions work slower than normal function as there is indirection.
  • Improper use of inheritance may lead to wrong solutions.
  • Often, data members in the base class are left unused which may lead to memory wastage.
  • Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes.

 

 

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me