Loading Posts...

Polymorphism in C++ Example | C++ Polymorphism Tutorial

Polymorphism in C++ Example | C++ Polymorphism Tutorial

Polymorphism in C++ Example | C++ Polymorphism Tutorial is a topic. The word polymorphism is derived from a combination of greek word poly + morphs where poly means many and morphs means forms. In other words, we can say that one which takes many forms. Polymorphism is one of the essential features of object-oriented programming. In more technical words, we can define polymorphism as the feature of object-oriented programming that allows the class object to behave differently at different times.

Polymorphism in C++

See the following figure.

 

Polymorphism in C++

#Real-World Example of Polymorphism

  1. As we all know, polymorphism is taking many forms. We can relate it with the example of a man, who is a husband, an employee at an office, son, father hence, man can take different forms. 
  2. Suppose a teacher at a school at teaching different subjects in different classes like he can be an English teacher for class 9 and the same teacher can be science class for class 10 hence taking various forms at different times.

There are two types of polymorphism: 

  1. Compile-time polymorphism
  2. Runtime polymorphism

#Compile-time polymorphism

Compile-time polymorphism can be achieved by overloading an operator or function.

#Function Overloading

In the case of function overloading, there are many functions with the same name but different no number of arguments of the return type. There are two ways by which we can overload any function. 1st is by changing the number of argument, and the 2nd is by changing the type of the argument.

#Operator Overloading

C++ allows us to specify more than one definition for an operator in the same scope and we call it operator overloading. Another way of defining operator overloading is that when an operator is overloaded with multiple jobs, it is known as operator overloading.

#Runtime polymorphism

Runtime polymorphism can be achieved by the function overriding.

#Function Overriding

If both the parent and child class have a member function with the same name and same number of arguments and then we create an object of child class and we call the member function present in both the child and parent class with the same name and same number of arguments then the member function of the child class is called and the member function of base class is ignored. This concept is known as function overriding.

#Difference between compile time polymorphism and runtime polymorphism

Compile-time polymorphism Runtime Polymorphism
It is generally for overloading functions and operators.  It is generally for overriding functions.
It is early or static binding. It is late or dynamic binding.

It provides fast execution.

It provides a slow execution.
It is flexible as it is executed at compile time.

It is flexible as it is executed at runtime.

A function to be invoked is known at a compile-time. A function to be invoked is known at runtime.

 

#Examples for compile-time polymorphism

#1: Write a program to show function overloading at compile time.

#include 
using namespace std;

class FunctionOverloading
{
public:
	void calculation(int a, int b, int k) /*addition of two numbers*/
	{
		cout << "The sum is = " << a + b + k << endl;
	}
	void calculation(double c, double d, double e) /*Multiplication of two numbers*/
	{
		cout << "Multiplication is = " << c * d * e << endl;
	}
	int calculation(int f, int g)
	{
		return (f / g);
	}
};
int main()
{
	FunctionOverloading obj1;

	obj1.calculation(5, 6, 7);
	obj1.calculation(2.34, 4.54, 6.72);
	cout << "The division is=" << obj1.calculation(10, 5) << endl;
}

See the following output.

 

Examples for compile-time polymorphism

#2: Write a program to show operator overloading at compile time.

#include 
using namespace std;
class A
{
private:
  int a, b;

public:
  int x, y;
  void setdata(int x, int y) /*Assigning private data members*/
  {
    a = x;
    b = y;
  }
  void showdata()
  {
    cout << "nValue ofa=" << a << "nValue ofb=" << b << endl;
  }
  A operator+(A obj)
  {
    A temp;
    temp.a = a + obj.a;
    temp.b = b + obj.b;
    return (temp);
  }
};
int main()
{
  A obj1, obj2, obj3;
  obj1.setdata(4, 5);
  obj2.setdata(2, 3);
  obj3 = obj1 + obj2; /* The same line can be written as obj3=obj1.operator+(obj2);*/
  obj3.showdata();
}

See the following output.

 

Write a program to show operator overloading at compile time

#Examples for runtime polymorphism

#1: Write a program to show the mechanism of function overriding.

#include 
using namespace std;
class A
{
public:
  int add(int x, int y, int z)
  {
    return (x + y + z);
  }
};
class B : public A
{
public:
  int add(int x, int y, int z)
  {
    cout << "The first number: " << x << endl;
    cout << "The second number:" << y << endl;
    cout << "The third number:" << z << endl;
    return (x + y);
  }
};
int main()
{
  B obj;
  int q, w, r;
  cout << "Enter 3 numbers for showing function overriding: ";
  cin >> q >> w >> r;

  cout << "The number retuns the sum of only two numbers(Function Overriding): " << obj.add(q, w, r) << endl;
  return (0);
}

See the following output.

 

Write a program to show the mechanism of function overriding

#2: Write a program to access the base class member function after the occurrence of function overriding. (Using scope resolution operator)

#include 
using namespace std;
class A
{
public:
  void base_derived()
  {
    cout << "I am the base class member function with the same name as  member function of derived class" << endl;
  }
};
class B : public A
{
public:
  void base_derived()
  {
    cout << "I am the derived class member function" << endl;
    A::base_derived();
  }
};
int main()
{
  B obj;

  obj.base_derived();
  return (0);
}

See the following output.

 

Using scope resolution operator

#Virtual Function

virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class the virtual function, with another version in the derived class, signals to a compiler that we don’t want static linkage for this function.

What we do want is a selection of the function to be called at any given point in a program to be based on the sort of object for which it is called. This kind of operation is referred to as the dynamic linkage or late binding.

#Pure Virtual Functions

In some instances, you want to include the virtual function in the base class so that it may be redefined in the derived class to suit the objects of that class, but that there is no meaningful definition that you could give for the function in the base class.

Finally, Polymorphism in C++ Example | C++ Polymorphism Tutorial is over.

The post Polymorphism in C++ Example | C++ Polymorphism Tutorial appeared first on AppDividend.

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

user

The author didnt add any Information to his profile yet

Leave a Comment

Loading Posts...