Announcements. CSCI 334: Principles of Programming Languages. Lecture 19: C++

Similar documents
Classes. Compiling Methods. Code Generation for Objects. Implementing Objects. Methods. Fields

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Implementing Object-Oriented Languages. Implementing instance variable access. Implementing dynamic dispatching (virtual functions)

Compiler construction

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

CS 403 Compiler Construction Lecture 8 Syntax Tree and Intermediate Code Generation [Based on Chapter 6 of Aho2] This Lecture

Module Mechanisms CS412/413. Modules + abstract types. Abstract types. Multiple Implementations. How to type-check?

CSE 303: Concepts and Tools for Software Development

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Announcements. CSCI 334: Principles of Programming Languages. Thanks! Lecture 23: Exam Review. Announcements. Announcements

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Semantics (cont.) Symbol Table. Static Scope. Static Scope. Static Scope. CSE 3302 Programming Languages. Static vs. Dynamic Scope

G Programming Languages - Fall 2012

Background Functions (1C) Young Won Lim 4/3/18

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Object Oriented Languages. Hwansoo Han

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (!

EECS1022 Winter 2018 Additional Notes Tracing Point, PointCollector, and PointCollectorTester

Some instance messages and methods

Data Abstraction. Hwansoo Han

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

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

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Classes. Code Generation for Objects. Compiling Methods. Dynamic Dispatch. The Need for Dispatching CS412/CS413

Programming Language Dilemma Fall 2002 Lecture 1 Introduction to Compilation. Compilation As Translation. Starting Point

Short Notes of CS201

Data, memory. Pointers and Dynamic Variables. Example. Pointers Variables (or Pointers) Fall 2018, CS2

C++ Part 2 <: <: C++ Run-Time Representation. Smalltalk vs. C++ Dynamic Dispatch. Smalltalk vs. C++ Dynamic Dispatch

CS201 - Introduction to Programming Glossary By

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Implicit differentiation

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

CPSC 427: Object-Oriented Programming

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS24 Week 3 Lecture 1

Background Functions (1C) Young Won Lim 5/26/18

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Semantics. Names. Binding Time

Dynamic Data Structures. CSCI 112: Programming in C

Run Time Environment

Programming, numerics and optimization

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Object Oriented Software Design II

Lecture 2, September 4

Generics/Templates. Steven R. Bagley

Typical Compiler. Ahead- of- time compiler. Compilers... that target interpreters. Interpreter 12/9/15. compile time. run time

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Java and C CSE 351 Spring

Constructors & Destructors

Separate Compilation Model

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Derived and abstract data types. TDT4205 Lecture 15

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

G52CPP C++ Programming Lecture 13

Object typing and subtypes

Written by John Bell for CS 342, Spring 2018

Lecture 15a Persistent Memory & Shared Pointers

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Subtyping (Dynamic Polymorphism)

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

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

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

CS 376b Computer Vision

COP 3530 Discussion Session #2 Ferhat Ay

C11: Garbage Collection and Constructors

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

Homework 4. Any questions?

Lecture 14: more class, C++ streams

CS201- Introduction to Programming Current Quizzes

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Lecture 2 8/24/18. Computer Memory. Computer Memory. Goals for today. Computer Memory. Computer Memory. Variables Assignment Sequential Steps

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

The University of Nottingham

CSE 100: C++ TEMPLATES AND ITERATORS

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

CS 132 Compiler Construction, Fall 2011 Instructor: Jens Palsberg Multiple Choice Exam, Dec 6, 2011

CS250 Final Review Questions

Using the same procedure that was used in Project 5, enter matrix A and its label into an Excel spreadsheet:

Introduction to Shape and Pointer Analysis

Object Reference and Memory Allocation. Questions:

C++ and OO. l C++ classes and OO. l More Examples. l HW2. C++ for C Programmers by Ira Pohl

CS201 Some Important Definitions

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Overriding Variables: Shadowing

CSc 328, Spring 2004 Final Examination May 12, 2004

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented?

CSE 333 Lecture smart pointers

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

C++ Programming: Polymorphism

CS250 Final Review Questions

CPSC 427: Object-Oriented Programming

CPS 506 Comparative Programming Languages. Programming Language

Transcription:

Announcements CSCI 4: Principles of Programming Languages Lecture 19: C++ HW8 pro tip: the HW7 solutions have a complete, correct implementation of the CPS version of bubble sort in SML. All ou need to do is encode the same logic in C++. Instructor: Dan Barow Announcements Dnamic Dispatch (ala Smalltalk) Superclass Point object Point class Template I ve decided to skip Java. ColoredPoint object ColoredPoint class Method dictionar Method dictionar Template

Call on ColoredPoint Superclass Point object Point class Template Optimize Dnamic Dispatch Superclass Point object Point vtable class Template 4 Method dictionar ColoredPoint object ColoredPoint class 1 Method dictionar Template 5 6 Method dictionar Static tpes let us do some optimization. Sacrificing runtime polmorphism-b-default also allows optimization. Staticall determine locations of and. Non-virtual method lookup determined staticall. Cop virtual methods from superclasses into method dictionar. Eliminate class object; just a virtual function table now. Optimize Dnamic Dispatch Call on ColoredPoint C++ virtual dispatch never searches as in SmallTalk! Point object Point vtable Point object Point vtable ColoredPoint object ColoredPoint vtable ColoredPoint object ColoredPoint vtable 1

Runtime polmorphism You ma have forgotten how OO polmorphism works. It s eas to forget with Smalltalk, which is dnamicall tped. In Java and especiall C++ ou need to think about this or it will bite ou. class Animal { string mname() { return "Animal"; ; What will the last line print? Animal *a = new Animal(); cout << a->mname() << endl; Human *h = new Human(); cout << h->mname() << endl; a = h; cout << a->mname() << endl; class Human : public Animal { string mname() { return "Human"; ; Runtime polmorphism You ma have forgotten how OO polmorphism works. It s eas to forget with Smalltalk, which is dnamicall tped. In Java and especiall C++ ou need to think about this or it will bite ou. class Animal { virtual string mname() { return "Animal"; ; What will the last line print? Animal *a = new Animal(); cout << a->mname() << endl; Human *h = new Human(); cout << h->mname() << endl; a = h; cout << a->mname() << endl; class Human : public Animal { virtual string mname() { return "Human"; ; Inheritance vs Subtping class Pirate : public Person { Pirate(string name); virtual void sahello(); Recall that inheritance and subtping are not the same. Eample: implement a stack using a dequeue. Inheritance of the form: class <subclass> : <superclass> is mere inheritance; the C++ compiler will not treat <subclass> as a subtpe of <superclass> Inheritance of the form: class <subclass> : public <superclass> is inheritance with subtping; the compiler will treat <subclass> as an instance of <superclass> when needed. Initializer Lists Pirate::Pirate(name) : Person(name) { In C++, the base class constructor is called automaticall for ou for no-argument constructors. When calling a superclass constructor with an argument from a subclass, ou must use initializer list snta. This is different from Java. You can (and should) also use the initializer list to call constructors for instance variables if the need initialization. Initializer lists onl work for instance variables that have constructors; primitives do not have constructors. For primitives, initialize the old-fashioned wa (in constructor bod).

Manual Memor Management In C++, ou need to think eplicitl about allocation and deallocation, just as ou do in C. While ou can use malloc and free in C++, ou should generall favor new and delete instead. new does more than malloc: it also calls the class constructor. delete does more than free: it also calls the class destructor. class String { private: char *s; int size; String(char *); ~String(); ; String::String(char *c) { size = strlen(c); s = new char[size+1]; strcp(s,c); Destructors constructor destructor String::~String() { delete []s; Automatic vs. Heap Allocation Tpe Inference! int main() { Pirate dan = Pirate( Dan ); dan.sahello(); allocated on the stack! access member using. Pirate *karen = new Pirate( Karen ); karen->sahello(); allocated on the heap access member using -> (remember, this is sntactic sugar) C++ has a restricted form of tpe inference. int main() { auto dan = Pirate( Dan ); dan.sahello(); auto karen = new Pirate( Karen ); karen->sahello(); auto makes life wonderful. Use it unless ou are confused about inferred tpes (in which case, write the tpe manuall)

Lambda epressions C++ has lambda epressions. The are a tad more verbose than in SML. Three main components. 1. Parameter list. Function bod.capture list 1 [ ]( ){ Lambda epressions 1 [ ]( ){ Let s rewrite this SML lambda epression in C++: fn (: int) => + 1 []( int ){ return + 1; ; Lambda epressions 1 [ ]( ){ Lambda epressions 1 [ ]( ){ Let s rewrite this SML lambda epression in C++: val = fn (: int) => + Let s rewrite this SML lambda epression in C++: val = fn (: int) => + int = ; [ ]( int ){ return + ; ; Captures b value (copies value of ) int = ; [& ]( int ){ return + ; ; Captures b reference (refers to )

Lambda epressions Lambda subtleties What is the tpe of a lambda epression? [&](int ){return + ;; This one takes an int and returns and int std::function<int(int)> More generall.. std::function<t(u 1,,U n )> Capture of closed-over lambda parameters is onl necessar for variables with automatic storage duration. (demo) Templates C++ lets ou program genericall just like Java or SML. Snta is a little different. Mechanism is ver different. class Bo { int ; Bo b = new Bo(); b-> = ; Templates No restriction on template parameter like Java (int vs Integer) Works b generating specialized code (literall, a new class) at compile-time for each parameter tpe used. template <tpename T> class Bo { T ; Bo<double> b = new Bo<double>(); b-> =.;

Templated Lambdas You can even template lambda epressions. Here s a generic identit function. template <tpename T> auto Identit = [](T ){return ;; Call it like: Identit<string>( hello ); Tpedef d Templates You can even tpedef templated tpes. Unfortunatel, the tpedef mechanism does not understand template parameters, which means that ou have to fi the template parameter if ou use it. C++0 introduced a generalization of tpedef to address this called using. std::function<bool(t,t)> Won t work; doesn t understand T: template <tpename T> tpedef Comparison std::function<bool(t,t)>; Will work: template <tpename T> using Comparison = std::function<bool(t,t)>; Net class Scala wrap-up Logic programming