Advertisement

What are templates??

Started by April 25, 2001 07:19 AM
0 comments, last by Xeee 23 years, 9 months ago
please i want to know what are templates , how to use them ......etc. also i wanted to ask about virtual functions please explain in detail and i''ll get it thanks for answering ...Xeee
xee..
Ok, here's the answer to "What is a template?"

Template classes are ways of providing type independant generic programming© For instance, the STL ¥standard template library¤, which ships with all c++ compilers¤ has a whole bunch of template container classes for linked lists, queues, vectors, stacks etc© and a bunch of template classes for generic algorithms like sort, find, replace, foreach, etc©

Suppose that you are making a game that spawns some number of enemies ¥objects of your CEnemy class¤ and powerups ¥objects of your CPowerup class¤ on each map© You decide to store the enemies and power ups in their own linked lists© Instead having to write one linked list class that handles the CEnemy objects and a second list for CPowerup objects, you can use a generic template linked list class like the one provided by the STL©

  #include <list>using namespace std;list <CEnemy> enemyList;list <CPowerup> powerupList;  


Thats all you have to do to have a fully functional list of enemies and powerups© Templates let you write code once and reuse if for a wide variety of objects, without modifying the code for different data types©

Any recent C++ book you can find will have at least a chapter on template classes and how to program them© Its probably worth your time to learn how to use them, since you'll find that there are a large number of generic data types ¥like linked lists¤ that you use all the time©

Edited by - invective on April 25, 2001 9:22:26 PM

This topic is closed to new replies.

Advertisement