ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Introduction to Polymorphism in C++

prodevelopertutorial February 25, 2020

Poly means many, morphism means forms. Polymorphism in C++means one function behaving differently under different situations.

There are 2 types of polymorphism, namely:

  1. Compile time polymorphism
    1. Function overloading
    2. Operator overloading
    3. Function overriding
  1. Run time polymorphism
    1. Virtual functions

 

  1. Explicit and Implicit casting

1. Compile time polymorphism:

 

The binding happens at compile time is called as Compile time polymorphism. Binding means, the particular function that is to be called when a function call is made is decided at the compile time itself. This kind of binding done at the compile time is called as compile time polymorphism.

 

There are 3 different ways that we can achieve compile time polymorphism:.\

  1. Function overloading
  2. Function overriding
  3. Operator Overloading [explained in further chapter]

 

We shall see the first 2 types with help of an example;

 

1. Function overloading

 

This type will have the same function name, but difference in number of parameters and type of parameter being called. This is called as function overloading. Here the binding for the function call and the function definition being called occurs at the compile time.

Hence it is compile time polymorphism.

 

Example:

 

We have 2 overloaded add functions, one takes 2 parameters another will take 3 parameters. The function call is decided at compile time.

 

#include <iostream>

using namespace std;




void add(int a, int b)

{

    cout<<"In add function that takes 2 parameters"<< (a+b)<<endl;

}




void add(int a, int b, int c)

{

    cout<<"In add function that takes 3 parameters"<< (a+b+c)<<endl;

}




int main(void)

{

    add(1, 2);

    add(1, 2, 3);

   

    return 0;

}

 

Output:

In add function that takes 2 parameters, sum is 3

In add function that takes 3 parameters, sum is 6

 

2. Function overriding

In function overriding, the base class function is overridden in derived class. It means, a base class function with same name and parameters will be replaced in derived class in inheritance.

#include <iostream>

using namespace std;



class Base

{

  public:

    void dispaly()

    {

        cout<<"In base class"<<endl;

    }

};




class Derived : public Base

{

   public:

    void dispaly()

    {

        cout<<"In derived class"<<endl;

    }  

};




int main(void)

{

    Derived obj;

    obj.dispaly();

   

    Base obj1;

    obj1.dispaly();

   

    return 0;

}

 

Output:

 

In derived class

In base class

 

 

2. Runtime Polymorphism.

 

Runtime Polymorphism is achieved by using virtual functions. We shall study about virtual function in next chapters.

 

Example for Runtime Polymorphism:

 

#include <iostream>

using namespace std;




class Base

{

  public:

    virtual void dispaly()

    {

        cout<<"In base class"<<endl;

    }

};




class Derived : public Base

{

   public:

    void dispaly()

    {

        cout<<"In derived class"<<endl;

    }  

};




int main(void)

{

    Derived obj;




    Base *obj1 = &obj;

    obj1->dispaly();

   

    return 0;

}

 

Output:

 

In derived class

 

 

3. Explicit and Implicit casting

 

Consider below example:

 

float f = 10; // here int will be implicitly converted into float




int a = 10.10; // here float will be implicitly converted into int.


This is an example for explicit and implicit casting.

 

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT