Difference Between Compile Time And Run Time Polymorphism In C++
Last Updated :
20 Oct, 2023
In this article, we will discuss the differences between the compile-time and runtime polymorphism in C++.
What is Polymorphism?
Poly means many and morph means forms or shape. Thus the word Polymorphism comes from Greek and it basically means having many forms.
For example,
Your DAD is your father. He's your mom's Husband. He's your grandma's son as well. Thus here your DAD is exhibiting many forms. He's a father, son, husband, brother, etc.
In C++, polymorphism can be divided into two types:
- Compile-time Polymorphism
- Run-time Polymorphism
Compile Time Polymorphism in C++
In compile-time polymorphism, the compiler determines which function or operation to call based on the number, types, and order of arguments. It is also called Static Polymorphism as the function calls are statically binded to its definition.
It is further categorized into two types:
- Function Overloading
- Operator Overloading
1. Function Overloading
When multiple functions in a class with the same name but different parameters exist, these functions are said to be overloaded.
- The functions can be overloaded by using a different number of arguments and by using different types of arguments.
- If two same name and same argument functions just vary in their return type then such function isn't overloaded.
Example
C++
// C++ Code to illustrate Function Overloading
#include <iostream>
using namespace std;
// Function to be overloaded
int add(int x, int y, int z = 0, int w = 0)
{
return (x + y + z + w);
}
int main()
{
// Passing different number of arguements
cout << add(10, 20) << endl;
cout << add(10, 20, 30) << endl;
cout << add(10, 20, 30, 40) << endl;
return 0;
}
Operator Overloading
We know that, "+" is used for addition or concatenation. Now, if I want to perform my customized operation i.e. upon calling +, I want to print "Hello Geek" or I want to perform subtraction instead of addition. In such cases, we use operator overloading.
Operator overloading is the process of defining different operations for the operator that vary depending on the argument type.
- Precedence and associativity remain intact in operators.
- List of operators that cannot be overloaded in C++ are ::, .*, ., ?:
- Operators = and & are already overloaded in C++, so we should avoid overloading them.
Example
C++
// C++ Code to illustrate Operator Overloading
#include <iostream>
using namespace std;
class B {
public:
int a, b;
public:
int add() { return a + b; }
// overloading + operator for B class
void operator+(B& obj)
{
int val1 = this->a;
int val2 = obj.a;
cout << "Output " << val2 - val1 << endl;
}
};
// driver code
int main()
{
B obj1, obj2;
obj1.a = 4;
obj2.a = 17;
obj1 + obj2;
return 0;
}
Run Time Polymorphism
In run-time polymorphism, the decision of which function to call is determined at runtime based on the actual object type rather than the reference or pointer type. It is also known as Dynamic Polymorphism because the function calls are dynamically bonded at the runtime.
Run Time Polymorphism can be exhibited by:
- Method Overriding using Virtual Functions
Method Overriding
Method overriding refers to the process of creating a new definition of a function in a derived class that is already defined inside its base class. Some rules that must be followed while overriding a method are:
- Method names must be the same.
- Method parameters must be the same.
Virtual Function
- Virtual Function is a member function that is declared as virtual in the base class and it can be overridden in the derived classes that inherit the base class.
- Virtual functions are generally declared in the base class and are typically defined in both the base and derived classes.
Example
C++
// C++ Code to illustrate Virtual Function
#include <iostream>
using namespace std;
// base class
class Base {
public:
// virtual function
virtual void print() { cout << "base"; }
};
// derived class
class Derived : public Base {
public:
// method overriding
void print() override { cout << "derived"; }
};
// driver code
int main()
{
Base* obj = new Derived();
obj->print();
delete obj;
return 0;
}
Difference Between Compile Time And Run Time Polymorphism
Compile-Time Polymorphism
| Run-Time Polymorphism
|
---|
It is also called Static Polymorphism. | It is also known as Dynamic Polymorphism. |
In compile-time polymorphism, the compiler determines which function or operation to call based on the number, types, and order of arguments. | In run-time polymorphism, the decision of which function to call is determined at runtime based on the actual object type rather than the reference or pointer type. |
Function calls are statically binded. | Function calls are dynamically binded. |
Compile-time Polymorphism can be exhibited by:
1. Function Overloading 2. Operator Overloading
|
Run-time Polymorphism can be exhibited by Function Overriding.
|
Faster execution rate. | Comparatively slower execution rate. |
Inheritance in not involved. | Involves inheritance. |
Similar Reads
Difference Between Compiler and Interpreter
The Compiler and Interpreter, both have similar works to perform. Interpreters and Compilers convert the Source Code (HLL) to Machine Code (understandable by Computer). In general, computer programs exist in High-Level Language that a human being can easily understand. But computers cannot understan
6 min read
Difference between Inheritance and Polymorphism
Inheritance is one in which a new class is created that inherits the properties of the already exist class. It supports the concept of code reusability and reduces the length of the code in object-oriented programming. Types of Inheritance are:Single inheritanceMulti-level inheritanceMultiple inheri
5 min read
Difference Between Structure and Class in C++
In C++, a structure works the same way as a class, except for just two small differences. The most important of them is hiding implementation details. A structure will by default not hide its implementation details from whoever uses it in code, while a class by default hides all its implementation d
3 min read
Difference between Inline and Macro in C++
1. Inline : An inline function is a normal function that is defined by the inline keyword. An inline function is a short function that is expanded by the compiler. And its arguments are evaluated only once. An inline functions are the short length functions that are automatically made the inline fun
3 min read
Difference between Single and Multiple Inheritance in C++
Single InheritanceSingle inheritance is one in which the derived class inherits the single base class either public, private, or protected access specifier. In single inheritance, the derived class uses the features or members of the single base class. These base class members can be accessed by a d
4 min read
Difference between Struct and Enum in C/C++ with Examples
Structure in C++ A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. The âstructâ keyword is used to create a structure. Syntax: struct structureName{ member1; member2; member3; . . . member
3 min read
Virtual Functions and Runtime Polymorphism in C++
A virtual function is a member function that is declared in the base class using the keyword virtual and is re-defined (Overridden) in the derived class. It tells the compiler to perform late binding where the compiler matches the object with the right called function and executes it during the runt
8 min read
Difference Between STL and Standard Library in C++
In C++, the term "Standard Library" and "Standard Template Library" are often misinterpreted as the same. Although they sound same with only a single word difference, they refer to the different part of the C++ programming language. In this article, we will learn what's the difference between the C+
3 min read
Difference between cout and std::cout in C++
The cout is a predefined object of ostream class, and it is used to print the data on the standard output device. Generally, when we write a program in Linux operating system for G++ compiler, it needs âstdâ namespace in the program.We use it by writing using namespace std; then we can access any of
2 min read
Similarities and Differences between Ruby and C++
There are many similarities between C++ and Ruby, some of them are: Just like C++, in Ruby⦠As in C++, public, private, and protected works similarly in Ruby also .Inheritance syntax is still only one character, but itâs < instead of : in Ruby.The way ânamespaceâ is used in C++, in the similar wa
3 min read