![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Programming tutorials All Knowledge Info and links to posted here |
![]() |
|
C++: Template Classes
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Administrator
Posts: 876
Join Date: Oct 2005
Rep Power: 10
IM:
|
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<INT> integerStack(20); </INT> |
|
|
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hospital H&P Template 2 | Xtreme | Application Downloads | 0 | 02-Mar-2008 06:56 AM |
| Sleek Web Hosting Template | Iphone | Graphic tutorials | 0 | 16-Apr-2007 11:23 PM |
| Extreme Template V5 - Another MASSIVE Extreme Template Tutorial! | Iphone | Graphic tutorials | 0 | 13-Apr-2007 05:41 AM |
| Extending Classes in Java | Anilrgowda | Programming tutorials | 0 | 21-Dec-2006 02:00 AM |
| Java: Wrapper Classes | Admin | Programming tutorials | 0 | 03-Dec-2006 11:34 PM |