Solved

Templates

Posted on 2004-04-19
4
258 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
Cell Value from TStringGrid::StringGrid1DrawColumnCell function in C++ builder XE8 3 98
Find Visual Studio Tools 2 126
Safe conversion? 4 76
Why isn't object file created? 6 94
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

730 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