What is Polymorphism in C++

The term “polymorphism” is a combination of “poly” and “morph” and means many forms. In C ++, polymorphism is an important feature of object-oriented programming. Simply put, polymorphism can be defined as the ability of a message to be displayed in multiple formats. A real example of polymorphism, at the same time you can have different traits. At the same time, as men, fathers, husbands, and employees. Therefore, the same person behaves differently in different situations. This is called a polymorphism. 

Types of Polymorphism:

1. Compile time Polymorphism

2. Runtime Polymorphism

1. Compile Time Polymorphism:

Compile-time polymorphism is determined by function overloads and operator overloads.

Call the overloaded function by matching the number and type of arguments. The information is present at the time of compilation. This means that the C ++ compiler will choose the appropriate function at compile time.

a). Function Overloading:

Function overloading occurs when there are many functions with similar names but different arguments. Arguments can vary in number or type.

For example:

Practice Your Code Here

In the example above, a single function called a function behaves differently in three different situations that are characteristic of polymorphism.

b). Operator Overloading:

Operator overloads define new meanings for C ++ operators. It also changes the way the operator works. For example, you can define the + operator to concatenate two strings. Known as the addition operator to add numbers. After the definition, if you put it between whole numbers, the whole number will be added. If you put it between strings, the strings will be concatenated.

For example:

Practice Your Code Here

In the above example, the “+” operator is overloaded. “+” operator is an addition operator, allowing you to add two numbers (integer or floating-point), but the operator is designed to perform the addition of two complex numbers.

2. Runtime polymorphism:

This happens when the object’s methods are called at run time instead of compile time. Run-time polymorphism is achieved by overriding functions. The called / called function is set at run time.

a). Virtual Function:

Virtual functions are another way to implement run-time polymorphism in C ++. This is a special function defined in the base class and redefined in the derived class. You must use the virtual keyword to declare a virtual function. The keyword must precede the declaration of the base class function.

If the virtual function class is inherited, the virtual class redefines the virtual function to suit your needs.

For example:

Practice Your Code Here

b). Function overriding:

Function overriding occur when a base class function receives a new definition in a derived class. At that point, the base function is said to vanish.

For example:

Practice Your Code Here

Important points about polymorphism:

  • Functions can behave differently from one instance to another.
  • Behavior depends on type of data used in operation.
  • Polymorphism is used to implement inheritance.

Advantages of Polymorphism:

  • Helps programmers reuse code and classes created, tested, and implemented.
  • You can use a single variable name to store variables of various data types (Float, double, Long, Integer, etc.).
  • Polymorphism helps reduce connections between different functions.

Disadvantages of Polymorphism:

  • One of the drawbacks of polymorphism is that it is difficult for developers to implement polymorphism in their code.
  • Polymorphism at runtime can cause performance problems, as it requires determining the methods or variables that the machine calls. As a result, decisions are made at runtime, which basically results in poor performance.
  • Polymorphism reduces the readability of programs. To determine the actual execution time, you must determine what happens when your program runs.

Conclusion of Polymorphism:

Polymorphism means having many shapes. This happens when there is a hierarchy of classes associated with inheritance. Polymorphism allows a function to behave differently depending on the object calling / calling it. In compile-time polymorphism, the function to call is set at compile time. In run-time polymorphism, the function to call is set at run time. Compile-time polymorphism is determined by function overloads and operator overloads. In function overloading, many functions(methods) with similar names but different arguments. Parameters can vary in number or type. Operator overloads define new meanings for C ++ operators. Run-time polymorphism is achieved by overriding functions. When overriding a function, the derived class gives a new definition to the function defined in the base class.

References:

  • https://www.geeksforgeeks.org/polymorphism-in-c/
  • https://www.w3schools.com/cpp/cpp_polymorphism.asp
  • https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm
  • https://www.programiz.com/cpp-programming/polymorphism

(Visited 260 times, 1 visits today)

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
0
Would love your thoughts, please comment.x
()
x