Home arrow C++ arrow Page 5 - 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 - A simple class template
(Page 5 of 9 )

Templates can also be used to create classes. Class templates stem from the same principles as normal templates, and are used in exactly the same way. A class template allows a developer to create classes whose constructor, destructor and functions can accept different types of values, just like a function template.

To create a class template, create a new C++ project and add two files: classtemplate.h and classtemplate.cpp. Into classtemplate.h, enter the following code:

template<typename T> class MyClass

{

public:

MyClass(T x = 0, T y = 0, T z = 0) : n1(x), n2(y), n3(z) {}

T GetAverage();

private:

T n1;

T n2;

T n3;

};


This code might look a little confusing at first, but allow me to explain it. Notice how we have defined the entire class in the header file this time, and not just the declaration like we did for the function template?

Firstly, we have the class declaration:

template<typename T> class MyClass

A class template declaration is a bit different to a function template declaration. Firstly, between the angled brackets, we are creating a type identifier. We have used “typename” instead of “class” here, but they are interchangeable.

Next, we have the “class” keyword followed by the name of the class, “MyClass”. The class can be called anything you like, but we will use “MyClass” in this example.

{

public:

MyClass(T x = 0, T y = 0, T z = 0) : n1(x), n2(y), n3(z) {}

T GetAverage();

private:

T n1;

T n2;

T n3;

};


Following the template class’s declaration, we have its default constructor and one function named GetAverage() which takes no parameters and returns a value of type T.

Notice that our constructor is accepting three variables of type T, all with a default value of zero. The constructor assigns these values to our private member variables (of type T) n1, n2 and n3 respectively. These private variables are simply used to hold the values of the variables passed in and can’t be accessed.
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