Solved

Templates

Posted on 2004-04-19
4
259 Views
Last Modified: 2010-04-01
I am not understanding the concept of templates. In my program I have a customer class truck class and contract class. I am not getting an idea how to templates for this. take the class roughly as below
class customer                class truck                                                    class contract
{                                   {                                                                  {
private:                           private:                                                        private:
  char* Name;                    enum truck{ford, toyota, Benz};                     int No_of_months;
  int age;                            int truc_milage;                                            DATE start_date;
public:                            public:                                                          public:  
 getage();                          get_trucktype();                                            get_contractdate();  
 setage();                          get_milage();                                                get_No_ofmonths();
};                                  };                                                                 };
0
Comment
Question by:rmvprasad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 10866220
What makes you think you need a template?
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 40 total points
ID: 10866221
Templates is a mechanism to have parametrised classes

For example, if u have a class Complex representing Complex Numbers & has various operations defined on it, u could simply write this way

class Complex {
       int real ;
       int im ;
       public :
           Complex ( int re = 0 ; int im1 = 0 ) : real ( re ) , im ( im1 ) {}
           ...
            ....
} ;

But in this case, u can have complex numbers with real and imaginary parts being int
If u want a float version fo COmplex number class, u will have to rewrite it for float with some other name
Instead u could create a Template class which accepts the type as parameter

template <class Real, class Imag>
class Complex {
       Real real ;
       Imag im ;
       public :
         Complex ( Real re1, Imag im1 ) : real ( re ), im  ( im1 ) {} ;
   ..
 ..
};

int main ( ) {
      Complex <int, float> c ;
       Complex <int, int> c ;
       Complex <double, int> c ;
}
 
amit
0
 

Author Comment

by:rmvprasad
ID: 10866298
I am told to do so. My doubt is in this design of multiclasses should I write a template for each class or can it be done for a group of classes. If so how. If not then how to decide to which classes templates have to be applied. Please explain that.
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 10867029
It can and should be done for group of classes with the same amount of functions and functionality.
If you have 2 classes and both of them has 2 functions that return integer and string then you can write
a TEMPLATE class for them.

Regards,
  Tomas Helgi
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question