C++
  Home arrow C++ arrow Page 5 - The Mighty C++ Template
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

The Mighty C++ Template
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 43
    2001-11-23

    Table of Contents:
  • The Mighty C++ Template
  • Understanding function overloading
  • The template
  • The template (contd.)
  • A simple class template
  • A simple class template (contd.)
  • Instantiating the class
  • Behind the scenes
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By Mitchell Harper


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek