Thread: Templates...

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

    Templates...

    hi all,
    i just learned the topic about templates...
    i successfully made a template program in which the instance of the templated class is a normal instance:
    Code:
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    
    template<class aType>
    class calc{
    
    
        public:
            void add();
            void sub();
            void mul();
            void div();
        private:
            aType a;
            aType b;
    
    
    };
    
    
    template<class aType> void calc<aType> :: add(){
    
    
        cout << "Enter a number: ";
        cin >> a;
        cin.ignore();
    
    
        cout << "Enter another number: ";
        cin >> b;
        cin.ignore();
    
    
        cout << "\n===\n\nThe sum is " << a + b << "...\n\n===";
        cout << "PRESS ENTER TO CONTINUE===";
        cin.get();
        system("cls");
    
    
    }
    
    
    template<class aType> void calc<aType> :: sub(){
    
    
        cout << "Enter a number: ";
        cin >> a;
        cin.ignore();
    
    
        cout << "Enter another number: ";
        cin >> b;
        cin.ignore();
    
    
        cout << "\n===\n\nThe difference is " << a - b << "...\n\n===";
        cout << "PRESS ENTER TO CONTINUE===";
        cin.get();
        system("cls");
    
    
    }
    
    
    template<class aType> void calc<aType> :: mul(){
    
    
        cout << "Enter a number: ";
        cin >> a;
        cin.ignore();
    
    
        cout << "Enter another number: ";
        cin >> b;
        cin.ignore();
    
    
        cout << "\n===\n\nThe product is " << a * b << "...\n\n===";
        cout << "PRESS ENTER TO CONTINUE===";
        cin.get();
        system("cls");
    
    
    }
    
    
    template<class aType> void calc<aType> :: div(){
    
    
        cout << "Enter a number: ";
        cin >> a;
        cin.ignore();
    
    
        cout << "Enter another number: ";
        cin >> b;
        cin.ignore();
    
    
        cout << "\n===\n\nThe quotient is " << a / b << "...\n\n===";
        cout << "PRESS ENTER TO CONTINUE===";
        cin.get();
        system("cls");
    
    
    }
    
    
    int main(){
    
    
        calc <double> co;
        co.add();
        co.sub();
        co.mul();
        co.div();
    
    
    
    
    }
    but what should i do if i want to create a pointer instance of the templated class ?

    i've tried this but doesn't work:
    Code:
        calc *co = new calc;
        co->add();
        co->sub();
        co->mul();
        co->div();

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    You need to specify a template parameter both for the type of the variable and for the object you want to create a new instance of, e.g.:

    Code:
    calc<double> *cop = new calc<double>();
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates
    By rodrigorules in forum C++ Programming
    Replies: 5
    Last Post: 11-23-2009, 11:46 AM
  2. How to use templates on this.
    By m3rk in forum C++ Programming
    Replies: 9
    Last Post: 08-19-2009, 06:15 PM
  3. Templates
    By arjunajay in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2005, 11:17 AM
  4. help with templates
    By drdodirty2002 in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2004, 05:25 AM
  5. Templates
    By Trauts in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2003, 03:58 PM