
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Types of Polymorphisms - Ad-hoc, Inclusion, Parametric & Coercion
Here we will see different types of polymorphism. The types are −
- Ad-Hoc
- Inclusion
- Parametric
- Coercion
The Ad-Hoc polymorphism is called as overloading. This allows function with same name to act in different manner for different types. The function and the operator both can be overloaded. Some language does not support operator overloading, but function overloading is common.
Example
#include<iostream> using namespace std; int add(int a, int b) { return a + b; } string add(string a, string b) { return a + b; //concatenate } int main() { cout << "Addition of numbers: " << add(2, 7) << endl; cout << "Addition of Strings: " << add("hello", "World") << endl; }
Output
Addition of numbers: 9 Addition of Strings: helloWorld
The Inclusion polymorphism is called as subtyping. This allows to point derived classes using base class pointers and references. This is runtime polymorphism. We do not know type of the actual object until it is executing. We need virtual function in C++ to achieve this inclusion polymorphism.
Example
#include<iostream> using namespace std; class Base { public: virtual void print() { cout << "This is base class." << endl; } }; class Derived : public Base { public: void print() { cout << "This is derived class." << endl; } }; int main() { Base *ob1; Base base_obj; Derived derived_obj; ob1 = &base_obj; //object of base class ob1->print(); ob1 = &derived_obj; //same pointer to point derived object ob1->print(); }
Output
This is base class. This is derived class.
The Coercion polymorphism is called as casting. This type of polymorphism occurs when an object or the primitive is cast into some other type. There are two type of casting. The implicit casting is done using compiler itself, and explicit cast is done using const_cast, dynamic_cast etc.
Example
#include<iostream> using namespace std; class Integer { int val; public: Integer(int x) : val(x) { } operator int() const { return val; } }; void display(int x) { cout << "Value is: " << x << endl; } int main() { Integer x = 50; display(100); display(x); }
Output
Value is: 100 Value is: 50
The Parametric polymorphism is called as Early Binding. This type of polymorphism allows to use same piece of code for different types. We can get it by using templates.
Example
#include<iostream> using namespace std; template <class T> T maximum(T a, T b) { if(a > b) { return a; } else { return b; } } int main() { cout << "Max of (156, 78): " << maximum(156, 78) << endl; cout << "Max of (A, X): " << maximum('A', 'X') << endl; }
Output
Max of (156, 78): 156 Max of (A, X): X
- Related Articles
- Introduction of Mobile Ad hoc Network (MANET)
- What is ad-hoc network?\n
- Differentiate between infrastructure mode and the ad hoc mode of IEEE 802.11
- Parametric and Non-Parametric Methods of Efficiency Analysis of Company
- Differentiate between ad-hoc mode and Wi-Fi Direct mode in 802.11 architecture
- How does Implicit coercion differ from Explicit coercion in JavaScript?
- Python – Sort by range inclusion
- Product of two number using HOC - JavaScript
- Coercion: Definition and Meaning
- The Importance of Diversity and Inclusion in Business Leadership
- Role of Post Hoc comparisons in Research Psychology
- Argument Coercion in C/C++?
- What is JavaScript type coercion?
- Line colour of a 3D parametric curve in Python's Matplotlib.pyplot
- Returning adjacent element in React.js and HOC
