Categories: C++

Method Overriding(Polymorphism)

What is Polymorphism?

The term “polymorphism” refers to having multiple forms. Polymorphism is defined as the ability of a message to be displayed in more than one form. There are two types of polymorphism:

  • Compile-time Polymorphism.
  • Runtime Polymorphism.
Method Overriding:

Function overriding is a concept in C++ that allows us to define a function with the same name and function signature (parameters and data types) in both the base and derived classes with a different function definition. It redefines a base class function within the derived class, which overrides the base class function. Run-time polymorphism is implemented through function overriding. As a result, it overrides the function during program execution.

Example:

#include <iostream>

using namespace std;

class parent_class
{
public:
    virtual void print()
    {
        cout << "\n print() method"

                " of BaseClass";
    }
};

class derived_class : public parent_class
{
public:
     // Function Overriding - new definition of
    // print method of base class
    void print()
   {
       cout << "\n print() method"

                " of the Derived Class";
   }
};
// Driver code
int main()
{
    derived_class obj;
    obj.print();
}
Output:
print() method of the Derived Class
Binding Function Calls to Class Objects:

Binding is the process of connecting the function call to the function body. When it is done before the program is executed, it is referred to as Early Binding, Static Binding, or Compile-time Binding.

Example:
#include <iostream>
using namespace std;
class parent
{        public:
  void func()
  {
   cout << "This is parent class" << endl;
  }
};
class Child : public parent
{         public:
  void func()
  {
   cout << "child class" << endl;
  }
};
int main()
{ parent *a;
 Child d;
 a= &d;
 a -> func();   //  early binding
 return 0;
}
Output:
This is parent class

The above example properly describes the function Call Binding using the Base class Pointer.

Note: also read about Order of Constructor Call in C++

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago