0% found this document useful (0 votes)
17 views18 pages

Oop Presentation

The document provides an overview of templates and generic programming, explaining their purpose in creating type-agnostic code to avoid duplication and enhance type safety. It covers function and class templates, including syntax, instantiation, specialization, and advanced features like default arguments and member function templates. Additionally, it discusses variable templates, non-type template parameters, template metaprogramming, best practices, and real-world applications such as STL containers and smart pointers.

Uploaded by

mtahir97345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

Oop Presentation

The document provides an overview of templates and generic programming, explaining their purpose in creating type-agnostic code to avoid duplication and enhance type safety. It covers function and class templates, including syntax, instantiation, specialization, and advanced features like default arguments and member function templates. Additionally, it discusses variable templates, non-type template parameters, template metaprogramming, best practices, and real-world applications such as STL containers and smart pointers.

Uploaded by

mtahir97345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Title: Templates and generic programing

Subtitle: From Basics to Advanced


Generic Programming

Group Members:
Muhammad Tahir : university roll number 1301-241019

Musadiq Ijaz : university roll number 1301- 241030

Hassan Aslam :university roll number 1303-241029

Muhammad Jawad : university roll number 1303-241035

Date: 10 May 2025


Introduction to Templates
What?:

Blueprints for generating type-agnostic code.

Why?:

Avoid code duplication (write once, use for any type).

Type safety (better than macros/void pointers).


Analogy:

Like a cookie cutter shaping dough (template) into cookies


(type-specific code).
Function Templates (Deep Dive)

Syntax:

template <typename T> // 'typename' or 'class' keyword


T add(T a, T b) { return a + b;

Key concepts;

1-Template Instantiation:

cout << add<int>(3, 5); // Explicit: T = int


cout << add(3.5, 2.1); // Implicit: T = double

2-Multiple Types:

template <typename T1, typename T2>


void printPair(T1 a, T2 b) { cout << a << ", " << b; }

3-Template Specialization:

template <>
const char* add(const char* a, const char* b) {
return strcat(a, b); // Custom behavior for strings
}

Common Pitfall:

Type mismatch: add(3, "hello") → compile error.


Class Templates

Basic Syntax:

template <class T>


class Box {
private:
T content;
public:
void set(T item) { content =
item; }
T get() { return content; }
};

Advanced Features:

1-Default Template Arguments:

template <class T = int> // Default to 'int'


class Container { /*...*/ };

2-Member Function Templates:

template <class U>


void compare(U other) { /* compare content with 'other' */ }

3-Inheritance with Templates:


template <class T>
class PrettyBox : public Box<T> { /*...*/ };

STL Example:

vector<int> v; // 'vector' is a class template


stack<string> s; // 'stack' is another
Variable Templates

Core Idea:

Typed constants or compile-time values.

Examples:

1-Mathematical Constants:
template <typename T>
constexpr T pi = T(3.141592653589793);
cout << pi<double>; // High precision

2-Type-Dependent Values:

template <class T>


constexpr T max_limit = numeric_limits<T>::max();

Why Useful?:

Replace #define macros with type-safe alternatives.


Non-Type Template Parameters

What?:

Templates can accept values (not just types).

Example:

template <int N>


class Array {
private:
int data[N]; // Fixed-size array
public:
int size() { return N; }
};

Array<10> arr; // N = 10

Use Cases:

Compile-time computations (e.g., factorial).


Fixed-size containers (like std::array).

Template Metaprogramming:
Compile-Time Power:

Templates are Turing-complete (can run logic at compile time).

Example: Factorial Calculation:

template <int N>

struct Factorial {

static const int value = N * Factorial<N-1>::value;

};

template <>

struct Factorial<0> { // Base case

static const int value = 1;

};

cout << Factorial<5>::value; // 120 (calculated at compile


time)

Real-World Use:

Optimizations in libraries (e.g., Eigen for linear algebra).

Best Practices & Pitfalls:


Dos:

Use typename for types, class for template template


parameters.

Prefer constexpr variable templates over macros.

Don’ts:

Avoid overly complex template hierarchies.

Watch for code bloat (each instantiation generates new code).

Debugging Tips:

Use static_assert for type constraints:

template <class T>


void print(T val) {
static_assert(is_integral<T>::value, "Must be integer");
cout << val;
}

Real-World Applications:
1-STL Containers: vector<T>, map<K,V>.

2-Smart Pointers: unique_ptr<T>, shared_ptr<T>.

3-Math Libraries: Matrix operations with Matrix<double>.


Summary & Q&A

Key Takeaways:

Function Templates: Generic algorithms.

Class Templates: Reusable data structures.

Variable Templates: Type-safe constants.

Q&A :
"What happens if you try to
instantiate Stack<Employee> without defining operator>?"
THANK YOU

You might also like