Distinguishing logic from data type

Slides:



Advertisements
Similar presentations
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Comp 205: Comparative Programming Languages Imperative Programming Languages Functional Programming Languages Semantics Other Paradigms Lecture notes,
Abstract Data Types and Encapsulation Concepts
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Copyright © Curt Hill Structured Data What this course is about.
Copyright Curt Hill Variables What are they? Why do we need them?
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
Copyright © Curt Hill Inheritance in C++ How to do it.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Copyright © Curt Hill Flow of Control A Quick Overview.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Batch Files Flow of Control to Strengthen Copyright © by Curt Hill.
Copyright © Curt Hill Generic Functions Separating Data Type from Logic.
Copyright © 2004 – 2006 – Curt Hill Linked Lists Very Powerful Data Structure.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
The for Statement A most versatile loop
Copyright © 2016 Curt Hill Static Code Analysis What it is and does.
Motivation for Generic Programming in C++
C++ Lesson 1.
Inheritance in C++ How to do it Copyright © Curt Hill
Static Code Analysis What it is and does. Copyright © 2016 Curt Hill.
Part of the Assembler Language Programmers Toolbox
A Review or Brief Introduction
Abstract Data Types and Encapsulation Concepts
C++ Templates.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 6: Data Types Lectures # 10.
More important details More fun Part 3
Very Powerful Data Structure
Comp 205: Comparative Programming Languages
11.1 The Concept of Abstraction
Type Systems Terms to learn about types: Related concepts: Type
The Prototyping Paradigm
Abstract Data Types and Encapsulation Concepts
Chap. 6 :: Control Flow Michael L. Scott.
Methods Additional Topics
A brief look at some of the new features introduced to the language
A Sorted, Unique Key Container
Abstract Data Types and Encapsulation Concepts
Building Java Programs
Building Java Programs
Objects as Variables Featuring the Date Object
Arrays in Java What, why and how Copyright Curt Hill.
Throwing and catching exceptions
Introduction to Primitive Data types
Chap. 6 :: Control Flow Michael L. Scott.
Variables, Identifiers, Assignments, Input/Output
Dynamic Data Structures
A Robust Data Structure
Building Java Programs Chapter 2
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Python Primer 1: Types and Operators
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
The Java Alternative to Multiple Inheritance
The Java switch Statement
Java’s Central Casting
Building Java Programs Chapter 2
Type Systems Terms to learn about types: Related concepts: Type
Introduction to Primitive Data types
11.1 The Concept of Abstraction
A dictionary lookup mechanism
Methods Coding in Java Copyright © Curt Hill.
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

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