Introduction to templates in C++


C++ Programming,
Introduction to templates in C++
Templates (generic programming) are useful, when we want to focus not on the type of objects but
rather on the generic algorithm or method of organizing our objects.
1. Template methods
Let’s assume that we need a function to calculate an average of two values.
It works without any problem:
2
What if we need to calculate an average of two int variables?
Still it works, because ints are automatically casted to doubles. However, the casting not always
solves the problem. For that reason, we can use templates in order to focus on the algorithm which
is not dependent on the type.
The average function can be implemented more generally:
Now we are ready to calculate average for any type (even our own) for which the operations of
adding and division by double are defined.
3
Still, if you have your version for a given type (such as double), this method will be called instead
of the template method.
For:
we have:
And for:
4
we have:
What will we have for in this situation?
Result (int was casted to double and the double version was used):
But what if we have only the template version?
5
Now there is a problem with the compilation. It is not clear for the compiler, as the types are not the
same. No casting is assumed when using templates.
To solve the problem we can define our template method as:

代做Programming留学生作业、代写C++程序设计作业
Now it works
It works even if T1 is the same type as T2:
6
Average for arrays.
Casting will not help us in case of arrays. For example, there is no auto casting from double * to
int *.
Example:
This works:
This does not work for array with ints, although the algorithm is exactly the same:
7
One possible solution is to add a method for int *.
The above works, but it is an unnecessary duplication of the same code. Instead we can use
templates. It will work for any type with proper operations, for example float.
8
2. Template classes
Classes also can be parameterized by types.
Let’s assume a class that implement a vector of numeric values with easy usage and boundary
checking.
What if we need a vector of ints, floats or objects of any other type? We do not have to duplicate the
code for each type, we can make it generic, independent on type of objects that are stored in vector.
9
We have to indicate what type of objects are stored in vector.
It has to be known during compilation, so that the compiler can generate a class definition.
Classes MyVector<double> and MyVector<int> (and others) are totally different classes, they are
not inherited, they are generated as separate classes.
10
We can use MyVector<T> to store elements of any type.
Example:
Task 1
Implement a generic class to store three elements, each of any type. They could be of the same type,
but they could also be of three different types.
Task 2
Create a new version of AddressBook project. Use vector<> class or list<> class from standard
template library (STL) to store the list of Address objects inside AddressBook class.
All the requirements are the same. Additionally, provide the functionality to remove a given address
from the address book.
Check available constructors and methods for vector<> and list<> at:
http://en.cppreference.com/w/cpp/container
11
http://en.cppreference.com/w/cpp/container/vector
http://en.cppreference.com/w/cpp/container/list

因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com 

微信:codinghelp

上一篇:Tomcat启动报错:More than one fragment with the name [org_apache_tomcat_websocket]


下一篇:sql语句