banner



How To Use Templates In C++

Templates and Template Classes in C++


By Alex Allain

What'south better than having several classes that do the aforementioned affair to different datatypes? One class that lets y'all choose which datatype it acts on.

Templates are a way of making your classes more than abstract by letting you define the behavior of the class without actually knowing what datatype will be handled past the operations of the class. In essence, this is what is known as generic programming; this term is a useful mode to think about templates because it helps remind the programmer that a templated class does not depend on the datatype (or types) information technology deals with. To a large caste, a templated grade is more focused on the algorithmic idea rather than the specific nuances of a single datatype. Templates can be used in conjunction with abstract datatypes in social club to allow them to handle any type of data. For example, y'all could brand a templated stack grade that can handle a stack of whatsoever datatype, rather than having to create a stack class for every unlike datatype for which y'all desire the stack to role. The ability to have a single class that can handle several different datatypes ways the code is easier to maintain, and information technology makes classes more than reusable.

The bones syntax for declaring a templated class is every bit follows:

template <grade a_type> form a_class {...};

The keyword 'grade' above simply means that the identifier a_type volition stand for a datatype. NB: a_type is not a keyword; it is an identifier that during the execution of the program will represent a single datatype. For example, you could, when defining variables in the class, use the following line:

a_type a_var;

and when the programmer defines which datatype 'a_type' is to exist when the program instantiates a particular instance of a_class, a_var will exist of that type.

When defining a function equally a fellow member of a templated class, information technology is necessary to define information technology as a templated function:

template<class a_type> void a_class<a_type>::a_function(){...}        

When declaring an instance of a templated class, the syntax is equally follows:

a_class<int> an_example_class;        

An instantiated object of a templated course is called a specialization; the term specialization is useful to think considering it reminds us that the original class is a generic class, whereas a specific instantiation of a grade is specialized for a single datatype (although it is possible to template multiple types).

Commonly when writing code it is easiest to precede from concrete to abstruse; therefore, information technology is easier to write a class for a specific datatype and and so proceed to a templated - generic - class. For that brevity is the soul of wit, this example volition be cursory and therefore of little practical application.

We will ascertain the beginning class to act only on integers.

class calc {   public:     int multiply(int x, int y);     int add(int x, int y);  }; int calc::multiply(int 10, int y) {   return x*y; } int calc::add(int 10, int y) {   return x+y; }

We at present have a perfectly harmless picayune class that functions perfectly well for integers; simply what if we decided nosotros wanted a generic class that would work equally well for floating point numbers? Nosotros would use a template.

template <class A_Type> class calc {   public:     A_Type multiply(A_Type ten, A_Type y);     A_Type add together(A_Type x, A_Type y); }; template <grade A_Type> A_Type calc<A_Type>::multiply(A_Type x,A_Type y) {   return ten*y; } template <form A_Type> A_Type calc<A_Type>::add(A_Type x, A_Type y) {   return x+y; }

To understand the templated grade, only recall about replacing the identifier A_Type everywhere it appears, except every bit part of the template or course definition, with the keyword int. It would be the same equally the above course; at present when you instantiate an
object of grade calc you lot tin can choose which datatype the grade volition handle.

calc <double> a_calc_class;

Templates are handy for making your programs more than generic and allowing your lawmaking to be reused later.


Related articles

Templated Functions

Template Specialization and Partial Specialization in C++

Intro to C++ classes

Initialization lists: Initialization lists are important for allowing template types to work with both archaic and user-defined types

Standard Template Library Introduction

How To Use Templates In C++,

Source: https://www.cprogramming.com/tutorial/templates.html

Posted by: alexanderdellittef1972.blogspot.com

0 Response to "How To Use Templates In C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel