C++
  Home arrow C++ arrow Page 6 - 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 
Sun Developer Network 
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 / 42
    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 (contd.)


    (Page 6 of 9 )

    Open classtemplate.cpp and enter the following code:

    #include <iostream>

    #include "classtemplate.h"

    using namespace std;

    template<typename T> T MyClass<T>::GetAverage()

    {

    return static_cast<T>((n1 + n2 + n3) / 3);

    }

    int main()

    {

    MyClass<int> mc(1, 2, 3);

    cout << "Return value is: " << mc.GetAverage() << endl << endl;

    return 0;

    }


    Lets start by describing the GetAverage function of the MyClass class.

    template<typename T> T MyClass<T>::GetAverage()

    {

    return static_cast<T>((n1 + n2 + n3) / 3);

    }


    Firstly, our class function is declared as a template using the template<typename T> keywords, meaning that our class function has just one type identifier, T.

    T MyClass<T>::GetAverage()

    Secondly, our class function returns a value of type T. Notice that the class name, “MyClass”, is followed by our type parameter, T. This tells the C++ compiler that our function will handle values of type T.

    The code for our class function is similar to the code for our template function, from above:

    return static_cast<T>((n1 + n2 + n3) / 3);

    All of the three privately declared variables, n1, n2 and n3 are summed up, divided by three and then returned as a variable of type T.

    More C++ Articles
    More By Mitchell Harper


     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT