GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Mar-2008, 01:42
ajpnaveen ajpnaveen is offline
New Member
 
Join Date: Mar 2008
Posts: 1
ajpnaveen is on a distinguished road

Class templates


Hi Everyone

I am not able to understand the concept of the class templates can anyone help
by sending some examples or any links which can easily understood

Jaya Prakash Naveen
  #2  
Old 25-Mar-2008, 05:15
L7Sqr L7Sqr is online now
Junior Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 91
L7Sqr will become famous soon enough

Re: Class templates


As far as links, google is your best bet.
The idea behind templates (in very short) can be illustrated by thinking about a sorting mechanism. Suppose that you have a class Auto that you provide an overloaded operator< for (that is given class Auto a1, a2 the following is a valid operation)
CPP / C++ / C Code:
bool less_than = a1 < a2;
And you want to provide a way to sort a set of those objects. The most likely candidate is a function that takes a set of class Auto and sorts (using the operator< mentioned above). Lets say that this function is 20-30 lines of code. This satisfies all of your requirements and you are finished... until you need to sort a set of class NonAuto. Then you need to design another function (another 20-30 lines of essentially the same code) to sort NonAuto objects as a set of NonAuto can not be passed to your Auto sort function.
Here is where you can use some of the power of templates. Consider the following:
CPP / C++ / C Code:
template <class Sortable>
void sort_set (set_of Sortable);
Now so long as you define operator< for whatever class you want to sort, you can reuse the same 20-30 lines of code for all sortable objects.
You may have noticed that I used the word set when describing how to store groups of the same object. This is another use of templates. In the stl (standard template library) you will find items such as vector, list, and map. These containers can hold any type of object and have a useful set of operations defined over them (insert, erase, push, pop, and iterators for traversing the sets - depending on what set you are using). Without the templates you would need to either define these operations for each type of set that there is or do some nasty void * casting.
I hope that helps.
  #3  
Old 25-Mar-2008, 11:06
C++_Bandit's Avatar
C++_Bandit C++_Bandit is offline
Junior Member
 
Join Date: Mar 2008
Location: Virginia
Posts: 40
C++_Bandit will become famous soon enough

Re: Class templates


here's just a sample of what you can find using google:
http://http://www.iis.sinica.edu.tw/.../templates.htm
http://www.codersource.net/cpp_class_templates.html
http://www.cplusplus.com/doc/tutorial/templates.html

there are litterally thousands more out there
  #4  
Old 27-Mar-2008, 04:14
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 348
Peter_APIIT is on a distinguished road

Re: Class templates


Template is a concept where relate to Generic programming.

No matter what data type, the operation behind it is the same.
__________________
Linux is the best OS in the world.
  #5  
Old 27-Mar-2008, 07:01
C++_Bandit's Avatar
C++_Bandit C++_Bandit is offline
Junior Member
 
Join Date: Mar 2008
Location: Virginia
Posts: 40
C++_Bandit will become famous soon enough

Re: Class templates


What's also great about templates is you can use them to create your own library that contains all the functions needed to work with an array, a struct, linked list, etc... instead of writing out those functions each time you need them in a program.
  #6  
Old 27-Mar-2008, 10:30
hawash hawash is offline
New Member
 
Join Date: Mar 2008
Posts: 1
hawash is on a distinguished road

Re: Class templates


www.cplusplus.com


may this link will help
 

Recent GIDBlogUpdates On The All New Toyota VIOS - Part III by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Return pointer to template's nested class problem Kimmo CPP / C++ Forum 2 20-Sep-2007 23:39
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Box Class, need help again :( TransformedBG CPP / C++ Forum 7 13-Nov-2006 15:11
a tester class and then some. postage Java Forum 1 06-May-2006 15:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:48.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.