Click here to Skip to main content
Licence 
First Posted 24 Mar 2002
Views 119,089
Bookmarked 43 times

Template based programming

By | 24 Mar 2002 | Article
As we already know of inheritance, it is a good way of reusing your code. However, Visual C++ provides another way of reusing your code. We can do this through templates. With templates you can make the 'type' parameter. Let me explain.

Introduction

As we already know of inheritance, it is a good way of reusing your code. However, Visual C++ provides another way of reusing your code. We can do this through templates. With templates you can make the 'type' parameter. Let me explain. When you had this class that shows all processor information. The class is huge, and it uses all integers. What if, in time, the integer wouldn't be able to store the information of the processor (just because it's out of range for the integer). Now you'll need to review all your code and change the integers to longs. Or when you have this class that handles int arrays, and someday you'll need the exact same class, only you want it to handle floats.

Details

For these problems we have templates. Let me show you some code:

class CMyIntArray
{ 
public: 
    int GetSize();//gets the size of the array int 
    SetSize();//sets the size of the array 
    void Add(int Value);//adds a value to the array etc...
}

As it becomes clear from this code, you cannot handle floats with this class. A float class would look like this:

class CMyFloatArray
{ 
public: 
    int GetSize();//gets the size of the array int 
    SetSize();//sets the size of the array 
    void Add(float Value);//adds a value to the array etc...
}

Ok, we can handle this little bit of change.. but what if you had lots of functions, and what if we needed them same class for short, char etc...

You might now the old C macros (#define), template based programming is just like that (in a way). But, type macros aren't type safe, templates are. If you want to know how to create a template please look at the code below:

//Use of templates in Visual C++ by Clemens Timmermans (maxcode.com)
template <typename Type>
class CMyArray
{ 
public:
    void Add(Type Value);
    int GetSize();
    int SetSize();
};

That wasn't so hard? right? Now lets see how we can USE this template, by the following code snippet:

    ...
    // USING CMyArray
    CMyArray <int> iArray;
    iArray.Add(100);
    ////////////////////
    CMyArray<float> fArray;
    fArray.Add(10.2);
    /////////////////////
    ...
    //you got the point of basic template use!

No, finally, we may also use the Templates as containers. Checkout the following code:

class CMercedes
{
public:
    int GetMaxSpeed();//this gets the max speed of the car
    void SpeedUp();
    CMercedes();
    virtual ~CMercedes();
}

in template based programming we would make it look like this, no matter what the brand is.

//what if we want to create a template class 
//which needs to take any valid car

template <typename MyCar>
class CTemplateCar
{
private:
    MyCar theCar;
public:
    CTemplateCar(){}
    virtual ~CTemplateCar(){}
    void SpeedUpWhateverCarWeLike()
    {
        theCar.SpeedUp();
    }
}

now, as we want to use the template.. use this code to use it!

    ...
    CTemplateCar<CMercedes>  car;
    car.SpeedUpWhateverCarWeLike();
    CTemplateCar<CFerrari>  car2;
    car2.SpeedUpWhateverCarWeLike();
    ...

I hope this article has helped you understand the meaning of template based programming, but above all, how to use it and why use it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

MAXcode



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 Pinmemberzxw196216:53 17 Aug '10  
GeneralA couple of other points PinmemberChristian Graus23:23 25 Mar '02  
GeneralRe: A couple of other points PinmemberJörgen Sigvardsson1:14 26 Mar '02  
GeneralRe: A couple of other points PinmemberChristian Graus - Inlaw Programmer8:07 26 Mar '02  
GeneralA couple of other points PinmemberChristian Graus23:23 25 Mar '02  
GeneralRe: A couple of other points PinmemberPaul Selormey1:11 26 Mar '02  
GeneralRe: A couple of other points PinmemberChristian Graus - Inlaw Programmer8:05 26 Mar '02  
GeneralRe: A couple of other points PinmemberPaul Selormey13:23 26 Mar '02  
GeneralRe: A couple of other points PinmemberChristian Graus20:29 26 Mar '02  
GeneralRe: A couple of other points PinmemberMike_Beard5:51 6 Aug '02  
Generalrating math PinmemberNish [BusterBoy]14:20 25 Mar '02  
GeneralRe: rating math PinmemberChristian Graus23:11 25 Mar '02  
GeneralRe: rating math PinmemberNish [BusterBoy]23:20 25 Mar '02  
GeneralI'm sorry, but.... PinmemberChristian Graus10:57 25 Mar '02  
GeneralRe: I'm sorry, but.... PinmemberNish [BusterBoy]14:23 25 Mar '02  
GeneralRe: I'm sorry, but.... PinmemberChristian Graus19:54 25 Mar '02  
Generalhey PinmemberNish [BusterBoy]8:19 25 Mar '02  
GeneralThe game for baby Pinmembersoptest10:10 25 Mar '02  
GeneralRe: The game for baby PinmemberNish [BusterBoy]14:24 25 Mar '02  
GeneralRe: The game for baby Pinmembersoptest14:37 25 Mar '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 25 Mar 2002
Article Copyright 2002 by MAXcode
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid