Thread: Templates in c++...

  1. #1
    Registered User
    Join Date
    Jul 2012

    Templates in c++...

    hey all,
    i just learned templates in C++ and wanna know how to put them to practical use...
    the following example will illustrate my doubt:
    using namespace std;
    template <class aType>
    class com{
            com(aType x){
                cout << x + 5;
    int main(){
        int a = NULL;
        double b = NULL;
        cout << "enter a number: ";
        /* here wha' i wanna do is,
        if the user enters a number,
        i wanna store it in a... USING A CIN STATEMENT WHICH I HAVEN'T MENTIONED
        if the user enters a number with a fraction store it in b... USING A CIN STATEMENT WHICH I HAVEN'T MENTIONED
        // if i could do that then i could practically use templates in real programs
        if(a == NULL && b != NULL){
            com <double> x(b);
        }else if(a != NULL && b == NULL){
            com <int> x(a);
            cout << "Sorry there was an error...";
    Last edited by tennisstar; 11-23-2012 at 10:40 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You should read into a string then parse the string (after or during which you determine if the contents can be interpreted as an integer or fraction).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    I think you may be misunderstanding what templates can do.
    Do you understand when a variable goes out of scope, and what that means?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    Templates are useful when you know the types of something at compile time.
    Here, you are deciding the type at runtime (you are deciding something on what the user inputs, which is after the program has been compiled). That's a different beast and requires radically different techniques to achieve.
    I'm just giving you a heads up here. Templates are compile-time beasts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. templates
    By yosef_yaniv in forum C++ Programming
    Replies: 9
    Last Post: 02-04-2008, 12:30 PM
  2. Templates. Help!
    By hanniball in forum C++ Programming
    Replies: 13
    Last Post: 01-11-2008, 10:32 AM
  3. I need help with templates!
    By advocation in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2005, 09:27 PM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM
  5. Need help with templates
    By Flyer in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2003, 10:12 AM