Home arrow C++ arrow Page 4 - The Mighty C++ Template
C++

The Mighty C++ Template


One of the biggest advantages of using C++ is templates. Templates were designed from the ground up to allow developers to write one function to handle many different types of parameters. In this article, Mitchell will explain what both function and class templates are, and give examples of each.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 48
November 23, 2001
TABLE OF CONTENTS:
  1. · The Mighty C++ Template
  2. · Understanding function overloading
  3. · The template
  4. · The template (contd.)
  5. · A simple class template
  6. · A simple class template (contd.)
  7. · Instantiating the class
  8. · Behind the scenes
  9. · Conclusion

print this article
SEARCH DEVARTICLES

The Mighty C++ Template - The template (contd.)
(Page 4 of 9 )

Now that we have created the function declaration for our template, lets create the actual code for it. In main.cpp, enter the following code:

#include <iostream>

#include "main.h"

using namespace std;

template<class T> T GetAverage(T num1, T num2, T num3)

{

return static_cast<T>((num1 + num2 + num3) / 3);

}

int main()

{

int x = 10;

int y = 20;

int z = 30;

int returnValue = GetAverage(x, y, z);

cout << "Returned " << returnValue << endl << endl;

return 0;

}


We start out by #including the iostream head filer (so we can output to the screen) and our main.h header file, which contains the declaration of our template.

Next, we have the full code for our template:

template<class T> T GetAverage(T num1, T num2, T num3)

{

return static_cast<T>((num1 + num2 + num3) / 3);

}


The first line is exactly the same as the templates declaration in main.h. Our template only contains one line, which calculates the average of the three variables passed in as parameters, and returns that value.

Our template uses the static_cast<>() function to make sure that the return value is also of type T. This is necessary, because most of the time, when finding the average, the result will contain a decimal point and mantissa. If, for example, we passed three int values to the template, then we would expect an int to be returned, and not a double, for example. Thatís what the static_cast<>() function is responsible for handling.

Note: The static_cast<>() function is actually a template. The value between the angled brackets tells the template what the return type of the template should be.

Before moving onto the next section, try changing the types and values of x, y, and z. The return type of the template will be the same as the values passed to it!
blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials