0% found this document useful (0 votes)
15 views17 pages

Faculty of Engineering & Faculty of Management (Polytechnic Wing)

Uploaded by

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

Faculty of Engineering & Faculty of Management (Polytechnic Wing)

Uploaded by

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

FACULTY OF ENGINEERING & FACULTY OF

MANAGEMENT
(Polytechnic wing)

A Microproject Report On
“Polymorphism and their types”

Submitted By

Enrollement No. Name Of Student


2112200048 Soham Govind Kulkarni
2112200050 Aditya Uttam Patil
2112200051 Sanskar Mandar Kulkarni

Guided By
Ms.Nikam S.V.

D.Y. PATIL TECHANICAL CAMPUS, TALSANDE FACULTY


OF ENGINEERING & FACULTY OF MANAGEMENT
(Polytechnic wing)

DEPARTMENT OF COMPUTER ENGINEERING


SEMESTER III
CERTIFICATE
This is Certify that student of Computer Engineering has
successfully completed the project term work “Polymorphism and their
types” in partial fulfillment of the Diploma of Engineering in Computer
as laid down during academic year 2022-23.

Roll No. Name of Student Exam Seat No.


2235 Soham Govind Kulkarni 214493
2237 Aditya Uttam Patil 214495

2238 Sanskar Mandar Kulkarni 214496

Ms. Nikam S.V. Mr. Kumbhar R.S.


Project Guide HoD

Dr. S.R.Pawaskar
Principal

Date – / /202
Place – Talsande
INDEX

Sr.NO Title Page no.

I Introduction and 1-2


working

II Advantages and 3
disadvantages

III Types and diagram 4

IV Information and 5-9


Examples

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.

How does polymorphism make working so easy: -


It is an object-oriented programming characteristic. Using Polymorphism, a class
can exhibit different functionalities even when they have a common interface.
Thus, we can say that this long term explains a basic concept. The noteworthy
thing about polymorphism is that all the working code in various classes does not
require knowing about the class being used by it as their way of usage is the same.
It is an object-oriented programming characteristic. Using Polymorphism, a class
can exhibit different functionalities even when they have a common interface.
Thus, we can say that this long term explains a very basic concept. The
noteworthything about it is that all the working code in various classes does not
require to know about the class being used by it as their way of usage is the same.
Consider a real-world scenario for polymorphism. Take an example of a button.
We know that we can click the button by applying some pressure, but we do not
know the output of pressing the button or the reference of its use. The point here
to note is that the result would not affect the way it is usedeither way So the basic
goal of it is to make objects which are interchangeable depending on the needs.

D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 1
Polymorphism and their types

Working with polymorphism: -

When a class has multiple methods that have the same name but different

parameters, it is referred to as Method Overloading, suppose we have to perform

only one operation, then having the same name as the methods would increase the

readability of the program. Ways tooverload the method in Java is:

 Changing the number of arguments

 Changing the data type

What can you do with polymorphism: -

When a child class has the same method as one declared in the parent class,

we call it amethod overriding.

Or in other words, if a child class provides the implementation of the method

that its parentclass has declared, it is called method overriding. Certain things

to remember in method overriding are:

 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.

 There must be the inheritance, that is, the IS-A relationship.

D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 2
Polymorphism and their types

Advantages about Polymorphism: -

 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.

Disadvantages about polymorphism: -

 One of the disadvantages of polymorphism is that developers find it


difficult toimplement polymorphism in codes.

 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.

 Polymorphism reduces the readability of the program. One needs to


identify theruntime behaviour of the program to identify actual
execution time.

D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 3
Polymorphism and their types

What are the types of Polymorphism: -

There is main two types of polymorphism

 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

Compile Time Polymorphism: -

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);

 // func() is called with double value


obj1.func(9.132);
 // func() is called with 2 int values
obj1.func(85, 64);
 return 0;
 }

D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 7
Polymorphism and their types

Run time polymorphism: -

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: -

 C++ program for function overriding


 #include <bits/stdc++.h>
 using namespace std;

 class base {
 public:
 virtual void print()
 {
cout << "print base class" <<endl;
 }

 void show()
 {
cout << "show base class" <<endl;
 }
 };

 class derived : public base {


 public:

 // print () is already virtual function in


 // derived class, we could also declared as
 // virtual void print () explicitly
 void print()
 {
cout << "print derived 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;

 // Virtual function, binded at


 // runtime (Runtime polymorphism)
 bptr->print();

 // Non-virtual function, binded


 // at compile time
 bptr->show();

 return 0;
 }

Example2: -
 C++ Program to demonstrate
 // the Virtual Function
 #include <iostream>
 using namespace std;

 // Declaring a Base class


 class GFG_Base {

 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

 // Declaring a Child Class


 class GFG_Child : public GFG_Base {

 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;

 // This will call the virtual function


 base->GFG_Base::display();

 // this will call the non-virtual function


 base->print();
 }

D.Y.Patil technical campus talsande Faculty of engineering and faculty of management Page 10
Polymorphism and their types

Differences between compile time and run time polymorphism: -

Compile time polymorphism Run time polymorphism

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

You might also like