Templates: Code Sharing (Genericity)
Templates: Code Sharing (Genericity)
(Genericity)
INTRODUCTION TO CODE SHARING
There are many programming situations where
same set of operations are applied on different data
types.
temp = oldVal;
oldVal = newVal;
newVal = temp;
}
Code-sharing is implemented in C++ through
templates.
temp = oldVal;
oldVal = newVal;
newVal = temp;
}
1. We have used a generic type called ‘myType’ to declare
the variables in the generic function ‘exchange’.
2. This generic type (myType) would get substituted at run
time by whichever type of data is employed by the
programmer.
3. Thus, templates use run time polymorphism to bind the
data with their types.
GENERIC CLASSES
Similar to functions, we can also create
generic classes in the form of class
templates.