Thread: class Template problems

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    class Template problems

    Hello,

    I am trying to wrap my head around class templates.

    I have an array but do not know how to implement with a template class.

    Before Template:
    Code:
    class Foo
    { public:
         Bar()
           { ...;  }
    
       private:
         ...
    };
    
    int main()
    {
       Foo *Array[10];
    
       Array[0] = new Foo;
       Array[0] -> Bar();
       
    };
    After Template
    Code:
    template <class T>
    class Foo
    { public:
         Bar()
           { ...;  }
    
       private:
         ...
    };
    
    int main()
    {
       Foo<What Do I put Here> *Array[10];  // Or is the even the correct syntax?
    
       Array[0] = new Foo;   
       Array[0] -> Bar();
       
    };

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In the ... code that you didn't post, you used T instead of some other type (maybe int?). Whatever type you replaced with T is a good candidate to put in the brackets. Of course, any type can be put there as long as your class can be compiled when T is replaced with that type.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    In this case it looks like I am declaring a template class Foo and then trying to create an array of type Foo to hold names and ages.

    Code:
    template<class T>
    class Foo
    { public:
         void getName()
          { cout<< "Enter Name" << endl;
             cin>> name;
             cout<< "Enter Age" << endl;
             cin>> age;
          }
    
      private:
         string name;
         int age;
    }
    
    int main()
    { Foo<Foo > *Array[10];   //Should I try to make a generic array class instead;
    
       Array[0] = new Foo; 
       Array[0] -> gerName();
    
    return(0);
    }a
    This obviously is not working. Should I be trying to make a template class Array and then populate it with a non template class Foo?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What are you trying to do? You made Foo a class template. Why? The point of making it a template is so that it can work with different types. For example, maybe you have a data member variable that can be an int or a string or a char or a double. Instead of having a different class FooInt, FooString, FooChar, FooDouble, you have one class that is templated and uses T as the type for the data member variable. Then later, you can use Foo<int> or Foo<string> or Foo<char> or Foo<double>.

    Try and figure out what you want to be generic. What code do you want to work for different types?

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    OK. I think I understand.

    Is this a better use of a template class. This way the user can enter an integer, float, double...

    Code:
    template <class T> 
    class Account
    {  public:
          void deposite(T sumNum)
            { Balance += sumNum;
            }
          vod withdraw(T sumNum)
            { Balance -= sumNum;
            }
       private:
          T Temp;
          T Balance;
    };

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes. That makes more sense. Then in main(), you could have
    Code:
    Account<int> my_dollar_account;
    my_dollar_account.deposite(100);

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    I think I finally see the light.

    So if I understand this

    Code:
    Account<int> my_dollar_account;
    'int' is giving a type to the generic T that is being used in the functions.
    'my_dollar_account' is acting like and array to store information.

    Is my_dollar_account cappible of handiling multiple enteries like an array. If so, how does it take care of indexing?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 'my_dollar_account' is acting like and array to store information.

    No, my_dollar_account is just a variable like any other variable. If you want an array, you have to create an array. Templates and arrays are two separate subjects, you can use them together, but one has nothing to do with the other.

    So if you want an array of Account objects, make one. Or use std::vector which is itself a templated class.

    If you are trying to write your own array class using a template, which is a common exercise in learning templates, then you can scrap your Account class. Instead, write an array class that holds ints. When you are done and it is working, replace int with T and make the class a template.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I am trying to wrap my head around class templates.
    You should probably start with function templates and get comfortable with those first.

    Is my_dollar_account cappible of handiling multiple enteries like an array. If so, how does it take care of indexing?
    my_dollar_account is an object of a class you defined. So, when you ask whether it is capable of handling multiple entries like an array, it's all up to you. If you define objects of your class to have a member variable that is an array, then it can handle multiple entries. How does it take care of indexing? Anyway you decide.

    Instead, write an array class that holds ints. When you are done and it is working, replace int with T and make the class a template.
    I think that is the best approach as well. At this point, it's apparent you've chosen an example that is too difficult for you. Try creating a simpler templated class. For instance, declare a template that has one private member variable that you set in a constructor. Then add a simple get() function that returns the member variable. Your goal is to write a template that will handle any type.
    Last edited by 7stud; 02-16-2006 at 10:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. template function v.s. template class
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2007, 01:46 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM