[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Templates

Posted on 2004-04-19
4
Medium Priority
?
262 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 160 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

649 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