Advanced Programming C++


Published on

Final Presentation for the curse

Published in: Technology, Business
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Advanced Programming C++

  1. 1. Why you should use templates <ul><li>Templates are type-safe. Based on the fact that the types which the template acts on is already known at compile time, the compiler is able to do the type checking before a error occurs. </li></ul><ul><li>Templates are more easy to write. </li></ul>
  2. 2. Why you should use templates (II) <ul><li>It is possible to optimize the code. </li></ul><ul><li>Templates provide direct support for generic programming. </li></ul><ul><li>The use of templates is a good possibilitiy to reuse code. </li></ul>
  3. 3. Functions Templates <ul><li>We will illustrate an example: </li></ul><ul><ul><ul><li>It is wanted to create a function that independently gave back </li></ul></ul></ul><ul><ul><ul><li>the minimum between two values of its type </li></ul></ul></ul><ul><ul><ul><li>(it assumes that both have he himself type). </li></ul></ul></ul><ul><ul><ul><li>How can we implement this ? </li></ul></ul></ul>
  4. 4. Functions Templates (II) <ul><li>It would be possible to be thought about to define the function so many times as data types can be displayed... </li></ul><ul><ul><li>(int, long, float, double, etc.) </li></ul></ul><ul><li>Solution: uses Templates!. </li></ul><ul><li>template <class T> T minimum( T a, T b); </li></ul>
  5. 5. Functions Templates <ul><ul><li>template <class T> T minimum( T a, T b); </li></ul></ul><ul><li>In that case with <class T> it is being indicated that one is a group whose parameter is going to be the T type and that as the value of return as each one of both arguments is going to be of this data type T . </li></ul>
  6. 6. Functions Templates <ul><li>But it can be that it is needed to use more than one data type and some other constant parameter that can be used in the declarations. </li></ul><ul><ul><ul><li>template <class T1, class T2> void mixing(T1 a, T2 b); </li></ul></ul></ul>
  7. 7. Functions Templates <ul><li>Could be the case of that some arguments or the return data is a data type that we know and constant. </li></ul><ul><li>template <class T> T minimum(T a, T b) </li></ul><ul><li>{ </li></ul><ul><li>if(a <= b) </li></ul><ul><li>return a; </li></ul><ul><li>else </li></ul><ul><li>return b; </li></ul><ul><li>} </li></ul>
  8. 8. The main program <ul><li>#include <iostream.h> </li></ul><ul><li>template <class T> T minimum(T a, T b); </li></ul><ul><li>void main(void){ </li></ul><ul><li>int eOne=1; </li></ul><ul><li>int eTwo=5; </li></ul><ul><li>cout << minimum(euno, edos) << endl; </li></ul><ul><li>long lOne=1; </li></ul><ul><li>long lTwo=5; </li></ul><ul><li>cout << minimum(lOne, ltwo) << endl; </li></ul><ul><li>char cOne='a'; </li></ul><ul><li>char cTwo='d'; </li></ul><ul><li>cout << minimum(cOne, cTwo) << endl; </li></ul><ul><li>double dOne=1.8; </li></ul><ul><li>double dTwo=1.9; </li></ul><ul><li>cout << minimum(dOne, dTwo) << endl; </li></ul><ul><li>} </li></ul>
  9. 9. Functions Templates <ul><li>The execution of the previous program demonstrates that the type of the arguments and the value of return of the minimum() function are distinguished in each case to those of the call. </li></ul><ul><li>Warning : It is obvious also that an error will take place if two variables of different type go like arguments . </li></ul>
  10. 10. Classes Templates <ul><li>Similarly with the functions, a parameter will be defined that will indicate the data type with which more ahead the objects will be created </li></ul>
  11. 11. Template Specialization <ul><li>Any generic code development will need a small case where it needs to do some hard coding or to avoid some amount generic code. </li></ul><ul><li>Come in : Template Specialization! </li></ul>
  12. 12. Template Specialization <ul><li>It is possible to provide different definitions of a template, if you want to handle a specific type in an alternative way. </li></ul><ul><li>The right implementation is chosen by the compiler according the template arguments. </li></ul><ul><li>It is a very powerful feature! </li></ul><ul><li>It is often used for optimization. </li></ul>
  13. 13. Template Specialization (II) <ul><li>The idea of C++ class template specialization is similar to function template overloading. </li></ul><ul><ul><li>Example : if we wants to write a Queue class template with the ability to handle all data types </li></ul></ul><ul><li>Once the template is specialized, all the member functions should be declared and defined for the specific data type. </li></ul>
  14. 14. With Specialization <ul><li>Declaration should include the template keyword with the class name followed by the type in the angular brackets. </li></ul><ul><li>template<> </li></ul><ul><li>class MyQueue<double> </li></ul><ul><li>{ </li></ul><ul><li>std::vector<double> data; </li></ul><ul><li>public: </li></ul><ul><li>void Add(double const &); </li></ul><ul><li>void Remove(); </li></ul><ul><li>void Print(); </li></ul><ul><li>}; </li></ul>
  15. 15. Without Specialization <ul><li>template < typename T> </li></ul><ul><li>class MyQueue </li></ul><ul><li>{ </li></ul><ul><li>std::vector<T> data; </li></ul><ul><li>public: </li></ul><ul><li>void Add(T const &d); </li></ul><ul><li>void Remove(); </li></ul><ul><li>void Print(); </li></ul><ul><li>}; </li></ul>
  16. 16. Other Example : <ul><li>Standard Implementation: template <class T, int size> void Example::add (int number) { for (int i=0; i<size; i++) { data[i] += T(number); } } </li></ul>
  17. 17. <ul><li>Specialized implementation: template <> void Example<int,4>::add (int number) { data[0] += number; data[1] += number; data[2] += number; data[3] += number; } </li></ul>
  18. 18. Defining Functions for C++ Class Template Specialization <ul><li>Now all the functions for this specialized template should be declared individually for the double data type. For example: </li></ul><ul><li>template <> void MyQueue<double>::Add(double const &d) </li></ul><ul><li>{ </li></ul><ul><li>data.push_back(d); </li></ul><ul><li>} </li></ul>
  19. 19. Defining Functions for C++ Class Template Specialization(II) <ul><li>template <> void MyQueue<double>::Print() </li></ul><ul><li>{ </li></ul><ul><li>std::vector <double>::iterator It1; </li></ul><ul><li>It1 = data.begin(); </li></ul><ul><li>cout<<&quot;Double&quot;<<endl; </li></ul><ul><li>for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ ) </li></ul><ul><li>cout << &quot; &quot; << *It1<<endl; </li></ul><ul><li>} </li></ul>
  20. 20. <ul><li>If the above template MyQueue is used for double data type, the C++ compiler will not generate another version of class for the double data type from the generic MyQueue<typename T> template </li></ul><ul><li>Instead it will use the one defined as above in MyQueue<double> . For any other data types, the compiler will expand the generic C++ class template and use it. </li></ul>
  21. 21. Template Specialization <ul><li>It is possible to add a specialization of the template later without changing the code where the template is called. </li></ul><ul><li>Specialization types: </li></ul><ul><ul><li>Full specialization </li></ul></ul><ul><ul><ul><li>As described in the previous slide </li></ul></ul></ul><ul><ul><li>Partial specialization </li></ul></ul><ul><ul><ul><li>As described in the next slide </li></ul></ul></ul>
  22. 22. Partial Specification <ul><li>There are also partial specifications possible. That means that the implementation of the template can still allow parameters. </li></ul><ul><li>Example: </li></ul><ul><li>template <class T> void Example<T,1>::add (int number) { data[0] += T(number); } </li></ul>
  23. 23. Partial Specialization <ul><li>template <typename T> </li></ul><ul><li>class MyQueue<T,T>{}; </li></ul><ul><li>The above is the example for partial specialization with the same type. </li></ul><ul><li>template <typename T> </li></ul><ul><li>class MyQueue<T,double>{}; </li></ul>
  24. 24. Order of Specializations <ul><li>There are specializations that are more specialized than another one. </li></ul><ul><li>The most specialized implementation will be preferred. </li></ul><ul><li>Example: </li></ul><ul><li>template<class T> class Vector; //general template<class T> class Vector<T*>; //specialized for any pointer template<> class Vector<void*>; //specialized for void* </li></ul>