Class 16: Function Parameters and Polymorphism



Similar documents
Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

C++FA 5.1 PRACTICE MID-TERM EXAM

Comp151. Definitions & Declarations

An Incomplete C++ Primer. University of Wyoming MA 5310

Chapter 5 Names, Bindings, Type Checking, and Scopes

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

1. Polymorphism in C++...2

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Chapter 5 Functions. Introducing Functions

Object Oriented Software Design II

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Parameter passing in LISP

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Object-Oriented Programming

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

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

C++FA 3.1 OPTIMIZING C++

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

C++ Overloading, Constructors, Assignment operator

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Java Interview Questions and Answers

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

History OOP languages Year Language 1967 Simula Smalltalk

C++ INTERVIEW QUESTIONS

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

CS 106 Introduction to Computer Science I

Object Oriented Software Design

Parameter Passing Methods

Semantic Analysis: Types and Type Checking

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Compiler Construction

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Object Oriented Software Design II

The C Programming Language course syllabus associate level

AP Computer Science Java Subset

Basics of I/O Streams and File I/O

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value

Habanero Extreme Scale Software Research Project

Function Overloading I

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

CSC Software II: Principles of Programming Languages

Moving from CS 61A Scheme to CS 61B Java

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Object Oriented Software Design

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Crash Course in Java

CSE 130 Programming Language Principles & Paradigms

13 Classes & Objects with Constructors/Destructors

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Syllabus OBJECT ORIENTED PROGRAMMING C++

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

Functional Programming

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Ch 7-1. Object-Oriented Programming and Classes

Install Java Development Kit (JDK) 1.8

Konzepte objektorientierter Programmierung

CSCI 3136 Principles of Programming Languages

Chapter 9 Delegates and Events

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

COSC 181 Foundations of Computer Programming. Class 6

PostgreSQL Functions By Example

Chapter 1 Fundamentals of Java Programming

CIS 190: C/C++ Programming. Polymorphism

CS193j, Stanford Handout #10 OOP 3

Lecture 1: Introduction

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Introduction to Java. CS 3: Computer Programming in Java

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Lecture 5: Java Fundamentals III

J a v a Quiz (Unit 3, Test 0 Practice)

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

COS 301 Programming Languages

Introduction to Programming Block Tutorial C/C++

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Chapter 13 - Inheritance

Chapter 9. Subprograms

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Transcription:

Class 16: Function Parameters and Polymorphism SI 413 - Programming Languages and Implementation Dr. Daniel S. Roche United States Naval Academy Fall 2011 Roche (USNA) SI413 - Class 16 Fall 2011 1 / 15

Parameter Passing Modes Our programs are littered with function calls like f (x, 5). This is a way of passing information from the call site (where the code f(x,5) appears) to the function itself. The parameter passing mode tells us how the information about the arguments (e.g. x and 5) is communicated from the call site to the function. Roche (USNA) SI413 - Class 16 Fall 2011 2 / 15

Pass by Value C/C++ use pass by value by default. Java uses it for primitive types (int, boolean, etc.). v o i d f( i n t a) { a = 2*a; print (a); i n t main () { i n t x = 5; f(x); print (x); r e t u r n 0; What does this C++ program print? Roche (USNA) SI413 - Class 16 Fall 2011 3 / 15

Pass by Value C/C++ use pass by value by default. Java uses it for primitive types (int, boolean, etc.). v o i d f( i n t a) { a = 2*a; print (a); i n t main () { i n t x = 5; f(x); print (x); r e t u r n 0; What does this C++ program print? 10, then 5 Roche (USNA) SI413 - Class 16 Fall 2011 3 / 15

Pass by Value In this scheme, the function recieves copies of the actual parameters. The function cannot modify the originals, only its copies, which are destroyed when the function returns. Function arguments represent one-way communication from call site to function. (How can the function communicate back?) Roche (USNA) SI413 - Class 16 Fall 2011 4 / 15

Pass by Reference C++ supports reference parameters. Perl and VB use this mode by default. sub foo { $_ [0] = haha changed by f o o ; my $y = t h i s i s mine! ; foo ($y ); p r i n t $y, \n ; You can guess what this Perl program prints... Roche (USNA) SI413 - Class 16 Fall 2011 5 / 15

Pass by Reference C++ supports reference parameters. Perl and VB use this mode by default. sub foo { $_ [0] = haha changed by f o o ; my $y = t h i s i s mine! ; foo ($y ); p r i n t $y, \n ; You can guess what this Perl program prints... Similar behavior happens if we wrote void f(int &a) {... in the previous C++ example. Roche (USNA) SI413 - Class 16 Fall 2011 5 / 15

Pass by Reference The formal parameters of the function become aliases for the actual parameters! Normally the actual parameters must be variable names (or more generally, l-values... ). Function arguments now represent two-way communication. References provide a way for functions to return multiple things. They also avoid the (potentially expensive) overhead of copying without having to deal with pointers. This is especially useful with (large) data structures stored in objects. Roche (USNA) SI413 - Class 16 Fall 2011 6 / 15

Variations Pass by Value/Result The initial value is passed in as a copy, and the final value on return is copied back out to the actual parameter. Behaves like pass-by-reference, unless the actual parameter is accessed during the function call.. Pass by Sharing This is what happens with objects in Java. Actual and formal parameters both reference some shared data (i.e., the object itself). But they are not aliases; functions can change the object that is referenced, but cannot set which object is referenced. Roche (USNA) SI413 - Class 16 Fall 2011 7 / 15

Pass by Value/Result This is the default in Fortran, and for in out parameters in Ada: --in file f.adb procedure f(x : i n out Integer ) i s begin x := 10; end f; --in file test.adb procedure test i s y : Integer := 5; begin f(y); Ada. Integer_Text_IO. Put (y); end test ; Calling test prints 10, not 5! Roche (USNA) SI413 - Class 16 Fall 2011 8 / 15

Pass by Sharing This is the default in languages like Java, for non-primitive types: c l a s s Share { s t a t i c c l a s s Small { p u b l i c i n t x; p u b l i c Small ( i n t thex ) { x = thex ; p u b l i c s t a t i c v o i d test ( Small s) { s. x = 10; s = new Small (20); p u b l i c s t a t i c v o i d main ( String [] args ) { Small mainsmall = new Small (5); test ( mainsmall ); System. out. println ( mainsmall.x); Does this program print 5, 10, or 20? Roche (USNA) SI413 - Class 16 Fall 2011 9 / 15

Pass by Sharing This is the default in languages like Java, for non-primitive types: c l a s s Share { s t a t i c c l a s s Small { p u b l i c i n t x; p u b l i c Small ( i n t thex ) { x = thex ; p u b l i c s t a t i c v o i d test ( Small s) { s. x = 10; s = new Small (20); p u b l i c s t a t i c v o i d main ( String [] args ) { Small mainsmall = new Small (5); test ( mainsmall ); System. out. println ( mainsmall.x); Does this program print 5, 10, or 20? 10! Roche (USNA) SI413 - Class 16 Fall 2011 9 / 15

Parameter Passing in Functional Languages Why haven t we talked about parameter passing in Haskell, Scheme, etc.? Roche (USNA) SI413 - Class 16 Fall 2011 10 / 15

Parameter Passing in Functional Languages Why haven t we talked about parameter passing in Haskell, Scheme, etc.? In pure functional programming, it makes no difference! Scheme actually uses pass by sharing. Roche (USNA) SI413 - Class 16 Fall 2011 10 / 15

Method calls in objects What does a call like obj.foo(x) do in an OOP language such as C++ or Java? foo must be a method defined in the class of obj. The method also has access to what object it was called on (called this in C++ and Java). This is syntactic sugar for having a globally-defined method foo, with an extra argument for this. So we can think of obj.foo(x) as foo(obj,x). Roche (USNA) SI413 - Class 16 Fall 2011 11 / 15

Overloading Function overloading is when the same name refers to many functions. Which function to call is determined by the types of the arguments. Roche (USNA) SI413 - Class 16 Fall 2011 12 / 15

Overloading Function overloading is when the same name refers to many functions. Which function to call is determined by the types of the arguments. c l a s s A { v o i d print () { cout << i n A << endl ; ; c l a s s B { v o i d print () { cout << i n B << endl ; ; v o i d foo ( i n t a) { cout << a << i s an i n t \n ; v o i d foo ( double a) { cout << a << i s a d o u b l e \n ; i n t main () { cout << (5 << 3) << endl ; A x; B y; x. print (); y. print (); foo (5); foo (5.0); How does overloading occur in this C++ example? Roche (USNA) SI413 - Class 16 Fall 2011 12 / 15

Polymorphism Sybtype polymorphism is like overloading, but the called function depends on the object s actual type, not its declared type! Each object stores a virtual methods table (vtable) containing the address of every virtual function. This is inspected at run-time when a call is made. See section 9.4 in your book if you want the details. Roche (USNA) SI413 - Class 16 Fall 2011 13 / 15

Polymorphism example in C++ c l a s s Base { v i r t u a l v o i d aha () = 0; ; c l a s s A : p u b l i c Base { v o i d aha () { cout << I m an A! << endl ; ; c l a s s B : p u b l i c Base { v o i d aha () { cout << I m a B! << endl ; i n t main ( i n t argc ) { Base * x; i f ( argc == 1 ) // can t know this at compile-time! x = new A; e l s e x = new B; x. aha (); // Which one will it call? Roche (USNA) SI413 - Class 16 Fall 2011 14 / 15

Class outcomes You should know: The differences between the four parameter passing modes we have discussed How class method calls are typically interpreted by compilers How overloading works with operators, classes, and functions What subtype polymorphism is and its benefits and drawbacks You should be able to: Follow and create programming examples demonstrating pass by value vs. pass by reference Roche (USNA) SI413 - Class 16 Fall 2011 15 / 15