CSC
CSC
Types of Polymorphism
Based on the functionality, you can categorize polymorphism into two types:
• Compile-Time Polymorphism
When the relationship between the definition of different functions and their
function calls, is determined during the compile-time, it is known as compile-time
polymorphism. This type of polymorphism is also known as static or early binding
polymorphism. All the methods of compile-time polymorphism get called or invoked
during the compile time.
You can implement compile-time polymorphism using function overloading and operator
overloading. Method/function overloading is an implementation of compile-time
polymorphism where the same name can be assigned to more than one method or
function, having different arguments or signatures and different return types.
Compile-time polymorphism has a much faster execution rate since all the methods
that need to be executed are called during compile time. However, it is less
preferred for handling complex problems since all the methods and details come to
light only during the compile time. Hence, debugging becomes tougher.
• Runtime Polymorphism
In runtime polymorphism, the compiler resolves the object at run time and then it
decides which function call should be associated with that object. It is also known
as dynamic or late binding polymorphism. This type of polymorphism is executed
through virtual functions and function overriding. All the methods of runtime
polymorphism get invoked during the run time.
Method overriding is an application of run time polymorphism where two or more
functions with the same name, arguments, and return type accompany different
classes of the same structure. This method has a comparatively slower execution
rate than compile-time polymorphism since all the methods that need to be executed
are called during run time. Runtime polymorphism is known to be better for dealing
with complex problems since all the methods and details turn up during the runtime
itself.
The implementation of run time polymorphism can be achieved in two ways:
• Function overriding
• Virtual functions
Function Overloading
Function overloading is an important pillar of polymorphism in C++. When the same
function name is overloaded with multiple tasks, that function gets overloaded. In
other words, function overloading takes place when you create two functions of the
same name and these functions serve different purposes. Both functions should have
the same return types and strictly different arguments from each other. It is
achieved during the compile time. Function overloading can take place both in the
base class as well as in the derived class.
All the functions that have been overloaded share the same scope. Since it is
achievable in both the base class and the derived class, you do not specifically
need inheritance.
Operator Overloading
Function Overriding
Function overriding takes place when your derived class and base class both contain
a function having the same name. Along with the same name, both the functions
should have the same number of arguments as well as the same return type. The
derived class inherits the member functions and data members from its base class.
So to override a certain functionality, you must perform function overriding. It is
achieved during the run time.
Functions that are overridden acquire different scopes. For function overriding,
inheritance is a must. It can only happen in a derived class. If a class is not
inherited from another class, you can not achieve function overriding. To sum up, a
function is overridden when you want to achieve a task supplementary to the base
class function.
Virtual Functions
Virtual functions are the member functions of the base class which are overridden
in a derived class. When you make a call to such a function by using a pointer or
reference to the base class, the virtual function is called and the derived class’s
version of the function gets invoked.
Some Key Points About Virtual Functions
• Virtual functions are only dynamic
• They are declared within a base class by using the keyword “virtual”
• These functions are called during run time
• Virtual functions are always declared with a base class and overridden
in a child class
• Virtual functions can exist in the absence of inheritance. In that
case, the base class version of the function will be called
The following is a simple example to show exception handling in C++. The output of
the program explains the flow of execution of try/catch blocks.
include <iostream>
using namespace std;
int main()
{
int x = -1;
// Some code
try {
if (x < 0)
throw x;
catch (int x ) {
cout << "Exception Caught \n";
return 0;
}
Output:
Before try
Inside try
Exception Caught
After catch (Will be executed)
2) Functions/Methods can handle only the exceptions they choose: A function can
throw many exceptions, but may choose to handle some of them. The other exceptions,
which are thrown but not caught, can be handled by the caller. If the caller
chooses not to catch them, then the exceptions are handled by the caller of the
caller.
In C++, a function can specify the exceptions that it throws using the throw
keyword. The caller of this function must handle the exception in some way (either
by specifying it again or catching it).
3) Grouping of Error Types: In C++, both basic types and objects can be thrown as
exceptions. We can create a hierarchy of exception objects, group exceptions in
namespaces or classes and categorize them according to their types.
Source https://www.geeksforgeeks.org/exception-handling-c/amp/
https://www.simplilearn.com/tutorials/cpp-tutorial/polymorphism-in-cpp