Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By Elysia

Templates in c++...

This is a discussion on Templates in c++... within the C++ Programming forums, part of the General Programming Boards category; hey all, i just learned templates in C++ and wanna know how to put them to practical use... the following ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    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:
    Code:
    #include<iostream>
    using namespace std;
    
    
    template <class aType>
    class com{
    
    
        public:
            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
        HOW TO DO THAT IS MY QUESTION...
        */
        // if i could do that then i could practically use templates in real programs
        //like:
        if(a == NULL && b != NULL){
    
    
            com <double> x(b);
    
    
        }else if(a != NULL && b == NULL){
    
    
            com <int> x(a);
    
    
        }else{
    
    
            cout << "Sorry there was an error...";
    
    
        }
    
    
    }
    Last edited by tennisstar; 11-23-2012 at 09:40 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    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).
    whiteflags likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Location
    New Zealand
    Posts
    6,288
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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.
    King Mir likes this.
    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, 11:30 AM
  2. Templates. Help!
    By hanniball in forum C++ Programming
    Replies: 13
    Last Post: 01-11-2008, 09:32 AM
  3. I need help with templates!
    By advocation in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2005, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21