Course Title: Object –Oriented Programming
Semester: 1st (Fall 2026)
Department: Computer Science
Instructor’s Name: Mr. Shabazz Hussain
Student’s Name: Muhammad Zain ul Abideen
Registration Number:BS-AI-B-34
Section: Morning (B)
Program: BS AI
Muhammad Nawaz Sharif University of engineering and technology
1. Explanation
Generic Programming is a programming technique that allows writing code that
works with different data types using a single implementation.
In C++, this is done using templates.
Instead of writing separate classes or functions for int, float, double, etc., we use a
template with a placeholder type (usually T).
C++ supports generic programming through templates, which are the foundation of
the Standard Template Library (STL).
When the program runs, the compiler replaces T with the actual data type.
This reduces:
Code duplication
Errors
Development time
2. Objectives of Generic Programming
The main objectives are:
Code Reusability – Write once, use for many data types.
Type Safety – Errors are checked at compile time.
Efficiency – No need to rewrite similar logic.
Flexibility – Works with user-defined and built-in data types.
Maintainability – Easier to manage and update code.
3. What is Generic Programming?
Generic programming is a method of writing programs in a general form so that
the same code works for different data types.
4. What is a Template?
A template is a blueprint or formula for creating generic functions and classes.
There are two types of templates in C++:
1. Function Template
2. Class Template
Templates are defined using:
template <typename T> or template <class T>
Function Template
Explanation:
A function template allows a function to work with different data types without
rewriting the code.
Code:
include <iostream>
using namespace std;
class Demo {
public:
template <typename T>
T add(T a, T b) {
return a + b;
}
};
int main() {
Demo obj; #
cout << "Integer Addition: " << [Link](10, 20) << endl;
cout << "Double Addition: " << [Link](5.8, 2.5) << endl;
cout << "Float Addition: " << [Link](3.7f, 1.8f) << endl;
return 0;
}
Result:
Class Template (Implementation Using Class and Object)
Explanation:
A class template allows a class to handle data of any type. Objects are created by
specifying the data type at the time of object creation.
Code:
#include <iostream>
using namespace std;
template <class T>
class Maximum {
T a, b;
public:
Maximum(T x, T y) {
a = x;
b = y;
}
T findMax() {
return (a > b) ? a : b;
}
};
int main() {
Maximum<int> obj1(10, 20);
cout << "Maximum (int): " << [Link]() << endl;
Maximum<double> obj2(5.5, 2.3);
cout << "Maximum (double): " << [Link]() << endl;
return 0;
}
Result:
5. Conclusion:
Generic Programming in C++ is a powerful concept that allows developers to write
flexible, reusable, and efficient code using templates. Templates enable both
functions and classes to operate on different data types without rewriting code. It
improves performance because all type checking happens at compile time. The
Standard Template Library (STL) is the best example of generic programming in
real-world C++ applications.
Generic programming is widely used in modern C++ software development and is
an important concept for building efficient and maintainable programs.