20-CS-122-001 Computer Science II Spring 2012
Function Template

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Source      Lectures     (Java doesn't do this - no Java version)

#include <iostream>
using namespace std;

template < class T >
void display (T *object) {  cout << object->value() << "\n";  }

void display (int i) {  cout << i << "\n";  }

void display (const char *a) {  cout << a << "\n";  }

class Test {
   int number;
   
 public:
   Test (int i) { number = i; }
   int value () { return number; }
};

void main() {
   Test *test = new Test (23);
   display(test);
   display(45);
   display("Hello There");
}