Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Welcome to the TechnoWorldInc! Community!
Recent Updates
[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » C/C++/C#
 C++: Template Classes
Pages: [1]   Go Down
  Print  
Author Topic: C++: Template Classes  (Read 3327 times)
Taruna
Elite Member
*****



Karma: 13
Offline Offline

Posts: 845

Hi ALL


View Profile
C++: Template Classes
« Posted: December 27, 2006, 12:40:11 AM »


How to layout a template class and what it's good for.

Template class is exactly what it seems, a template. It allows you to define a class and involve variables of unknown type T. Thus the class can be implemented as a universal class for any group of elements sharing a class or base class.

A stack is a good example which can be used for many types.
CODE Example:

Code:
template <class T> 
class Stack
{
       int max;
   T** rep;            // a dynamic array of pointers to T
   int size;
public:
   Stack(int);            // a constructor
   ~Stack( ) {delete [ ] rep;}   // destructor   
   bool empty( );
   void push(T&);
   T& pop( );
};

You could add many more methods to this to expand the explanation, but this shows the use.

NOTE: the method body for the member functions have not been written, these are only the declarations.

This stack is implemented using a dynamic array, which is created by passing the max size to the stack upon creating the object. To create an unending stack, you could implement a Vector or Linked List.

Now the stack can handle objects of typ T which can be any type. If you use primatives, they must all be of the same type in a single stack to prevent chaos in your methods.

If you wish to use only user definded objects, then you could write the methods to call subsequent methods inside your object based on the current pointer on the stack.
NOTE this last idea requires that all mixed objects be derived from a single superclass or base class. as the declaration of this stack even as a template requires a given type.

Stack integerStack(20);

Questions/Comments: [email protected]

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.099 seconds with 25 queries.