Distinguishing logic from data type Generics Distinguishing logic from data type Copyright © 2006-2017 Curt Hill
History 1950s 1960s 1970s 1980s 1990s Assemblers High level languages FORTRAN, ALGOL, COBOL, BASIC, etc 1970s Structured languages, C, Pascal 1980s Object oriented languages Smalltalk, C++ and others 1990s Generics in C++ and Ada Copyright © 2006-2017 Curt Hill
Basic Idea There are a number of functions and classes where the data type being used does not matter Consider several screens of examples Copyright © 2006-2017 Curt Hill
Linked List Destructor LinkedList::~LinkedList(){ if(root == NULL) return; // Walk the list deleting everything LinkedNode * ptr = root; while(ptr){ // != NULL) { root = ptr; ptr = ptr -> next; delete root; } root = NULL; Copyright © 2006-2017 Curt Hill
Questions What kind of data did the list carry? Do not know and do not care The logic is the same in all cases Copyright © 2006-2017 Curt Hill
Linked List ToString wxString LinkedList::ToString (){ wxString result = ""; if(root == NULL) return "Empty\n"; LinkedNode * ptr=root; while (ptr) { result = result << wxString("Key: ") << ptr->key << ", data: " << ptr->data << "\n"; ptr = ptr->next; } return result; Copyright © 2006-2017 Curt Hill
Questions What kind of data is in key and data? We still do not know We do know that the data is subject to being converted to a wsString with the put-to operator This could be any primitive, a C-style string or a few objects Copyright © 2006-2017 Curt Hill
Linked List Find bool LinkedList::find(? k, ? & s){ LinkedNode * ptr = root; while(ptr){ if(ptr->key == k){ s = ptr->data; return true; } ptr = ptr->next; return false; Copyright © 2006-2017 Curt Hill
Questions What kind of data is in k, d, key and data? We could know because it must be declared We do know that the data is suitable for comparison and assignment Copyright © 2006-2017 Curt Hill
C++ Has the template feature to implement generics Consider the sort The code is mostly the same regardless of whether an array of ints or an array of doubles is in view Consider the linked list The code is mostly the same regardless of whether the list carries strings or floats Thus generics separate logic from data type Copyright © 2006-2017 Curt Hill
Templates The reserved word for these is template This is usually followed by a parameter list: template <class TYPE> thing class precedes the generic name of the type TYPE is the generic type The thing is a class or function There may be multiple types as well Copyright © 2006-2017 Curt Hill
More An alternative to the class reserved word is the typename reserved word This was introduced in a later C++ standard This better signifies that the parameterizing type may be: A class A primitive type Anything named by a typedef This true regardless of whether class or typename is used Copyright © 2006-2017 Curt Hill
Process First we will examine template functions They are easier Second we will consider template classes Similar but somewhat more involved Copyright © 2006-2017 Curt Hill