What Are Templates? - C++ template

C++ examples for template:template function

Introduction

Templates create a general class and pass types as parameters to the template to build specific instances of the parameterized type.

You declare a parameterized List object with the template keyword.

template <class T> // declare the template and the parameter 
class List         // the class being parameterized 
{ 
public: 
   List(); 
   // full class declaration here 
}; 

The keyword template is used at the beginning of every declaration and definition of a template class.

The template's parameters follow the keyword template; they are the items that will change with each instance.

To declare an int and a Robot instance of the parameterized list class:

List<int> intList; 
List<Robot> robotList; 

Demo Code

#include <iostream> 
 
enum { kIsSmaller, kIsLarger, kIsSame}; 
 
class Data { /* w  w  w  .  jav a 2  s .  c o  m*/
public: 
    Data(int newVal):value(newVal) {} 
    ~Data() 
    { 
           std::cout << "Deleting Data object with value: "; 
           std::cout << value << "\n"; 
    } 
    int compare(const Data&); 
    void show() { std::cout << value << "\n"; } 
private: 
    int value; 
}; 
 
int Data::compare(const Data& otherObject) 
{ 
    if (value < otherObject.value) 
           return kIsSmaller; 
    if (value > otherObject.value) 
           return kIsLarger; 
    else 
        return kIsSame; 
} 
 
// Another class to put into the linked list. 
class Robot 
{ 
public: 
    Robot(int newAge): age(newAge) {} 
    ~Robot() 
    { 

        std::cout << "Deleting "; 
        std::cout << age << "-year-old robot.\n"; 
    } 
    int compare(const Robot&); 
    void show() 
    { 
        std::cout << "This robot is "; 
        std::cout << age << " years old\n"; 
    } 
private: 
    int age; 
}; 
 
// This class compares a different value than Data. 
int Robot::compare(const Robot& otherRobot) 
{ 
    if (age < otherRobot.age) 
            return kIsSmaller; 
    if (age > otherRobot.age) 
            return kIsLarger; 
    else 
        return kIsSame; 
} 
 
template <class T> 
class Node 
{ 
public: 
    Node(){} 
    virtual ~Node() {} 
    virtual Node* insert(T* object) = 0; 
    virtual void show() = 0; 
private: 
}; 
 
template <class T> 
class InternalNode: public Node<T> 
{ 
public: 
    InternalNode(T* object, Node<T>* next); 
    ~InternalNode(){ delete next; delete object; } 
    virtual Node<T> * insert(T * object); 
    virtual void show() 
    { 
           object->show(); 
           next->show(); 
    } // delegate! 
private: 
    T* object;          // the object itself 
    Node<T>* next; // points to next node in the linked list 
}; 
 
// a simple constructor 
template <class T> 
InternalNode<T>::InternalNode(T* newObject, Node<T>* newNext): 
object(newObject),next(newNext) 
{ 
} 
 
template <class T> 
Node<T>* InternalNode<T>::insert(T* newObject) 
{ 
    int result = object->compare(*newObject); 
 
    switch(result) 
    { 
    case kIsSame:   // fall through 
    case kIsLarger: // new object comes before me 
          { 
              InternalNode<T>* objectNode = 
              new InternalNode<T>(newObject, this); 
              return objectNode; 
          } 
    case kIsSmaller: 
          next = next->insert(newObject); 
          return this; 
    } 
    return this;  // appease the compiler 
} 
 
template <class T> 
class TailNode : public Node<T> 
{ 
public: 
    TailNode() {} 
    virtual ~TailNode() {} 
    virtual Node<T>* insert(T * object); 
    virtual void show() { } 
private: 
}; 
 
template <class T> 
Node<T>* TailNode<T>::insert(T * object) 
{ 
    InternalNode<T>* objectNode = 
    new InternalNode<T>(object, this); 
    return objectNode; 
} 
 
template <class T> 
class HeadNode : public Node<T> 
{ 
public: 
    HeadNode(); 
    virtual ~HeadNode() { delete next; } 
    virtual Node<T>* insert(T * object); 
    virtual void show() { next->show(); } 
private: 
    Node<T> * next; 
}; 
 
// The first node in the list, which creates the tail 
template <class T> 
HeadNode<T>::HeadNode() 
{ 
    next = new TailNode<T>; 
} 
 
template <class T> 
Node<T> * HeadNode<T>::insert(T* object) { 
    next = next->insert(object); 
    return this; 
} 
template <class T> 
class LinkedList { 
public: 
    LinkedList(); 
    ~LinkedList() { delete head; } 
    void insert(T* object); 
    void showAll() { head->show(); } 
private: 
    HeadNode<T> * head; 
}; 
 
template <class T> 
LinkedList<T>::LinkedList() { 
    head = new HeadNode<T>; 
} 
 
template <class T> 
void LinkedList<T>::insert(T* pObject) { 
    head->insert(pObject); 
} 
int main() { 
    Robot* pRobot; 
    Data* pData; 
    int val; 
    LinkedList<Robot> listOfRobots; 
    LinkedList<Data> listOfData; 
 
    // store user values in a linked list 
    while (true) 
    { 
          std::cout << "What value (0 to stop)? "; 
          std::cin >> val; 
          if (!val) 
              break; 
          pRobot = new Robot(val); 
          pData = new Data(val); 
          listOfRobots.insert(pRobot); 
          listOfData.insert(pData); 
    } 
 
    // display the list 
    std::cout << "\n"; 
    listOfRobots.showAll(); 
    std::cout << "\n"; 
    listOfData.showAll(); 
    std::cout << "\n ************ \n\n"; 
    return 0; 
}

Result


Related Tutorials