All Sessions - Lesson 15(partial We are not going to cover...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lesson 15 (partial) We are not going to cover macros in this course. An Introduction to Templates Templates are probably one of the most powerful features of the C++ language that often are the least approached, or understood. Templates in C++ allow you to define a behavior that you can apply to objects of varying types. Template Declaration Syntax You begin the declaration of a template using the template keyword followed by a type parameter list. The format of this declaration is template <parameter list> … template declaration … Let’s analyze a sample template function declaration: template < typename objectType> const objectType & GetMax ( const objectType & value1, const objectType & value2) { if (value1 > value2) return value1; else return value2; } The keyword template marks the start of a template declaration and is followed by the template parameter list. This parameter list contains the typename keyword that defines the template parameter objectType, making it a placeholder for the type of the object that the template is being instantiated for. The template declaration is what contains the pattern that you want to implement. Sample usage: #include <iostream> using namespace std; int main() { int nInteger1 = 25; int nInteger2 = 40; const int nMaxValue = GetMax < int > (nInteger1, nInteger2); cout << "The largest of " << nInteger1 << " and " << nInteger2 << " is " << nMaxValue << endl; double dDouble1 = 1.1; double dDouble2 = 1.001; const double dMaxValue = GetMax < double > (dDouble1, dDouble2); cout << "The largest of " << dDouble1 << " and " << dDouble2 << " is " << dMaxValue << endl; return 0; } Page 1 of 18
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Note the detail <int> used in the call to GetMax. It effectively defines the template parameter objectType as int. The preceding code would lead to the compiler generating two versions of the template function GetMax that can be visualized as the following: const int& GetMax ( const int & value1, const int & value2) { //... } const double& GetMax ( const double & value1, const double & value2) { //... } In reality, however, template functions don’t necessarily need an accompanying type specifier. So, the following function call would work perfectly well: const int nMaxValue = GetMax (nInteger1, nInteger2); Compilers in this case are intelligent enough to understand that the template function being invoked for the integer type. However, this is true only for template functions and not for classes. The Different Types of Template Declarations A template declaration can be - A declaration or definition of a function (as you saw earlier) - A declaration or definition of a class - A definition of a member function or a member class of a class template - A definition of a static data member of a class template - A definition of a static data member of a class nested within a class template - A definition of a member template of a class or class template Template Classes Template classes are templatized versions of C++ classes that you have learned since Lesson 10, “Classes and Objects”.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern