Presentation is loading. Please wait.

Presentation is loading. Please wait.

Distinguishing logic from data type

Similar presentations


Presentation on theme: "Distinguishing logic from data type"— Presentation transcript:

1 Distinguishing logic from data type
Generics Distinguishing logic from data type Copyright © Curt Hill

2 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 © Curt Hill

3 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 © Curt Hill

4 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 © Curt Hill

5 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 © Curt Hill

6 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 © Curt Hill

7 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 © Curt Hill

8 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 © Curt Hill

9 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 © Curt Hill

10 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 © Curt Hill

11 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 © Curt Hill

12 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 © Curt Hill

13 Process First we will examine template functions
They are easier Second we will consider template classes Similar but somewhat more involved Copyright © Curt Hill


Download ppt "Distinguishing logic from data type"

Similar presentations


Ads by Google