For example, suppose we have the function makeSound(). When a cat calls this function, it will produce the meow sound. When a cow invokes the same function, it will provide the moow sound. Though we have one function, it behaves differently under different circumstances. The function has many forms; hence, we have achieved polymorphism. In this C++ tutorial, you will learn:

What is Polymorphism?
Types of Polymorphism
Compile Time Polymorphism
Function Overloading
Operator Overloading
Runtime Polymorphism
Function Overriding
C++ Virtual Function
Compile-Time Polymorphism Vs. Run-Time Polymorphism

Types of Polymorphism

C++ supports two types of polymorphism:

Compile-time polymorphism, and Runtime polymorphism.

Compile Time Polymorphism

You invoke the overloaded functions by matching the number and type of arguments. The information is present during compile-time. This means the C++ compiler will select the right function at compile time. Compile-time polymorphism is achieved through function overloading and operator overloading.

Function Overloading

Function overloading occurs when we have many functions with similar names but different arguments. The arguments may differ in terms of number or type. Example 1: Output:

Here is a screenshot of the code:

Code Explanation:

Include the iostream header file into our code. We will be able to use its functions. Include the std namespace in our code. We will be able to use its classes without calling it. Create a function named test that takes an integer parameter i. The { marks the beginning of the body of function test. Statement to be executed if the above function test is invoked/called. End of the body of above function test. Create a function named test that takes a float parameter f. The { marks the beginning of the body of function test. Statement to be executed if the above function test is invoked/called. End of the body of the above function test. Create a function named test that takes a character parameter ch. The { marks the beginning of the body of function test. Statement to be executed if the above function test is invoked/called. End of the body of the above function test. Call the main() function. The { marks the beginning of the body of the function. Call the function test and passing 5 to it as the value of the argument. This invokes the test function that accepts an integer argument, that is, the first test function. Call the function test and passing 5.5 to it as the value of the argument. This will invoke the test function that accepts a float argument, that is, the second test function. Call the function test and passing five to it as the value of the argument. This will invoke the test function that accepts a character argument, that is, the third test function. The program must return a value if it runs successfully. The end of the body of the main() function.

We have three functions with the same name but different types of arguments. We have achieved polymorphism.

Operator Overloading

In Operator Overloading, we define a new meaning for a C++ operator. It also changes how the operator works. For example, we can define the + operator to concatenate two strings. We know it as the addition operator for adding numerical values. After our definition, when placed between integers, it will add them. When placed between strings, it will concatenate them. Example 2: Output:

Here is a screenshot of the code:

Code Explanation:

Include the iostream header file into our program in order to use its functions. Include the std namespace into our program in order to use its classes without calling it. Create a class named ComplexNum. The { marks the beginning of the class body. Use the private access modifier to mark variables as private, meaning they can only be accessed from within the class. Define two integer variables, real and over. Use the public access modifier to mark the constructor as public, meaning that it will be accessible even from outside the class. Create the class constructor and initializing the variables. Initialize the value of the variable real. Initialize the value of the variable over. End of the constructor body. We need to override the meaning of the + operator. Create the data type result of type ComplexNum. Use the + operator with complex numbers. This line will add the real part of a number to the real part of another number. Use the + operator with complex numbers. This line will add the imaginary part of a number to the imaginary part of another number. The program will return the value of the variable result upon successful execution. End of the definition of the new meaning of + operator, that is, overloading. Call the print() method. Print the new complex number after addition on the console. End of the body of print() function. End of the body of ComplexNum class. Call the main() function. Pass the values of both real and complex parts to be added. The first part of c1 will be added to the first part of c2, that is, 10+3. The second part of c1 will be added to the second part of c, that is, 2+7. Perform an operation using the overloaded + operator and storing the result in variable c3. Print the value of variable c3 on the console. End of the body of main() function.

Runtime Polymorphism

This happens when an object’s method is invoked/called during runtime rather than during compile time. Runtime polymorphism is achieved through function overriding. The function to be called/invoked is established during runtime.

Function Overriding

Function overriding occurs when a function of the base class is given a new definition in a derived class. At that time, we can say the base function has been overridden. For example: Output:

Here is a screenshot of the code:

Code Explanation:

Import the iostream header file into our program to use its functions. Include the std namespace into our program in order to use its classes without calling it. Create a class named Mammal. The { marks the beginning of the class body. Use the public access modifier to set the function we are about to create as publicly accessible. It will be accessible from outside this class. Create a public function named eat. The { marks the beginning of the function body. Print the statement added to the cout function when the function eat() is invoked. The end of the body of function eat(). End of the body of the class Mammal. Create a class named Cow that inherits the Mammal class. Cow is the derived class, while Mammal is the base class. The { marks the beginning of this class. Use the public access modifier to mark the function we are about to create as publicly accessible. It will be accessible from outside this class. Override the function eat() that was defined in the base class. The { marks the beginning of the function body. The statement to print on the console when this function is invoked. End of the body of the function eat(). End of the body of the class Cow. Call the main() function. The { marks the beginning of the body of this function. Create an instance of the Cow class and giving it the name c. Call the eat() function defined in the Cow class. The program must return a value upon successful completion. End of the main() function.

C++ Virtual Function

A virtual function is another way of implementing run-time polymorphism in C++. It is a special function defined in a base class and redefined in the derived class. To declare a virtual function, you should use the virtual keyword. The keyword should precede the declaration of the function in the base class. If a virtual function class is inherited, the virtual class redefines the virtual function to suit its needs. For example: Output:

Here is a screenshot of the code:

Code Explanation:

Include the iostream header file in the code to use its functions. Include the std namespace in our code to use its classes without calling it. Create a class named ClassA. Use the public access modifier to mark a class member as publicly accessible. Create a virtual function named show(). It will be a public function. The text to print when the show() invoked is invoked. The endl is a C++ keyword, which means end line. It moves the mouse cursor to the next line. End of the body of the virtual function show(). End of the body of the class ClassA. Creating a new class named ClassB that inherits the class ClassA. ClassA becomes the base class while ClassB becomes the derived class. Use the public access modifier to mark a class member as publicly accessible. Redefine the virtual function show() derived in the base class. The text to print on the console when the show() function defined in the derived class is invoked. End of the body of the show() function. End of the body of the derived class, ClassB. Call the main() function. The program logic should be added within its body. Create a pointer variable named a. It points to the class named ClassA. Create an instance of the class named ClassB. The instance is given the name b. Assign the values stores in the address b in the variable a. Invoke the show() function defined in the derived class. Late binding has been implemented. End of the body of the main() function.

Compile-Time Polymorphism Vs. Run-Time Polymorphism

Here are the major differences between the two:

Summary:

Polymorphism means to have many forms. It occurs when there is a hierarchy of classes related through inheritance. With polymorphism, a function can behave differently based on the object that invokes/calls it. In compile-time polymorphism, the function to be invoked is established during compile-time. In runtime polymorphism, the function to be invoked is established during runtime. Compile-time polymorphism is determined through function overloading and operator overloading. In function overloading, there are many functions with similar names but different arguments. The parameters can differ in number or type. In operator overloading, a new meaning is defined for C++ operators. Runtime polymorphism is achieved through function overriding. In function overriding, a derived class gives a new definition to a function defined in the base class.