Presentation is loading. Please wait.

Presentation is loading. Please wait.

Eleventh step for Learning C++ Programming

Similar presentations


Presentation on theme: "Eleventh step for Learning C++ Programming"— Presentation transcript:

1 Eleventh step for Learning C++ Programming
Template

2 We already know linked lists as data structures:
Generic Programming We already know linked lists as data structures: class ListOfInt { public: int data; ListOfInt * next; }; class ListOfDouble { public: double data; ListOfDouble * next; }; So far we have to create for every datatype a separate ListOf[Datatype] definition 11/2014

3 Templates offer a convenient way to overcome this problem:
Class Templates Templates offer a convenient way to overcome this problem: template <class TYPE> class ListOf { public: TYPE data; ListOf<TYPE> * next; }; Example is in file linkedList_generic.cpp 11/2014

4 Both forms are identical
Template Declaration General form of Template declaration: template <class identifier> class_definition; or template <typename identifier> class_definition; Both forms are identical In the class definition identifier is some form of placeholder that can be used at positions, where we expect some type specification 11/2014

5 [ practice 1 Function Template ]

6 [ explain 1 Function Template ]

7 [ practice 2 Type Inference ]

8 [ explain 2 Type Inference ]

9 [ practice 3 Template Specialization ]

10 [ explain 3 Template Specialization ]

11 Weird Template template <long N> class Factorial { public: long Value(void) { return N * fn_1.Value(); } private: Factorial<N-1> fn_1; }; template <> class Factorial<0> { public: long Value(void) { return 1; } }; 11/2014

12


Download ppt "Eleventh step for Learning C++ Programming"

Similar presentations


Ads by Google