C++
  Home arrow C++ arrow Page 7 - 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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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 - Instantiating the class


    (Page 7 of 9 )

    Before we can use our class template, we must create an instance of it within our main function:

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

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


    On the first line, we are creating a new instantiation of our class template. Between the angled brackets, we are telling the C++ compiler that our class will be handling integer (int) type variables. This is a mandatory inclusion and the compiler will complain if you leave it out. The new instantiation of our “MyClass” template is called “mc”. We are passing three values to the default constructor, which will be stored as the private variables n1, n2 and n3. In our examples, these variables will be of type int, meaning T will take on the role of an integer.

    On the second line above, we simply call the GetAverage() function of our class template, just like we would with any class, outputting the value returned from the function, which looks something like this:

    Return value is: 2

    More C++ Articles
    More By Mitchell Harper


     

    C++ ARTICLES

    - 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
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway