Faculty of Engineering & Faculty of Management (Polytechnic Wing)
Faculty of Engineering & Faculty of Management (Polytechnic Wing)
MANAGEMENT
(Polytechnic wing)
A Microproject Report On
“Polymorphism and their types”
Submitted By
Guided By
Ms.Nikam S.V.
Dr. S.R.Pawaskar
Principal
Date – / /202
Place – Talsande
INDEX
II Advantages and 3
disadvantages
V Difference 10
VI Result 11
VII Conclusion 12
VIII Reference 13
Polymorphism and their types
What is Polymorphism: -
Polymorphism is the concept with the help of which single action in different
ways can be performed. It is derived from two Greek words: poly and morphs.
“poly” means many, and “morphs” means forms. Henceforth, polymorphism
implies many forms. Object-oriented programming can be defined as a
programing language ‘s ability to process objects differently depending on their
class or data type. Basically, we can define it as the ability forderived classes to
redefine methods.
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 1
Polymorphism and their types
When a class has multiple methods that have the same name but different
only one operation, then having the same name as the methods would increase the
When a child class has the same method as one declared in the parent class,
that its parentclass has declared, it is called method overriding. Certain things
The method must have the same name as one mentioned in the parent class
The method must also have the same parameter as the one mentioned
in the parentclass.
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 2
Polymorphism and their types
It helps programmers reuse the code and the classes that are
once writtento be tested and implemented. (Reusability of
code)
A single variable name can store variables of multiple
data types (int,float, double, long, etc.).
Increases the readability of the program.
Run time polymorphism can lead to the performance issue as machine needs
to decidewhich method or variable to invoke so it basically degrades the
performances as decisions are taken at run time.
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 3
Polymorphism and their types
Compile-time Polymorphism
Run-time polymorphism
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 4
Polymorphism and their types
The overloaded functions are invoked by matching the type and number of
arguments. This information is available at the compile time and, therefore,
compiler selects the appropriate function at the compile time. It is achieved by
function overloading and operator overloadingwhich is also known as static
binding or early binding. Now, let's consider the case where function name and
prototype are same.
Function Overloading: -
When there are multiple functions with the same name but different parameters, then
the functions are said to be overloaded, hence this is known as Function Overloading.
Functions can be overloaded by changing the number of arguments or/and changing
the type of arguments. In simple terms, it is a feature of object-oriented programming
providing many functions to have the same name but distinct parameters when
numerous tasks are listed under one function name. There are certain Rules of
Function Overloading that should be followed while overloading a function.
Example1: -
Class a
{
Int a;
Public:
Void input()
{
Cout<<”Enter the value of A”;
}
Void show()
{
Cout<<a;
}
};
Class b: public a
{
Int b;
Public:
Void input1()
{
Cout<<”Enter the value of B”;
}
Void show()
{
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 5
Polymorphism and their types
Cout<<b;
}
};
Void main()
{
A ob;
B ob1;
Ob1.input();
Ob1.show();
Ob1.inpit1();
Ob1.show1();
Getch();
Return o;
}
Example 2: -
#include <bits/stdc++.h>
Class a
{
Public:
{
// Function with 1 int parameter
void func(int x)
{
cout << "value of x is " <<x
<< endl;
}
// Function with same name but
// 1 double parameter
void func(double x)
{
cout << "value of x is " <<x
<< endl;
}
// Function with same name and
// 2 int parameters
void func(int x, int y)
{
cout << "value of x and y is " <<x
<< ", " << y << endl;
}
};
// Driver code
int main()
{
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 6
Polymorphism and their types
Geeks obj1;
// Function being called depends
// on the parameters passed
// func() is called with int value
obj1.func(7);
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 7
Polymorphism and their types
Run time polymorphism is achieved when the object's method is invoked at the
runtime instead of compile time. It is achieved by method overriding which is also
known as dynamic binding orlate binding.
Function Overriding: -
Function Overriding occurs when a derived class has a definition for one of the
member functions of the base class. That base function is said to be overridden.
Example1: -
class base {
public:
virtual void print()
{
cout << "print base class" <<endl;
}
void show()
{
cout << "show base class" <<endl;
}
};
void show()
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 8
Polymorphism and their types
{
cout << "show derived class" <<endl;
}
};
// Driver code
int main()
{
base* bptr;
derived d;
bptr = &d;
return 0;
}
Example2: -
C++ Program to demonstrate
// the Virtual Function
#include <iostream>
using namespace std;
public:
// virtual function
virtual void display()
{
o cout << "Called virtual Base Class function" <<
"\n\n";
}
void print()
{
o cout << "Called GFG_Base print function" <<
"\n\n";
}
};
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 9
Polymorphism and their types
public:
void display()
{
o cout << "Called GFG_Child Display Function" <<
"\n\n";
}
void print()
{
o cout << "Called GFG_Child print Function" <<
"\n\n";
}
};
// Driver code
int main()
{
// Create a reference of class birdGFG_Base* base;
GFG_Child child;
base = &child;
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 10
Polymorphism and their types
The function to be invoked is known at the compile The function to be invoked is known at the run
time. time.
It is also known as overloading, early binding and It is also known as overriding, Dynamic
static binding. binding and late binding.
Overloading is a compile time polymorphism where Overriding is a run time polymorphism where
more than one method is having the same name but more than one method is having the same
with the different number of parameters or the type name, number of parameters and the type of
of the parameters. the parameters.
It is achieved by function overloading and operator It is achieved by virtual functions and pointers.
overloading.
It provides fast execution as it is known at the It provides slow execution as it is known at the
compile time. run time.
It is less flexible as mainly all the things execute at It is more flexible as all the things execute at
the compile time. the run time.
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 11
Polymorphism and their types
Result: -
The word polymorphism means having many forms. Typically, polymorphism occurs when
there is a hierarchy of classes and they are related by inheritance.
C++ polymorphism means that a call to a member function will cause a different function to
be executed depending on the type of object that invokes the function.
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 12
Polymorphism and their types
Conclusion: -
The word “polymorphism” means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form.
A real-life example of polymorphism is a person who at the same time can have different
characteristics. A man at the same time is a father, a husband, and an employee. So the same
person exhibits different behaviour in different situations. This is called polymorphism.
Polymorphism is considered one of the important features of Object-Oriented Programming.
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 13
Polymorphism and their types
Reference: -
https://www.geeksforgeeks.org/cpp-polymorphism/
https://www.scaler.com/topics/types-of-polymorphism-in-cpp/
https://www.topper.co.in/topics/cpp/polymorphism-in-cpp/
https://www.javatpoint.com/cpp-polymorphism
https://www.javatpoint.com/types-of-polymorphism-in-cpp
D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 14