Solved

Templates

Posted on 2004-04-19
4
257 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

837 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