Open Source Camp Kubernetes 2024 | Running WebAssembly on Kubernetes by Alex ...
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
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;
}
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.
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;
}
};