Cpp - Use template items as any other type.

Introduction

You can use template items as any other type.

You can pass them as parameters, either by reference or by value, and you can return them as the return values of functions, by value or by reference.

Demo

#include <iostream> 
class Data//w  w  w  .j  av a2 s . c  om
{
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 -1;
  if (value > otherObject.value)
    return 1;
  else
    return 0;
}

class Dog
{
public:
  Dog(int newAge) : age(newAge) {}
  ~Dog()
  {
    std::cout << "Deleting " << age
      << "-year-old Dog.\n";
  }
  int compare(const Dog&);
  void show()
  {
    std::cout << "This Dog is " << age
      << " years old\n";
  }
private:
  int age;
};

int Dog::compare(const Dog& otherDog)
{
  if (age < otherDog.age)
    return -1;
  if (age > otherDog.age)
    return 1;
  else
    return 0;
}

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* theObject, Node<T>* next);
  virtual ~InternalNode() { delete next; delete object; }
  virtual Node<T>* insert(T* object);
  virtual void show()
  {
    object->show();
    next->show();
  }
private:
  T * object;
  Node<T>* next;
};

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 0:
  case 1:
  {
    InternalNode<T> * objectNode =
      new InternalNode<T>(newObject, this);
    return objectNode;
  }
  case -1:
    next = next->insert(newObject);
    return this;
  }
  return this;
}

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;
};

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);
}

void myFunction(LinkedList<Dog>& listOfDogs);
void myOtherFunction(LinkedList<Data>& listOfData);

int main()
{
  LinkedList<Dog> listOfDogs;
  LinkedList<Data> listOfData;

  myFunction(listOfDogs);
  myOtherFunction(listOfData);

  std::cout << "\n";
  listOfDogs.showAll();
  std::cout << "\n";
  listOfData.showAll();
  return 0;
}

void myFunction(LinkedList<Dog>& listOfDogs)
{
  Dog* pDog;
  int val;

  while (true)
  {
    std::cout << "\nHow old is your Dog (0 to stop)? ";
    std::cin >> val;
    if (!val)
      break;
    pDog = new Dog(val);
    listOfDogs.insert(pDog);
  }
}

void myOtherFunction(LinkedList<Data>& listOfData)
{
  Data* pData;
  int val;

  while (true)
  {
    std::cout << "\nWhat value (0 to stop)? ";
    std::cin >> val;
    if (!val)
      break;
    pData = new Data(val);
    listOfData.insert(pData);
  }
}

Result

Related Topic