SlideShare a Scribd company logo
BIRLA INSTITUTE OF TECHNOLOGY
MESRA-RANCHI
JAIPUR CAMPUS
TOPIC:-
POLYMORPHISM AND TEMPLATES
SUBMITTED BY:
YASH SOGANI MCA/25025/18
ANUSHKA PAREEK MCA/25024/18
OVERVIEW
 POLYMORPHISM
• COMPILE TIME POLYMORPHISM
• RUNTIME POLYMORPHISM
 VIRTUAL FUNCTIONS
 ABSTRACT CLASSES
POLYMORPHISM
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.
In C++ polymorphism is mainly divided into two types:
1.Compile time Polymorphism
2.Runtime Polymorphism
COMPILE TIME POLYMORPHISM
This type of polymorphism is achieved by using
the 2 following ways:
1.Function overloading
2.Operator overloading
FUNCTION OVERLOADING
When there are multiple functions with same name
but different parameters then these functions are said
to be overloaded. Functions can be overloaded
by change in number of arguments or/and change in
type of arguments.
// C++ program for function overloading
#include <iostream.h>
class Geeks
{
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;
}
};
int main() {
Geeks obj1;
// Which function is called will depend on the parameters passed
// The first 'func' is called
obj1.func(7);
// The second 'func' is called
obj1.func(9.132);
// The third 'func' is called
obj1.func(85,64);
return 0;
}
OUTPUT:-
value of x is 7
value of x is 9.132
value of x and y is 85,64
OPERATOR OVERLOADING
C++ also provide option to overload operators. For example,
we can make the operator (‘+’) for string class to concatenate
two strings. We know that this is the addition operator whose
task is to add to operands. So a single operator ‘+’ when
placed between integer operands , adds them and when
placed between string operands it concatenates them.
RUNTIME POLYMORPHISM
This type of polymorphism is achieved by Function
Overriding.
FUNCTION OVERRIDING
It 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.
VIRTUAL FUNCTION
A virtual function is a function in a base class that is declared
using the keyword virtual. Defining in a base class a virtual
function, with another version in a derived class, signals to the
compiler that we don't want static linkage for this function.
What we do want is the selection of the function to be called at
any given point in the program to be based on the kind of
object for which it is called. This sort of operation is referred to
as dynamic linkage, or late binding.
// C++ program for function overriding
#include<iostream.h>
class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }
void show ()
{ cout<< "show base class" <<endl; }
};
class derived:public base
{
public:
void print () //print () is already virtual function in derived class,
we could also declared as virtual void print () explicitly
{ cout<< "print derived class" <<endl; }
void show ()
{ cout<< "show derived class" <<endl; }
};
//main function
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;
}
OUTPUT:-
print derived class
show base class
ABSTRACT CLASSES
An abstract class is a class that is designed to be specifically
used as a base class. It contains at least one Pure Virtual
function in it. Abstract classes are used to provide an Interface
for its sub classes. Classes inheriting an Abstract Class must
provide definition to the pure virtual function, otherwise they
will also become abstract class.
#include<iostream.h>
class Base
{
public:
virtual void fun() = 0;
};
// This class inherits from Base and implements fun()
class Derived: public Base
{
public:
void fun()
{
cout << “fun() called";
}
};
int main(void)
{
Derived d;
d.fun();
return 0;
}
OUTPUT:-
func() called
Templates
PRESENTED BY :
ANUSHKA PAREEK
MCA/25024/18
Templates?
 Templates are a feature of the C++
programming language that allow
functions and classes to operate with
generic types.
 This allows a function or class to work on
many different data types without being
rewritten for each one.
Types of Templates
C++ provides two types of templates :
 Class Templates
 Function Templates
Function Template
 Function templates are special functions that can operate with generic
types. This allows us to create a function template whose functionality can
be adapted to more than one type or class without repeating the entire
code for each type.
 These define the logic behind the algorithms that work for multiple data
types.
 In C++ , this can be achieved using template parameters.
Template Parameter & Syntax
 A template parameter is a special kind of parameter that can
be used to pass a type as argument: just like regular
function parameters can be used to pass values to function,
template parameter allow to pass values and types to a
function.
template < T0, T1… Tn>
Keyword Parameters
template <class T>
T min (T var1, T var2); // Returns a value of type T
Ex : Finding the max of two number
int maximum(int a, int b)
{
if (a > b)
return a;
else
return b;
}
int maximum(double a,
double b)
{
if (a > b)
return a;
else
return b;
}
Function with integer data type Function with double data type
C++ routines work on specific types. We often need to
write different routines to perform the same operation
on different data types.
Template Function
template <class Item>
Item maximum(Item a,
Item b)
{
if (a > b)
return a;
else
return b;
}
Function templates allow the logic to be
written once and used for all data types –
generic function.
It uses Item as its data type.
This function can now be used with any
data type depending on the value of a
& b passed.
#include <iostream.h>
using namespace std;
// One function works for all data types.
template <typename T>
T myMax(T x, T y)
{
return (x > y)? x: y;
}
int main()
{
cout << myMax<int>(3, 7) << endl; // Call myMax for int
cout << myMax<double>(3.0, 7.0) << endl; // call myMax for double
cout << myMax<char>('g', 'e') << endl; // call myMax for char
return 0;
}
CLASS TEMPLATES
 These define generic class patterns into which specific data
types can be plugged in to produce new classes.
 Class templates encourage software reusability by enabling
type-specific versions of generic classes to be instantiated.
Syntax
template <class T>
class className
{
... .. ...
public: T var;
T someOperation(T arg);
... .. ...
};
//Creating class template object
className<dataType> classObject;
#include <iostream>
using namespace std;
template <class T>
class Calculator //Calculator Program
{
private: T num1, num2;
public: Calculator(T n1, T n2)
{ num1 = n1; num2 = n2;
}
void displayResult()
{
cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
cout << "Addition is: " << add() << endl;
cout << "Subtraction is: " << subtract() << endl;
cout << "Product is: " << multiply() << endl;
cout << "Division is: " << divide() << endl;
}
T subtract()
{
return num1 - num2;
}
T multiply()
{
return num1 * num2;
}
T divide()
{
return num1 / num2;
}
};
int main()
{
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
cout << "Int results:" << endl;
intCalc.displayResult();
cout << endl << "Float results:" << endl;
floatCalc.displayResult();
return 0;
}
Thank you

More Related Content

PDF
Learn C# Programming Polymorphism & Operator Overloading
PPSX
Polymorphism
PPTX
Interoduction to c++
PPT
Static and dynamic polymorphism
PPTX
Polymorphism
PPT
Static and Dynamic polymorphism in C++
PDF
2nd puc computer science chapter 8 function overloading
PDF
379008-rc217-functionalprogramming
Learn C# Programming Polymorphism & Operator Overloading
Polymorphism
Interoduction to c++
Static and dynamic polymorphism
Polymorphism
Static and Dynamic polymorphism in C++
2nd puc computer science chapter 8 function overloading
379008-rc217-functionalprogramming

What's hot (20)

PPTX
Operator Overloading and Scope of Variable
PDF
Operator overloading
PPTX
07. Virtual Functions
PPTX
Pointers,virtual functions and polymorphism cpp
PPTX
pointers,virtual functions and polymorphism
PDF
Generics
PDF
Polymorphism in Java by Animesh Sarkar
PPTX
Java 8 - An Overview
PDF
Introduction to C++
PPT
JAVA Polymorphism
PDF
Kotlin- Basic to Advance
PPTX
Polymorphism Using C++
PPTX
Virtual function complete By Abdul Wahab (moon sheikh)
PPT
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
PPT
Oo Design And Patterns
PDF
Functions in C++
PPTX
Function different types of funtion
PPT
14 operator overloading
PPTX
Polymorphism in C++
PPT
C++ Interview Questions
Operator Overloading and Scope of Variable
Operator overloading
07. Virtual Functions
Pointers,virtual functions and polymorphism cpp
pointers,virtual functions and polymorphism
Generics
Polymorphism in Java by Animesh Sarkar
Java 8 - An Overview
Introduction to C++
JAVA Polymorphism
Kotlin- Basic to Advance
Polymorphism Using C++
Virtual function complete By Abdul Wahab (moon sheikh)
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Oo Design And Patterns
Functions in C++
Function different types of funtion
14 operator overloading
Polymorphism in C++
C++ Interview Questions
Ad

Similar to Polymorphism & Templates (20)

PPTX
Templates presentation
PPTX
Templates2
PDF
Object Oriented Programming using C++ - Part 5
PPTX
TEMPLATES IN JAVA
PPTX
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
PDF
4.1 C++ Template for engineering course. Learn easily
PPT
2CPP15 - Templates
PPTX
11.C++Polymorphism [Autosaved].pptx
PPTX
chapter 5 Templates-Introduction in C++.pptx
PDF
polymorphism for b.tech iii year students
PPTX
CSE107JAN2023_KMS_Intrnjononolo+CPP.pptx
PPTX
Multiple file programs, inheritance, templates
PDF
Object Oriented Programming (OOP) using C++ - Lecture 4
PPTX
C++ Basics
PPTX
Object Oriented Programming using C++: C++ Templates.pptx
PPTX
Templates1
PDF
PPTX
Oopsecondgrouppresentation 180726073512-converted (1)
PPTX
C++ Presen. tation.pptx
Templates presentation
Templates2
Object Oriented Programming using C++ - Part 5
TEMPLATES IN JAVA
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
4.1 C++ Template for engineering course. Learn easily
2CPP15 - Templates
11.C++Polymorphism [Autosaved].pptx
chapter 5 Templates-Introduction in C++.pptx
polymorphism for b.tech iii year students
CSE107JAN2023_KMS_Intrnjononolo+CPP.pptx
Multiple file programs, inheritance, templates
Object Oriented Programming (OOP) using C++ - Lecture 4
C++ Basics
Object Oriented Programming using C++: C++ Templates.pptx
Templates1
Oopsecondgrouppresentation 180726073512-converted (1)
C++ Presen. tation.pptx
Ad

More from Meghaj Mallick (20)

PPT
24 partial-orderings
PPTX
PORTFOLIO BY USING HTML & CSS
PPTX
Introduction to Software Testing
PPTX
Introduction to System Programming
PPTX
MACRO ASSEBLER
PPTX
Icons, Image & Multimedia
PPTX
Project Tracking & SPC
PPTX
Peephole Optimization
PPTX
Routing in MANET
PPTX
Macro assembler
PPTX
Architecture and security in Vanet PPT
PPTX
Design Model & User Interface Design in Software Engineering
PPTX
Text Mining of Twitter in Data Mining
PPTX
DFS & BFS in Computer Algorithm
PPTX
Software Development Method
PPTX
Secant method in Numerical & Statistical Method
PPTX
Motivation in Organization
PPTX
Communication Skill
PPT
Partial-Orderings in Discrete Mathematics
PPTX
Hashing In Data Structure
24 partial-orderings
PORTFOLIO BY USING HTML & CSS
Introduction to Software Testing
Introduction to System Programming
MACRO ASSEBLER
Icons, Image & Multimedia
Project Tracking & SPC
Peephole Optimization
Routing in MANET
Macro assembler
Architecture and security in Vanet PPT
Design Model & User Interface Design in Software Engineering
Text Mining of Twitter in Data Mining
DFS & BFS in Computer Algorithm
Software Development Method
Secant method in Numerical & Statistical Method
Motivation in Organization
Communication Skill
Partial-Orderings in Discrete Mathematics
Hashing In Data Structure

Recently uploaded (20)

PPTX
Research Process - Research Methods course
PPTX
Lesson-7-Gas. -Exchange_074636.pptx
PDF
PM Narendra Modi's speech from Red Fort on 79th Independence Day.pdf
PPTX
power point presentation ofDracena species.pptx
PPTX
PHIL.-ASTRONOMY-AND-NAVIGATION of ..pptx
PPTX
Introduction-to-Food-Packaging-and-packaging -materials.pptx
PPTX
Phylogeny and disease transmission of Dipteran Fly (ppt).pptx
PPTX
Shizophrnia ppt for clinical psychology students of AS
PPTX
chapter8-180915055454bycuufucdghrwtrt.pptx
DOC
LSTM毕业证学历认证,利物浦大学毕业证学历认证怎么认证
PPTX
HOW TO HANDLE THE STAGE FOR ACADEMIA AND OTHERS.pptx
PPTX
FINAL TEST 3C_OCTAVIA RAMADHANI SANTOSO-1.pptx
PPTX
CAPE CARIBBEAN STUDIES- Integration-1.pptx
PPTX
Anesthesia and it's stage with mnemonic and images
PDF
Presentation1 [Autosaved].pdf diagnosiss
PDF
Yusen Logistics Group Sustainability Report 2024.pdf
PPTX
Sustainable Forest Management ..SFM.pptx
PPTX
Bob Difficult Questions 08 17 2025.pptx
PDF
public speaking for kids in India - LearnifyU
PPTX
Module_4_Updated_Presentation CORRUPTION AND GRAFT IN THE PHILIPPINES.pptx
Research Process - Research Methods course
Lesson-7-Gas. -Exchange_074636.pptx
PM Narendra Modi's speech from Red Fort on 79th Independence Day.pdf
power point presentation ofDracena species.pptx
PHIL.-ASTRONOMY-AND-NAVIGATION of ..pptx
Introduction-to-Food-Packaging-and-packaging -materials.pptx
Phylogeny and disease transmission of Dipteran Fly (ppt).pptx
Shizophrnia ppt for clinical psychology students of AS
chapter8-180915055454bycuufucdghrwtrt.pptx
LSTM毕业证学历认证,利物浦大学毕业证学历认证怎么认证
HOW TO HANDLE THE STAGE FOR ACADEMIA AND OTHERS.pptx
FINAL TEST 3C_OCTAVIA RAMADHANI SANTOSO-1.pptx
CAPE CARIBBEAN STUDIES- Integration-1.pptx
Anesthesia and it's stage with mnemonic and images
Presentation1 [Autosaved].pdf diagnosiss
Yusen Logistics Group Sustainability Report 2024.pdf
Sustainable Forest Management ..SFM.pptx
Bob Difficult Questions 08 17 2025.pptx
public speaking for kids in India - LearnifyU
Module_4_Updated_Presentation CORRUPTION AND GRAFT IN THE PHILIPPINES.pptx

Polymorphism & Templates

  • 1. BIRLA INSTITUTE OF TECHNOLOGY MESRA-RANCHI JAIPUR CAMPUS TOPIC:- POLYMORPHISM AND TEMPLATES SUBMITTED BY: YASH SOGANI MCA/25025/18 ANUSHKA PAREEK MCA/25024/18
  • 2. OVERVIEW  POLYMORPHISM • COMPILE TIME POLYMORPHISM • RUNTIME POLYMORPHISM  VIRTUAL FUNCTIONS  ABSTRACT CLASSES
  • 3. POLYMORPHISM 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. In C++ polymorphism is mainly divided into two types: 1.Compile time Polymorphism 2.Runtime Polymorphism
  • 4. COMPILE TIME POLYMORPHISM This type of polymorphism is achieved by using the 2 following ways: 1.Function overloading 2.Operator overloading
  • 5. FUNCTION OVERLOADING When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.
  • 6. // C++ program for function overloading #include <iostream.h> class Geeks { 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; }
  • 7. // function with same name and 2 int parameters void func(int x, int y) { cout << "value of x and y is " << x << ", " << y << endl; } }; int main() { Geeks obj1; // Which function is called will depend on the parameters passed // The first 'func' is called obj1.func(7); // The second 'func' is called obj1.func(9.132); // The third 'func' is called obj1.func(85,64); return 0; }
  • 8. OUTPUT:- value of x is 7 value of x is 9.132 value of x and y is 85,64
  • 9. OPERATOR OVERLOADING C++ also provide option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add to operands. So a single operator ‘+’ when placed between integer operands , adds them and when placed between string operands it concatenates them.
  • 10. RUNTIME POLYMORPHISM This type of polymorphism is achieved by Function Overriding. FUNCTION OVERRIDING It 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.
  • 11. VIRTUAL FUNCTION A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function. What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding.
  • 12. // C++ program for function overriding #include<iostream.h> class base { public: virtual void print () { cout<< "print base class" <<endl; } void show () { cout<< "show base class" <<endl; } };
  • 13. class derived:public base { public: void print () //print () is already virtual function in derived class, we could also declared as virtual void print () explicitly { cout<< "print derived class" <<endl; } void show () { cout<< "show derived class" <<endl; } };
  • 14. //main function 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; }
  • 16. ABSTRACT CLASSES An abstract class is a class that is designed to be specifically used as a base class. It contains at least one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.
  • 17. #include<iostream.h> class Base { public: virtual void fun() = 0; }; // This class inherits from Base and implements fun() class Derived: public Base { public: void fun() { cout << “fun() called"; } };
  • 18. int main(void) { Derived d; d.fun(); return 0; } OUTPUT:- func() called
  • 19. Templates PRESENTED BY : ANUSHKA PAREEK MCA/25024/18
  • 20. Templates?  Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types.  This allows a function or class to work on many different data types without being rewritten for each one.
  • 21. Types of Templates C++ provides two types of templates :  Class Templates  Function Templates
  • 22. Function Template  Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.  These define the logic behind the algorithms that work for multiple data types.  In C++ , this can be achieved using template parameters.
  • 23. Template Parameter & Syntax  A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to function, template parameter allow to pass values and types to a function. template < T0, T1… Tn> Keyword Parameters template <class T> T min (T var1, T var2); // Returns a value of type T
  • 24. Ex : Finding the max of two number int maximum(int a, int b) { if (a > b) return a; else return b; } int maximum(double a, double b) { if (a > b) return a; else return b; } Function with integer data type Function with double data type C++ routines work on specific types. We often need to write different routines to perform the same operation on different data types.
  • 25. Template Function template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; } Function templates allow the logic to be written once and used for all data types – generic function. It uses Item as its data type. This function can now be used with any data type depending on the value of a & b passed.
  • 26. #include <iostream.h> using namespace std; // One function works for all data types. template <typename T> T myMax(T x, T y) { return (x > y)? x: y; } int main() { cout << myMax<int>(3, 7) << endl; // Call myMax for int cout << myMax<double>(3.0, 7.0) << endl; // call myMax for double cout << myMax<char>('g', 'e') << endl; // call myMax for char return 0; }
  • 27. CLASS TEMPLATES  These define generic class patterns into which specific data types can be plugged in to produce new classes.  Class templates encourage software reusability by enabling type-specific versions of generic classes to be instantiated.
  • 28. Syntax template <class T> class className { ... .. ... public: T var; T someOperation(T arg); ... .. ... }; //Creating class template object className<dataType> classObject;
  • 29. #include <iostream> using namespace std; template <class T> class Calculator //Calculator Program { private: T num1, num2; public: Calculator(T n1, T n2) { num1 = n1; num2 = n2; } void displayResult() { cout << "Numbers are: " << num1 << " and " << num2 << "." << endl; cout << "Addition is: " << add() << endl; cout << "Subtraction is: " << subtract() << endl; cout << "Product is: " << multiply() << endl; cout << "Division is: " << divide() << endl; }
  • 30. T subtract() { return num1 - num2; } T multiply() { return num1 * num2; } T divide() { return num1 / num2; } };
  • 31. int main() { Calculator<int> intCalc(2, 1); Calculator<float> floatCalc(2.4, 1.2); cout << "Int results:" << endl; intCalc.displayResult(); cout << endl << "Float results:" << endl; floatCalc.displayResult(); return 0; }