Lec_16 (Templates in Object Oriented Programming).pptx
1.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Women University of Azad Jammu and Kashmir Bagh
Department of Computer Sciences and Information Technology, Women University of Azad Jammu and Kashmir Bagh
www.wuajk.edu.pk
Object Oriented Programming
CS-5201
Dr. Anees Qumar Abbasi
aneesabbasi.wuajk@gmail.com
2.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Templates in C++
SLOs
• Explain the concept of generic programming and the role of templates
in achieving type-independent code reuse.
• Differentiate between function templates and class templates with
examples.
• Implement function and class templates to perform operations on
multiple data types without code duplication.
• Analyze the advantages of templates in terms of flexibility,
maintainability, and performance within object-oriented design.
3.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Generic Programming
• Generic Programming is a programming method that focuses on writing code in
a type-independent manner.
• It allows the same function or class to work with different data types without
rewriting the logic.
• Instead of writing separate versions of a function for int, float, and double, we
write one generic version using a template parameter.
• The compiler automatically generates type-specific code at compile time based
on the data type used.
• Promote code reusability and flexibility.
• Eliminate redundancy in programs.
• Provide type safety — the compiler ensures type consistency for every instance.
• Example Concept:
A single sorting or swapping algorithm can be written once and used for
any data type (integers, floats, strings, etc.) by using templates.
4.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
What Are Templates?
• Templates allow functions and classes to operate with generic types.
• Enable writing type-independent code.
• Help in reducing code duplication.
• Provide the foundation of generic programming in C++.
• Used extensively in Standard Template Library (STL).
Advantages of Templates:
• Code reusability — one version works for many data types.
• Type safety — compiler checks type compatibility.
• Maintainability — less duplication, easier updates.
• Performance — resolved at compile-time (no runtime cost).
5.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Types of Templates
Templates
Function Templates
[Generic Functions]
Class Templates
[Generic Classes]
6.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Function Templates
• Function templates allow you to create a single function that
can operate on different data types.
• The function logic is written once, and the compiler
automatically generates type-specific versions as needed.
• Replace function overloading when operations are identical for
multiple data types.
• Syntax:
template <class T>
T functionName(T parameter1, T parameter2);
- template and class are keywords, T is name of tempplate
7.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Function Templates-Example
template <class T>
T Max(T val1, T val2)
{
return (val1 > val2) ? val1 : val2;
}
• Works for any type: int, float, char, etc.
• Compiler generates specific versions
automatically:
int Max(int, int)
float Max(float, float)
char Max(char, char)
8.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Declaring Multiple Type Parameters
• A template can have more than one type parameter.
• Example:
template <class T1, class T2>
void display(T1 a, T2 b)
{
cout << a << " " << b;
}
• Allows mixing of data types (e.g., display(5, 'A')).
9.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Function Templates-Example
int main()
{
int n;
float m;
char c;
n=Min(20,35);
m=Min(42.5, 23.8);
c=Min(‘x’, ‘y’);
cout<<”Minimum of Two Integers is:”<<n;
cout<<”Minimum of Two Float is:”<<m;
cout<<”Minimum of Two Character is:”<<c;
return 0;
}
Program: Write a program that
accepts two values and displays
minimum of them.
#include <iostream>
#include <conio.h>
template <class Type>
Type Min (Type a, Type b)
{
if(a<b)
Return a;
else
Return b;
}
10.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Class Templates
• Class templates allow you to create a class that works with
different data types without rewriting the class code.
• They enable defining generic classes, where the type of data
members, function parameters, or return values can be
parameterized using a template type.
• Purpose is to make data structures and classes reusable for
any data type (e.g., int, float, string, or even user-defined types).
• Avoids duplicating class code for each data type.
• Forms the foundation of C++ Standard Template Library (STL)
containers like vector<T> and list<T>.
11.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Class Templates- Example
template <class Type>
class Test
{
private:
Type arr[5];
public:
void input();
void output();
};
When Creating objects:
• Test<int> x → creates integer array.
• Test<char> y → creates character array.
• Each object behaves as a separate class for that data type.
• Promotes code reuse and flexibility.
12.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Class Templates- Example
template <class Type>
class Test
{
private:
Type arr[5];
public:
void input()
{
for (int i=0;i<5;i++)
{
cout<<”Enter Value”;
cin>>arr[i];
cout<<endl;
}
}
void output()
{
cout<<” Values in the Array”;
for (int i=0;i<5;i++)
{
cout<<arr[i]<<”t”;
}
cout<<endl;
}
};
int main()
{
Test <int> x;
Test<char> y;
cout<<”Enter Five Integers:”<<endl;
x.input();
cout<<”Enter Five Characters”<<endl;
y.input();
x.output();
y.output();
return 0;
}
13.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Templates and Reusability
• Templates simplify software design and reduce redundancy.
• Enable implementation of data structures and algorithms that are type-
independent.
• Eliminate the need to rewrite the same class or function for each data type.
• Simplify software design by focusing on logic, not data type.
• Improve consistency — when logic changes, it only needs to be updated
once.
• Enhance productivity and reduce development time for large-scale projects.
• Used widely in C++ STL containers such as:
vector<T> → Dynamic array for any data type
list<T> → Doubly linked list
map<K, V> → Key-value associative container
stack<T>, queue<T>, set<T>, etc.
14.
Department of ComputerSciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Key Take Aways
• Templates = Core of Generic Programming in C++.
• Function templates → generalize functions.
• Class templates → generalize classes.
• Help build efficient, reusable, type-safe code.