Object Oriented Programming: Chapter 5: Templates and Exception Handling

Generic Programming

C++ Object Oriented Programming

The generic programming is a technique that allows to write the code for any data type elements.

Syllabus : 5

Templates and Exception Handling

 

Generic Programming

•  The generic programming is a technique that allows to write the code for any data type elements. The template is used as a tool for generic programming.

int max(int left, int right)

{

if(left<right)

      return right;

else

      return left;

}

double max(double left,double right)

{

if(left<right)

      return right;

else

      return left;

}

• The above two functions are nearly identical but the function on the left side is for comparing the two integer numbers and the function on the right hand side is for comparing the two double type numbers. Now we want the generic programming algorithm which will perform the functionality of both the data type elements. Hence we can replace int and double by a type T. Here is an example ‒

#include<iostream>

using namespace std;

template <class T> //T helps us to take any type of data

T max(T left,T right)

{

if(left<right)

      return right;

else

      return left;

}

int main()

{

int int1,int2;

cout<<"\n\t Comparison of Two integer numbers";

cout<<"\n Enter first integer number";

cin>>int1;

cout<<"\n Enter second integer number";

cin>>int2;

int max1=max(int1,int2);//passing two integer values

cout<<" The Maximum integer number is"<<max1<<endl;

double d1,d2;

cout<<"\n\t Comparison of Two double numbers";

cout<<"\n Enter first double number";

cin>>d1;

cout<<"\n Enter second double number";

cin>>d2;

double max2=max(d1,d2);// passing two double values

cout<<"The maximum double number is" <<max2<<endl;

return 0;

}

Output

Comparison of two integer numbers

Enter first integer number20

Enter second integer number10

The Maximum integer number is20

Comparison of two double numbers

Enter first double number6.2

Enter second double number77.99

The maximum double number is77.99

 

Templates allow the reusability of the code. There are two categories of templates ‒

1. Function template

2. Class template

 

Review Questions

1. Explain the usage of templates in C++.

2. Describe templates and its types.

 

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


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