Solved

Templates

Posted on 2004-04-19
4
254 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
Comment Utility
What makes you think you need a template?
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 40 total points
Comment Utility
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
Comment Utility
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 24

Expert Comment

by:Tomas Helgi Johannsson
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
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 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 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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now