Data types. Definitions Aggregate constructors User-defined type definitions Types and storage Types and subtypes Type systems and type checking

Similar documents
Fundamentals of Programming Languages

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Structuring the Data. Structuring the Data

Introduction to Programming Using Java (98-388)

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Object-oriented Programming. Object-oriented Programming

CMSC 4023 Chapter 11

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

CSE 431S Type Checking. Washington University Spring 2013

Chapter 11. Abstract Data Types and Encapsulation Concepts

C++ (Non for C Programmer) (BT307) 40 Hours

Programmiersprachen (Programming Languages)

COMP 2355 Introduction to Systems Programming

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

Evolution of Programming Languages

Abstract Data Types and Encapsulation Concepts

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Chapter 11. Abstract Data Types and Encapsulation Concepts

Sub- PPL Unit-II Class-SE Comp

10. Abstract Data Types

Basic Types & User Defined Types

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Chapter 11. Abstract Data Types and Encapsulation Concepts

Types. What is a type?

G Programming Languages - Fall 2012

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

CS201 Some Important Definitions

Programming Languages Third Edition. Chapter 7 Basic Semantics

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

CPS 506 Comparative Programming Languages. Programming Language

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Object Oriented Software Design II

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

What are the characteristics of Object Oriented programming language?

Where do we go from here?

Ch. 11: References & the Copy-Constructor. - continued -

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Absolute C++ Walter Savitch

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

2 ADT Programming User-defined abstract data types

STRUCTURING OF PROGRAM

Binding and Variables

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

CS304 Object Oriented Programming Final Term

Type Systems, Type Inference, and Polymorphism

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑 ISBN

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Informatica 3 Syntax and Semantics

Object-Oriented Programming

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Francesco Nidito. Programmazione Avanzata AA 2007/08

VALLIAMMAI ENGINEERING COLLEGE

Chapter 11 Object and Object- Relational Databases

Implementing Subprograms

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Special Member Functions

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

And Even More and More C++ Fundamentals of Computer Science

C# Adds Useful Features


Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CLASSES AND OBJECTS IN JAVA

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Programming Languages

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Pierce Ch. 3, 8, 11, 15. Type Systems

OBJECT ORIENTED PROGRAMMING USING C++

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Properties of an identifier (and the object it represents) may be set at

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Ch. 12: Operator Overloading

CSCI-GA Scripting Languages

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

More C++ : Vectors, Classes, Inheritance, Templates

Object Oriented Programming. Solved MCQs - Part 2

Programming Languages

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Polymorphic (Generic) Programming in Java

Lecture Notes on Programming Languages

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

CSE 307: Principles of Programming Languages

Other Features of Procedures

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

IEEE LANGUAGE REFERENCE MANUAL Std P1076a /D3

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Types and Type Inference

Object-Oriented Programming

C and C++ 7. Exceptions Templates. Alan Mycroft

Transcription:

Data types Definitions Aggregate constructors User-defined type definitions Types and storage Types and subtypes Type systems and type checking Ghezzi&Jazayeri: Ch 3 1

Data types Definition: a set of values + set of operations, e.g.. Boolean, integer Program entities (or objects) are instances of types: x, y, z: T; or T x, y, x; Ghezzi&Jazayeri: Ch 3 2

Primitive and composite types Primitive (atomic) types are atomic types (values usually defined by language/machine) Compound types have components (values are formed from components) Built-in types are provided by language (NB: built-in types may be not primitive, e.g. string) Type constructors are used to define new types Ghezzi&Jazayeri: Ch 3 3

Purpose of types Classification of the data manipulated by program c: centigrade; f: fahrenheit; Protection from illegal operations f := c and f; -- should not be allowed Ghezzi&Jazayeri: Ch 3 4

Advantages of types (1) Hiding of underlying representation name versus structure specification versus implementation independence of client from server: change implementation without changing interface and use modifiability and portability Ghezzi&Jazayeri: Ch 3 5

Advantages of types (2) Correct use of variables can be checked at compile time (if type is known) x := i/j can be checked for type correctness but not j 0 Resolution of overloaded operators at compile-time (if type is known) x := y * z; Ghezzi&Jazayeri: Ch 3 6

Defining the values of a new type Enumeration (also subrange): type color = (white, yellow, red, green) What about operations? succ, pred, < (ordered elements, by position and value) Ghezzi&Jazayeri: Ch 3 7

Aggregates Set of values, one name Homogeneous versus heterogeneous aggregates Constructors for compound values and types Operations on entire aggregate Selection of components possible Ghezzi&Jazayeri: Ch 3 8

Aggregate constructors Cartesian product: A1 A2...An Ordered n-tuples (a1, a2,..., an) Examples: complex numbers point (x, y) record, struct,... Ghezzi&Jazayeri: Ch 3 9

Aggregate constructors Finite mapping (domain is finite) map: DT RT Arrays of most languages Is DT fixed at compile-time or instantiation time or object manipulation time? Is DT only subrange of integers? Is DT extensible at runtime? Ghezzi&Jazayeri: Ch 3 10

Aggregate constructors (Discriminated) union A1 A2 A3 Disjunction of values What is the type of the object at runtime? Requires runtime type checking for correct operation union u (int i, float f); Ghezzi&Jazayeri: Ch 3 11

Aggregate constructors Recursive structures (unbound size) struct int_list { int val; int_list* next; }; val int_list* head; val head Ghezzi&Jazayeri: Ch 3 12

Problems with pointers Pointers are often used to implement recursive structures Pointers establish a dependency between two objects. It is important for the object to exist as long as the pointer points to it Memory leaks (object exists, but can t be reached) Dangling pointers (pointer to deallocated area) Ghezzi&Jazayeri: Ch 3 13

Dangling pointers int* px; void trouble (); { int x;...px= &x;... return; } main( ){ trouble (); /* now x deallocated px dangling */ Ghezzi&Jazayeri: Ch 3 14

Pointer examples in C++ int j = 3; const int k = 99; int*p = &j; *p = 0; const int*q = &k; --pointer to a constant *q = 10; illegal q = &j; legal --questionable but true const int*const pc = &k; --const ptr to const pc = &j; illegal Ghezzi&Jazayeri: Ch 3 15

References in C++ A variable declared to be a T& (reference to type T) must be initialized by an object of type T (or convertible to a T) inti; int& r = i; //r and i share the same object, i.e.the l-value is the same r=1 //changes value of i to 1 int& rr = r; //same object shared by rr, r, i a reference cannot be changed after initialization Ghezzi&Jazayeri: Ch 3 16

Type constructor: routine int foo (int n, float f) {... } Signature: foo : int float int Can you have variables of type, e.g.. int float int? (first class objects) or pointers to such objects? int (*ps)(int, float); ps = &foo; Can you express generic procedures? Ghezzi&Jazayeri: Ch 3 17

Parameter passing in C++ void f(int val, int& ref) { val++; ref++; } val is by value only local copy is incremented ref is by reference actual parameter is affected an argument of type T[] converted to a T* when passed (arrays cannot be passed by copy) Ghezzi&Jazayeri: Ch 3 18

User-defined types struct complex { float real_part, imaginary_part; }... complex a, b, y; variable can be declared and instantiated no protection from illegal (undesired) operations this way, attributes are public Ghezzi&Jazayeri: Ch 3 19

A C++ example Abstract data types class point { private: float x, y; public: point (float a, float b) {x = a; y = b;} void x_move (float a) {x += a;} void y_move (float b) {y += b;} void reset () { x = y = 0.0;} }; Ghezzi&Jazayeri: Ch 3 20

ADTs point p1 (1.3, 3.7); point p2 (53.3, 0.0); point * p3 = new point(0.0, 0.0); point p4= point(1.3, 3.7); p1.x_move(9.3); *p3 = p2; Public versus private part Implementation versus specification Hidden representation Ghezzi&Jazayeri: Ch 3 21

First class types Is point a first class type? Can I pass a point as argument? Can I write a function to return a copy of a point? point copy (point p) { point temp; temp = p; return temp; };... p2 = copy (p1) Ghezzi&Jazayeri: Ch 3 22

The class construct in C++ Definition and implementation can be compiled separately WARNING: definition interface! Clients must be compiled after servers (interfaces) Names local to a class implicitly private, unless declared public Ghezzi&Jazayeri: Ch 3 23

The stack example class stack { private int* p; int* top; int size; public: stack (int n) {top = p = new int[size = n];} ~stack() {delete [ ] p;}} destructor void push(int i) {(*top++) = i;} int pop() {return *--top;}. Ghezzi&Jazayeri: Ch 3 24

Object lifetime At construction: Object allocated, then attributes initialized as specified by constructor allocation automatic for stack objects or explicit (via new) At desctruction: Object attributes cleaned, then object deallocated deallocation automatic for stack objects or explicit (via delete) Ghezzi&Jazayeri: Ch 3 25

Constructor Same name as type being defined There can be many, with different signatures Default constructors are provided if none explicitly defined Ghezzi&Jazayeri: Ch 3 26

Copy constructor Special kind of constructor Differs from assignment (object does not exist yet) Builds an object from an existing one, whose copy is constructed Used also for parameter passing (by value) point(const point& p) { /* x and y private parts of the object itself */ x = p.x; y = p.y; } Ghezzi&Jazayeri: Ch 3 27

Copy constructor Can use the two notations point p1=p2; or point p1(p2) Signature is point(const point&) Ghezzi&Jazayeri: Ch 3 28

Destructor Name of class prefixed by ~ Performs cleanup actions after last use of an object There can be only one destructor for a class Default destructor exists if not provided Ghezzi&Jazayeri: Ch 3 29

Assignment for classes C++ uses memberwise copy of attributes by default; copy is shallow Works okay for point but... Ghezzi&Jazayeri: Ch 3 30

Memberwise copy and pointers void f() { stack s1(20), s2(30); s1 = s2; //array referred to by s1 is lost //it is not deleted upon exit from block }; Predefined assignment operator performs memberwise assignment also for pointers Need a customized assignment operator Ghezzi&Jazayeri: Ch 3 31

Assignment operation for objects class stack {... public: stack (int n) {top = p = new int[size = n];} ~stack() {delete [ ] p;} stack& operator = (const stack&);... an assignment operator with this signature is typically present Ghezzi&Jazayeri: Ch 3 32

Assignment operation for objects stack& stack::operator=(const stack& s) { if (this!= &s) { //to cover s = s; delete [ ] p; p = s.p; top = s.top; size = s.size; return *this; this: pointer to current object } } This is the typical predefined assignment but for stacks you need actual cloning Ghezzi&Jazayeri: Ch 3 33

How to assign by cloning? stack& stack::operator=(const stack& s) { if (this!= &s) { //to cover s = s; delete [ ] p; } } p = new int[size= s.size]; NB: a new one is allocated top = p + s.top - s.p; for (int i = 0; i<(top-p); ++i) {p[i] = s.p[i];} return *this; Ghezzi&Jazayeri: Ch 3 34

Copy constructor for stack stack::stack(const stack& s){ p = new int[size= s.size]; top = p + s.top - s.p; for (int i = 0; i<(top-p); ++i) { p[i] = s.p[i]; } } Ghezzi&Jazayeri: Ch 3 35

ADTs in C++: summing up Constructor: to initialize the object Destructor: to clean up after object Public part (excluding method bodies): spec Private data and functions (and method bodies): implementation (default is private) Ghezzi&Jazayeri: Ch 3 36

Friend functions in C++ Class vector and matrix are given We wish to define operation to multiply a vector and a matrix: where should it be placed? Does it belong to matrix? Does it belong to vector? If global, it has no access to private attributes Can be declared friend of both Ghezzi&Jazayeri: Ch 3 37

Given classes class matrix {... friend vector mult (const matrix&, const vector&); //can be placed in either private or public part } class vector {... friend vector mult (const matrix&, const vector&); //can be placed in either private or public part } Ghezzi&Jazayeri: Ch 3 38

Friend function vector mult (const matrix& m, const vector& v); { //assume 0..3 matrix and vector vector r; for (int i = 0; i<3, i++) { //r[i] = m[i] * v r.elem(i) = 0 for (int j = 0, j<3; j++) r.elem(i) += m.elem(i, j) * v.elem(j); } return r; } Ghezzi&Jazayeri: Ch 3 39

Objects and visibility stack s1(20); can perform push and pop s1.push(5); int i = s1.pop(); top, size, and p are hidden stack * s1 = new stack (20); can dereference and then use push, pop *s1.push(5); abbreviated as s1-> push(5) Ghezzi&Jazayeri: Ch 3 40

Generic (or parameterized) ADTs template<class T> class Stack{ public: Stack(int sz) {top = s = new T[size = sz];} ~Stack() {delete[ ] s;} //destructor void push(t el){*top++ = el;} T pop() {return *--top;} int length() {return top - s;} private: int size; T* top; T* s; }; Ghezzi&Jazayeri: Ch 3 41

Use of generic ADT void foo() { Stack<int> int_st(30); Stack<item> item_st(100);... int_st.push(9);... } Ghezzi&Jazayeri: Ch 3 42

C++: not only classes C++ modularizes a system also based on functions, not only classes Functions may be generic generic algorithms Ghezzi&Jazayeri: Ch 3 43

A generic C++ function template <class T> void swap(t& a, T& b) { Ttemp= a; a = b; b = temp; } int i, j; char x, y; pair<int,string> p1, p2; //pair is a generic class //p1, p2 are non-generic pairs... swap(i, j); //swap integers swap(x, y); //swap characters swap(p1, p2); //swap pairs Ghezzi&Jazayeri: Ch 3 44

Java Generics (1) class gstack<t> { private List<T> stack = new ArrayList<T>(); public void push(t a) { stack.add(a); }; public T top() { return stack.get(stack.size()-1);}; public void pop() { stack.remove(stack.size()-1); }; public void print() { System.out.print("["); Iterator<T> iterator = stack.iterator(); for (T el : stack) System.out.print(el + " "); System.out.println("]"); } } Ghezzi&Jazayeri: Ch 3 45

Java Generics (2) class Example { public static void main(string [] args) { gstack<integer> IntegerStack = new gstack<integer>(); } } IntegerStack.push(3); IntegerStack.push(4); IntegerStack.push(5); IntegerStack.print(); System.out.println("top = " + IntegerStack.top()); IntegerStack.pop(); IntegerStack.print(); Ghezzi&Jazayeri: Ch 3 46

Java Generics - Implementation Generics are implemented by the Java compiler as a front-end conversion called erasure Erasure is the process of translating or rewriting code that uses generics into non-generic code Thus erasure maps the new syntax to the current JVM specification This conversion erases all generic type information all information between angle brackets is erased. For example, LinkedList<Integer> will become LinkedList. when the resulting code is not type correct, a cast to the appropriate type is inserted. Ghezzi&Jazayeri: Ch 3 47

Java Generics vs. C++ templates Java generics look like the C++ templates, but they are not the same. Java Generics They provide compile-time type safety and eliminate the need for casts. The main difference is encapsulation: errors are flagged where they occur and not later at some use site, and source code is not exposed to clients. Use type erasure: the compiler keeps track of the generics internally, and all instances use the same class file at compile/run time. C++ template They are macros Whenever a template class is instantiated with a new class, the entire code for the class is reproduced and recompiled for the new class. Ghezzi&Jazayeri: Ch 3 48

Type systems (1) Type system is set of rules used by language to structure and organize its types Objects are instances of types Incorrect use of an object is a type error A program is type safe if it has no type errors Ghezzi&Jazayeri: Ch 3 49

Type systems (2) A type system is strong if it guarantees type safety Programs written according to the rules of the type system are guaranteed not to contain type errors A language with strong type system is strongly typed If a language is strongly typed the compiler can guarantee type safety (static checking) Ghezzi&Jazayeri: Ch 3 50

Static vs dynamic type checking Static checking: done by compiler before program execution Dynamic checking: requires program execution Static type checking advantageous in terms of program correctness (independent of input data) and efficiency of checking (dynamic checks slow down execution) Ghezzi&Jazayeri: Ch 3 51

Static vs strong typing Static type system: expressions bound to a type at compile time Dynamic type system polymorphic A static type system is strong A strong type system is NOT necessarily static (do not confuse a static type system with static type checking!) There are languages that are polymorphic and yet strongly typed (we will see the case of inheritance in OO languages) Ghezzi&Jazayeri: Ch 3 52

Issues defined by type systems Type compatibility When can an object of type T1 be used instead of an expected object of type T2? Type conversions How/when an object of type T1 can be transformed into an object of type T2? Types and subtypes What kind of compatibility rules between type and subtype? Ghezzi&Jazayeri: Ch 3 53

Type issues: compatibility (or conformance, or equivalence) Type T1 is compatible with type T2 with respect to operation O if it can be used in place of T2 in operation O x: T1; y: T2; f(t2) {...} y = x;... f (x); Ghezzi&Jazayeri: Ch 3 54

Type compatibility rules Within the context of an operation defined for an object of type T, only type T is acceptable Also called name compatibility (a type name is compatible only with itself) Structural compatibility two different struct types with fields having the same types (e.g., COMPLEX and COORDINATE) are compatible Ghezzi&Jazayeri: Ch 3 55

Type issues: conversions Conversions(casting) fun: T1 R1 x: T2; y: R2; How to call: y = fun(x);? Use of conversion routines t21 and r12: t21: T2 T1 r12: R1 R2 y = r12 (fun (t21 (x))); Calledcoercion when performed automatically and implicitly by the compiler Warning: interaction between coercion and overloading may lead to obscure programs and unexpected behavior Ghezzi&Jazayeri: Ch 3 56

Types and subtypes type natural = 0..maxint; digit = 0..9; small = -9.. 9; Compatibility rules? Substitutability. x: integer; n: natural; d: digit; s: small;... x := n; --okay n := x; --? requires runtime check Ghezzi&Jazayeri: Ch 3 57

Monomorphic vs polymorphic TS Monomorphic type systems All program entities are typed Every operation accepts operands only of exactly the defined type strongly typed but too strict, no flexibility Polymorphic type systems Some program objects can belong to more than one type All practical prog. lang. provide some polymorphism Goal: combine polymorphism ( flexibility) + strong typing + efficient, preferably static type checking Ghezzi&Jazayeri: Ch 3 58

Classification of polymorphism (the case of routines) Ad hoc: finite (and small) set of types Universal: infinite set of types poly morphism universal ad hoc parameter (generic routines) inclusion (e.g. subtyping) overloading coercion Ghezzi&Jazayeri: Ch 3 59