Late binding Ch 15.3

Similar documents
Late binding Ch 15.3

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

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Inheritance Ch

EL2310 Scientific Programming

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

Highlights. - Making threads. - Waiting for threads. - Review (classes, pointers, inheritance)

Lecturer: William W.Y. Hsu. Programming Languages

Chapter 10 :: Data Abstraction and Object Orientation

CS 415 Midterm Exam Spring 2002

CS 430 Computer Architecture. C/Assembler Arithmetic and Memory Access William J. Taffe. David Patterson

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

QUIZ. What is wrong with this code that uses default arguments?

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Overview of Input/Output Mechanisms

COP 3330 Final Exam Review

Inheritance, Polymorphism and the Object Memory Model

Principles of Programming Languages. Lecture Outline

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Intro to C: Pointers and Arrays

Object-Oriented Design

Learning Objectives. A Meta Comment. Exercise 1. Contents. From CS61Wiki

G52CPP C++ Programming Lecture 20

CS 11 C track: lecture 5

Elementary Data Structures: Lists

Introduction to Scientific Computing

Lecture 15: Even more pointer stuff Virtual function table

Survey #2. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

CSSE 220. Interfaces and Polymorphism. Check out Interfaces from SVN

QUIZ. How could we disable the automatic creation of copyconstructors

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

SERIOUS ABOUT SOFTWARE. Qt Core features. Timo Strömmer, May 26,

Inheritance, polymorphism, interfaces

Chapter 9 :: Data Abstraction and Object Orientation

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Performance Measurement

QUIZ. How could we disable the automatic creation of copyconstructors

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

Important From Last Time

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS 106 Introduction to Computer Science I

Important From Last Time

Virtual functions concepts

Centura is Dynamic Gianluca Pivato F

accordingly. This is managed through our Other content feature.

JAVA An overview for C++ programmers

10/8/2018 Programming Data Structures. class diagram for assignment 08 review: polymorphism review: exception new: File I/O

int $0x32 // call interrupt number 50

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor


Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

MITOCW watch?v=0jljzrnhwoi

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Lecture 3 More OOP. CS 2113 Software Engineering Tim Wood. The George Washington University

CS 360 Programming Languages Interpreters

Elementary Data Structures: Lists

LinkedList Implementation Mini-project intro

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Printable View of: Week 13: Miscelaneous cool features. Returns from standard functions. returns from standard functions: scanf(), fopen()

2.4 Choose method names carefully

Software Design and Analysis for Engineers

Code Generation & Parameter Passing

Side Effects (3B) Young Won Lim 11/20/17

Compilers and Interpreters

Computer Systems and Networks

Binary Trees

Homework #2 Think in C, Write in Assembly

VIRTUAL FUNCTIONS Chapter 10

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Object Oriented Programming with c++ Question Bank

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Lecture #6 Intro MIPS; Load & Store

CS Lecture #6. Administrative... Assignment #2 Graded NN Turned In, Average Grade: XX Assignment #3 due on Thursday

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Inheritance. Chapter 15 & additional topics

Basics Inheritance and Instance Variables Inheritance and Methods Class Variables. Ruby Inheritance CSCI September 2017.

Classes, Objects, and OOP in Java. June 16, 2017

COP 3530 Discussion Session #2 Ferhat Ay

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Graphical User Interface (GUI)

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

CS61B Lecture #8: Object-Oriented Mechanisms

CSC207 Week 3. Larry Zhang

Chapter 3-Camera Work

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

QUIZ. Can you find 5 errors in this code?

QUIZ Friends class Y;

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

CS61C Machine Structures. Lecture 8 - Introduction to the MIPS Processor and Assembly Language. 9/14/2007 John Wawrzynek

Pedal Box / Pedal Board Usage Manual Table of Contents

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

Transcription:

Late binding Ch 15.3

Highlights - Late binding for variables - Late binding for runctions

Review: Derived classes Today we will deal more with inheritance Mainly we will focus on how you can store a child class in a parent container (sort of) Questions we will answer: What is this line of code doing exactly? Are there other ways of doing this?

Early vs late binding Static binding (or early) is when the computer determines what to use when you hit the compile button Dynamic binding (late) is when the computer figures out the most appropriate action when it is actually running the program Much of what we have done in the later parts of class is similar to late binding

Static binding When you go to a fast-food-ish restaurant, you get one tray, regardless of what you order The key is before they knew what you were ordering, they determined you needed one tray

Dynamic binding When you order a drink, they do not just give you a standard cup and say fill to this line Now, they have to react to what you want and give you the correct cup size (not a predetermined action, thus dynamic binding)

Static binding Checking out at a grocery store, all items are scanned and added to the bill in the same way The same program on the computer runs for all items and just identifies their price

Dynamic binding After you pay, you put the food into bags (paper/plastic/your own) Both What items go where depends on what you want to use and the item properties (weight, dampness, rigidness, etc.)

Static/dynamic binding Consider this code: You know the output even before the program runs (you know at compile time = static) While this code, you only know the output when the program runs (i.e. dynamic): (See: complevsrun.cpp)

Static/dynamic binding static = rigid/constant dynamic = flexible/adaptive

Static/dynamic binding Static/dynamic binding is similar to how we originally made arrays: (static/early binding) To dynamic memory arrays: (dynamic/late)

Mini-quiz (ungraded) What is in p at end of main()? 1. x=2 2. x=2, y=10 3. x=1, y=10 4. x=1 (Hint: what happens on this:)

= between parent/child It is debatable how we should interpret line: In C++ (not some other languages), this just copies the parts of the parent class over Parent Parent = int x = 2 int x = 2 p Child int y = 10 c

Mini-quiz (ungraded) What is at p now? 1. x=2 2. x=2, y=10 3. x=1, y=10 4. x=1

= between parent/child pointers When the objects are pointers, lines line just changes the object being pointed to (but not any information inside either class) Parent Parent int x = 1 int x = 2 Parent* Child* = go go p c Child int y = 10

Dynamic variable binding If a Parent type is pointing to a Child instance, we cannot directly access them (variables cannot be virtual...) Instead, we have to tell it to act like a Child* by casting it: (bad practice as y public) (see: dynamicobject.cpp)

Dynamic variable binding If p points to a Parent instance, the below line is VERY BAD (but it might work... sorta...) You will be fooling around in some part of memory that is not really associated p (though you might not crash...) (see: badmemorymanagement.cpp) (see: memoryoops.cpp)

Break

Dynamic binding Consider this relationship:

Dynamic binding Tell each of them to swing()!

Dynamic function binding Who's swing function is being run?

Dynamic function binding Who's swing function is being run? Answer: the Person's If you have normal variables, p=b only copies b's Person parts into p's Person box, so you still only have one swing function

Dynamic function binding Who's swing function is being run now?

Dynamic function binding Who's swing function is being run now? Answer: the Person's still... p is pointing to a full Boxer object, but it only thinks there is the Person part due to type (see: incorrectchildfunction.cpp)

Dynamic function binding If we want the computer to not simply look at the type of pointer and instead determine what action to take based on the object...... we need to add virtual (this is slower) (see: dynamicbindingfunctions.cpp)

Dynamic function binding If you use a function to run an object and you want to use virtualization, you need to pass-by-reference (i.e. use an &) If you do not, it will make a copy an this will ignore the Child's part Can be Person, Always a Person Boxer or Baseballer

Dynamic function binding If you want to use this virtualization: 1. Pass in a pointer 2. Pass by reference (i.e. use &) Needs to be memory address so the computer can look at what type is actually there If you give it a Parent box, it cannot do anything but run normal Parent stuff (see: dynamicbindingfunctionv2.cpp)

virtual deconstructors If you use Parent* to dynamically create a instance of a Child class, by default it will ONLY run the parent's deconstructor With a virtual deconstructor it will run the deconstructor for whatever it is pointing at (the Child's deconstructor in this case) Thus it avoids memory leak (see: yetanothermemoryleak.cpp)