0% found this document useful (0 votes)
11 views4 pages

CSC

A

Uploaded by

isiaktijani0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

CSC

A

Uploaded by

isiaktijani0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

What is Polymorphism and what is its significance?

How is Compile time & Run time


Polymorphism achieved?

Polymorphism is one of the most important concepts of Object-Oriented Programming


(OOPs). For a language considered to be an OOP language, it must support
polymorphism. You can describe the word polymorphism as an object having many
forms. Polymorphism is the notion that can hold up the ability of an object of a
class to show different responses. In other words, you can say that polymorphism is
the ability of an object to be represented in over one form.
To understand polymorphism, you can consider a real-life example. You can relate it
to the relationship of a person with different people. A man can be a father to
someone, a husband, a boss, an employee, a son, a brother, or can have many other
relationships with various people. Here, this man represents the object, and his
relationships display the ability of this object to be represented in many forms
with totally different characteristics.
Polymorphism in C++ is broadly classified into two categories based on their
characteristic features that you will explore further in this article. These
categories are:
• Compile-time Polymorphism
• Run-time Polymorphism

Why Do You Need to Use Polymorphism?


Polymorphism has several benefits associated with it. One of them is that it helps
you write the code consistently. For example, suppose you want to find the area of
a circle and a rectangle. For that, your first approach will be to create a base
class that will deal with the type of polygon. This base class contains two derived
classes, one for the circle and another one for the rectangle. Now, instead of
declaring two separate functions with separate names in both the derived classes to
calculate the area of each polygon, you can just declare one function in the base
class and override it in the child classes to calculate the area. In this way, you
increase the code consistency using polymorphism.

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

How is Compile time & Run time Polymorphism achieved?

The implementation of compile-time polymorphism is achieved in two ways:


• Function overloading
• Operator overloading

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

Operator overloading is sometimes referred to as ad-hoc polymorphism too. In


operator overloading, different operators display different implementations based
on their parameters or signatures. The C++ programming language allows you to
create operators that can serve specific purposes with user-defined functions. This
ability of C++ to create operators with a specific meaning for data types is termed
operator overloading.
A simple and common example of operator overloading is that when you use the “+”
operator in between two or more strings. It is used to join or concatenate them.
Some other use-cases of operator overloading are working with Fractional Numbers,
Complex Numbers, Big integers, etc.

The implementation of run time polymorphism can be achieved in two ways:


• Function overriding
• Virtual functions

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

B) With the help of an example, explain Exception Handling mechanism in detail?


Also what are the advantages of using Exception Handling in program

Exceptions handling are runtime anomalies or abnormal conditions that a program


encounters during its execution. There are two types of exceptions: a)Synchronous,
b)Asynchronous (i.e., exceptions which are beyond the program’s control, such as
disc failure, keyboard interrupts etc.). C++ provides the following specialized
keywords for this purpose:
try: Represents a block of code that can throw an exception.
catch: Represents a block of code that is executed when a particular exception is
thrown.
throw: Used to throw an exception. Also used to list the exceptions that a function
throws but doesn’t handle itself

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

cout << "Before try \n";

try {

cout << "Inside try \n";

if (x < 0)

throw x;

cout << "After throw (Never executed) \n";

catch (int x ) {
cout << "Exception Caught \n";

cout << "After catch (Will be executed) \n";

return 0;
}

Output:
Before try
Inside try
Exception Caught
After catch (Will be executed)

Advantages of using Exception Handling?


The following are the main advantages of exception handling over traditional error
handling:

1) Separation of Error Handling code from Normal Code: In traditional error


handling codes, there are always if-else conditions to handle errors. These
conditions and the code to handle errors get mixed up with the normal flow. This
makes the code less readable and maintainable. With try/catch blocks, the code for
error handling becomes separate from the normal flow.

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

You might also like