C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Templates

C++ Templates

shape Introduction

The present chapter CPP Templates first discusses about Generic Programming and then discusses about C++ Templates. Generic Programming defines the class behavior without knowing the data type possessed by the class operations. It is a type of programming that C++ supports. It is same as OOP's concept in C++ i.e. it reuses the code and abstracts the general concepts.

shape Description

CPP Templates are one of the features of C++ programming.Templates are resolved at compile time, not at runtime. This has some benefits and some drawbacks. The number one benefit is speed. All of the performance hit is taken at compile time. There is zero performance hit at runtime. The compiler actually generates versions of the code for the various types that is being used with. It allows both functions and classes to work on various data types without the need of rewriting the code. They make the classes or functions more abstract. Hence, the class or function template are independent of data types. For example, A template class is assigned a memory instead of creating memory for each data type specified. The template class can store different data type variables and is easily reused.

Function Templates

shape Description

Function CPP Templates act as a model to create similar new functions called Template Type Parameters. These place holder types limits the function call given by the functions. They play the key role. The compiler, therefore, sends the function template model to the function called. The called function replaces its parameters in the template model parameters and uses it. So, many functions use one template model.

shape Syntax

template return_type function_name(parameter list) { // body of function }

shape Example

[c] #include <iostream> using namespace std; template <class S> S Bigger(S x1, S x2) { return (x1>x2) ? x1:x2; } int main() { int a1, a2; float b1, b2; cout <<"Enter any two integers "; cin >> a1 >>a2; cout << Bigger(a1, a2) << " is bigger one" << endl; cout << " Enter any two floating-point numbers: "; cin >> b1 >> b2; cout << Bigger(b1, b2) << " is bigger one" << endl; return 0; } [/c] Output [c] Enter any two integers 5 6 6 is bigger one Enter any two floating-point numbers: 11.8 9.5 11.8 is bigger one[/c]

Class templates

shape Description

Even class templates use Template Type Parameters for the member function calls similar to Function Templates.

shape Syntax

template class class_name { //class members }

shape Example

[c] #include <iostream> using namespace std; template <class S> class splessons { S x,y; public: splessons(S first, S second) { x=first; y=second; } S getmax_val(); }; template <class S> S splessons<S>::getmax_val () { S dec; dec = x>y ? x : y; return dec; } int main () { splessons <int> values(45,65); cout << "The maximum value is " << values.getmax_val(); return 0; }[/c] Output [c] The maximum value is 65[/c]

Summary

shape Key Points

  • Class definition without knowing what data type is used is called as Generic Programming
  • Function CPP Templates is the model for upcoming functions
  • Class templates also use template type parameters for the member function calls