Object Oriented Programming: Chapter 5: Templates and Exception Handling

Template Arguments

C++ Object Oriented Programming

Explain the concept of templates arguments using C++.

Template Arguments

Templates can have multiple arguments. These arguments are normally denoted by the data type T. But along with T we can also define other data type arguments. For example we can define the template function as

Compare (T first, int second); //First argument is of type T and second is int

Following program illustrates this concept ‒

#include <iostream>

using namespace std;

template <class T>

class Compare

{ //writing the class as usual

       Ta, b;//note we have used data type as T

     public:

       Compare (T first, int second);

              T max ();//finds the maximum element among two

};

//template class member function definition

 //here the member function is constructor

template<class T>

Compare<T>::Compare(T first, int second)

{

       a=first;

       b=second;

}

//here the member function of template class is max

template<class T>

T Compare <T>::max ()

{

T val;

 if(a>b)

       val=a;

else

       val=b;

return val;

}

int main()

{

       Compare <int> obj1 (100, 160);//comparing two integers

       cout<<"\n maximum(100,160) = "<<obj1.max();

       return 0;

}

 

Review Question

1. Explain the concept of templates arguments using C++.


Object Oriented Programming: Chapter 5: Templates and Exception Handling : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Template Arguments


Object Oriented Programming: Chapter 5: Templates and Exception Handling



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation