POLYMORPHISM IN C++

Introduction:

In this article, we are going to learn about polymorphism. In object-oriented programming, we use 3 main concepts: inheritance, encapsulation, and polymorphism. Polymorphism is considered one of the important features of Object-Oriented Programming.

C++ Polymorphism:

The word “polymorphism” means having many forms.poly means many and morphism means forms. it is a greek word. Polymorphism is considered one of the important features of Object-Oriented Programming. In C++, polymorphism is mainly divided into two types:

Compile-time Polymorphism

Runtime Polymorphism

We can implement polymorphism in C++ using the following ways:

  • Function overloading
  • Operator overloading
  • Function overriding
  • Virtual functions

Why do we need Polymorphism?

Polymorphism allows us to create consistent code.

For example, we need to calculate the area of a circle and a square. To do so, we can create a Shape class and derive two classes Circle and Square from it.

In this case, it makes sense to create a function having the same name calculate area() in both the derived classes rather than creating functions with different names, thus making our code more consistent.

Types of Polymorphism:

1. Compile time polymorphism:

This type of polymorphism can be achieved by function overloading and operator overloading. when the relationship between the definition of different functions and their function calls, is determined during the compile-time, it is known as compile-time polymorphism also known as early binding polymorphism. All the methods of compile-time polymorphism get called or invoked during the compile time.

See also  Expressions in C++

The implementation of compile-time polymorphism is achieved in two ways:

Function overloading

Operator overloading

i.Function overloading:

Function overloading is an important pillar of polymorphism in C++. When the same function name is overloaded with multiple tasks, that function gets overloaded.

Syntax

// same name functions having different arguments
int func (int var) {}
int func (float var) {}

Program:

#include <bits/stdc++.h>
using namespace std;
class functionOverloadingExample
{
    public:
    // function with 1 int parameter
    void myFunc(int a)
    {
        cout << "a = " << a << "\n\n";
    }
    // myFunction with the same name but 1 double parameter
    void myFunc(double a)
    {
        cout << "a =  " << a << "\n\n";
    }
    // myFunction with the same name and 2 int parameters
    void myFunc(int a, int b)
    {
        cout << "a = " << a << ", b = " << b << "\n\n";
    }
};
int main() {
    functionOverloadingExample obj;
    obj.myFunc(10); 
   obj.myFunc(10.20);
    obj.myFunc(100, 200);
    return 0;
}

Output:

a=10
b=10.2
a=100,b=200

ii. Operator overloading:

C++ also provides the option to overload operators. For example, we can make use of the addition operator (+) for string class to concatenate two strings.

These are some of the operators that can be overloaded in C++ :

Arithmetic operators : – , + , / , * , % and -=, +=, /= , *= , %=

Boolean algebra : !=, ==, > ,< ,>=, <= , && ,||

Bit manipulation : &, |, ^ ,<< ,>> and |=, &= ,>>=, <<=, ^=

Memory management : new[] , delete[], delete, new.

Program:

#include<iostream>
using namespace std;
class Count
{
    int x;
    public:
     //  constructor
    Count(int X = 0)
    {
        this -> x = X;
    }
    // Overloading the ++ operator
    Count operator++()
    {
       Count c ;
       c.x = ++x;   
        return c;
    }
    // print value of x
    void print()
    {
        cout << x << endl;
    }
};
int main()
{
   Count c1(143);
   cout << "Before using ++ operator: ";
    c1.print();    
   Count c2 = ++c1;
    cout << "After using ++ operator: ";
    c2.print();
    }

output:

Before using ++ operator: 143
After using ++ operator: 144

2. Run time polymorphism:

In runtime polymorphism, the compiler resolves the object at run time and then it decides which function call should be associated with that object. It is also known as dynamic or late binding polymorphism. All the methods of runtime polymorphism get invoked during the run time.

See also  C++ STRING FUNCTIONS:

Method overriding is an application of run time polymorphism where two or more functions with the same name, arguments, and return type accompany different classes of the same structure.

The implementation of run time polymorphism can be achieved in two ways:

Function overriding

Virtual functions

i, Function overriding:

It occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.

Program:

#include <bits/stdc++.h>
using namespace std;

class base
{
public:
	virtual void print ()
	{ cout<< "print base class" <<endl; }

	void show ()
	{ cout<< "show base class" <<endl; }
};
class derived:public base
{
public:
	void print () { cout<< "print the derived classes" <<endl; }
	void show () { cout<< "show the derived classes" <<endl; }
};
//main function
int main()
{
	base *bptr;
	derived d;
	bptr = &d;
	//virtual function, bound at runtime 
	bptr->print();
	// Non-virtual function, bound at compile time
	bptr->show();
	return 0;
}

Output:

print the derived classes
show the base classes

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock