Parameters, Overloading Methods, and Random Garbage

Slides:



Advertisements
Similar presentations
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Advertisements

1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Chapter 4: Writing Classes
Inheritance Inheritance Reserved word protected Reserved word super
Road Map Introduction to object oriented programming. Classes
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Classes - Intermediate
Methods.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
C++ Functions A bit of review (things we’ve covered so far)
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
Operator Overloading Introduction
3 Introduction to Classes and Objects.
Static data members Constructors and Destructors
Chapter 4: Writing Classes
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Methods.
Lecture 11 B Methods and Data Passing
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Object Based Programming
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Corresponds with Chapter 7
Chapter 6 Methods: A Deeper Look
Encapsulation and Constructors
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
Building Java Programs
Chapter 6 Methods.
Methods and Data Passing
Building Java Programs
Lecture 11 Parameters CSE /26/2018.
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Classes and Objects Object Creation
Corresponds with Chapter 5
Methods and Data Passing
CMSC 202 Constructors Version 9/10.
Introduction to Methods and Interfaces
Object-Oriented Programming and class Design
Chapter 6: Methods CS1: Java Programming Colorado State University
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Parameters, Overloading Methods, and Random Garbage Lecture 11 Parameters, Overloading Methods, and Random Garbage CSE 1322 4/26/2018

What is a Method? Think of a method as a black box that contains the detailed implementation for a specific task. The method may use inputs (parameters) and may return an output with a specific type. 7/16/2019

Method Structure Return Type Method Name Formal Parameters Modifiers Method Header public static int addTwoNums (int x, int y) { return x + y; } Method Body 7/16/2019

Actual parameters (called ”arguments”) Calling the Method Return Type Method Name Formal Parameters Modifiers Method Header public static int addTwoNums (int x, int y) { return x + y; } Method Body Actual parameters (called ”arguments”) int result = addTwoNums (5, 2); 7/16/2019

Parameters There are three main types of parameters: Value - passes a copy of the value of the variable to the method.  This is the default. Reference (C#)- passes a reference to the actual variable.  Keyword is ref. Use this when you want to pass a value in and have any change to that value be persistent when the method is complete Out (C#) - passes a reference to the actual variable.  Keyword is out. Use this when you want the method to initialize a value and place it for later use in the actual variable (persists when the method is complete) Note that Java passes all parameters by value. C++ uses both Call-by-reference and Call-by-value 4/26/2018

Example 1 - and the output is ? static void B (int x) { // Built-in type argument passing x += 9; PRINT (x); } public static void M/main (S/string[] args) { int a = 42; PRINT (a); // Prints 42 B (a); // Prints 51 because is call-by-value; PRINT (a); // Prints 42 } 4/26/2018

Example 2 - and the output is ? static void B (ref int x) { x += 9; Console.WriteLine (x); } public static void Main (string[] args) { int a = 42; Console.WriteLine (a); // Prints 42 B (ref a); // Prints 51 BUT! a has changed! Console.WriteLine (a); // Prints 51 } 4/26/2018

What about out? Remember, this is used to initialize a variable Why even have it? We have ref, right? Honestly, out isn’t used often, but: Marks the “intent” of what you’re doing Could prevent bad data from coming in 4/26/2018

Example 3 - and the output is ? static void B (out int x) { x = 9; Console.WriteLine (x); } public static void Main (string[] args) { int a; // We can't print this yet - not initialized B (out a); // Initializes a and prints 9 Console.WriteLine (a); // Prints 9 } 4/26/2018

What about Complex Data Types? With classes/objects, we still pass by value HOWEVER, we are copying the reference We’ll start with code, then look at what’s going on 4/26/2018

class Dog { public int weight; } static void B(Dog x) { // IMPORTANT class Dog { public int weight; } static void B(Dog x) { // IMPORTANT! x is referencing the same // chunk of memory myDog is referencing! x.weight += 9; PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); // 10 B(myDog); // 19 PRINT(myDog.weight); // 19 4/26/2018

class Dog { public int weight; } static void B(Dog x) { x class Dog { public int weight; } static void B(Dog x) { x.weight += 9; PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); myDog weight = 0; 4/26/2018

class Dog { public int weight; } static void B(Dog x) { x class Dog { public int weight; } static void B(Dog x) { x.weight += 9; PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); myDog weight = 10; 4/26/2018

class Dog { public int weight; } static void B(Dog x) { x class Dog { public int weight; } static void B(Dog x) { x.weight += 9; PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); // PRINTS 10 to the screen myDog weight = 10; 4/26/2018

class Dog { public int weight; } static void B(Dog x) { x class Dog { public int weight; } static void B(Dog x) { x.weight += 9; PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); // MAGIC HAPPENS! x is a copy of myDog... // That is, x points to the same thing // myDog points to x myDog weight = 10; 4/26/2018

Watch what happens to the weight variable IMPORTANT Watch what happens to the weight variable 4/26/2018

class Dog { public int weight; } static void B(Dog x) { x class Dog { public int weight; } static void B(Dog x) { x.weight += 9; // Look at weight before… PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); x myDog weight = 10; 4/26/2018

class Dog { public int weight; } static void B(Dog x) { x class Dog { public int weight; } static void B(Dog x) { x.weight += 9; // Look at weight after! PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); x myDog weight = 19; 4/26/2018

Now, myDog’s weight has now changed! IMPORTANT Now, myDog’s weight has now changed! 4/26/2018

What about adding ref? class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); 4/26/2018

What about adding ref? myDog weight = 0; class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); myDog weight = 0; 4/26/2018

What about adding ref? myDog weight = 10; class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); myDog weight = 10; 4/26/2018

What about adding ref? myDog weight = 10; class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); // OUTPUT 10 myDog weight = 10; 4/26/2018

What about adding ref? myDog == x weight = 10; class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); myDog == x weight = 10; 4/26/2018

What about adding ref? weight = 0; myDog == x weight = 10; class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); // Uh oh... weight = 0; myDog == x weight = 10; 4/26/2018

Our first piece of garbage No one is pointing to the old dog anymore! We’ve created trash Yes, it’s really called that (or garbage) Yes, we have a garbage collector (GC) The GC picks up unloved objects There’s no way to recover the unloved dog Rule: when no one points to an object, it’s TRASHED! weight = 0; myDog == x weight = 10; 4/26/2018

What about adding ref? weight = 9; myDog == x weight = 10; class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); weight = 9; myDog == x weight = 10; 4/26/2018

What about adding ref? weight = 9; myDog == x weight = 10; class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); // OUTPUT 9 to the screen weight = 9; myDog == x weight = 10; 4/26/2018

What about adding ref? weight = 9; myDog == x weight = 10; class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); // OUTPUT 9 to the screen AGAIN because myDog has changed weight = 9; myDog == x weight = 10; 4/26/2018

Pass-by-value vs. Pass-by-reference vs. Pass-by-reference-type Be careful to note the difference between a pass-by-reference parameter and a parameter of a reference type. Use the activation stack to track local variables and how parameters of the above types affect the variables from one stack frame to the next. 4/26/2018

Overloading Methods Method overloading: using the same method name for multiple methods Do not confuse with overriding (from inheritance) The signature of each overloaded method must be unique The signature includes the number, type, and order of the parameters The compiler determines which version of the method is being invoked by analyzing the parameters The return type of the method is not part of the signature 4/26/2018

Overloading Methods float tryMe (int x) { return x + .375; } float tryMe (int x, float y) return x*y; // Which tryMe is called? result = tryMe (25, 4.32f); 4/26/2018

Overloading Operators In C# and C++, not only can methods be overloaded, even operators (e.g. +, -, *, /, ==, ++, etc) can be overloaded. Java does not allow operator overloading 4/26/2018

Operator overloading C# enables you to overload most operators to make them sensitive to the context in which they are used. Use operator overloading when it makes an application clearer than accomplishing the same operations with explicit method calls. Example: Class ComplexNumber overloads the plus (+), minus (-) and multiplication (*) operators to enable programs to add, subtract and multiply instances of class ComplexNumber using common mathematical notation 4/26/2018

Rules for Operator Overloading Keyword operator, followed by an operator symbol, indicates that a method overloads the specified operator. Methods that overload binary operators must take two arguments—the first argument is the left operand, and the second argument is the right operand. Overloaded operator methods must be public and static. 4/26/2018

public class Dog { public string name; public int weight; public Dog(string name, int weight) { this.name = name; this.weight = weight; } public static Dog operator+ (Dog d1, Dog d2) {// Overload the + operator Dog mergedDog = new Dog("ComboDog", 0); mergedDog.name = d1.name+":"+d2.name; mergedDog.weight = d1.weight+d2.weight; return mergedDog; } } class MainClass { public static void Main(String[] args) { Dog myDog = new Dog("CSE", 8); Dog yourDog = new Dog ("1322", 9); Dog uberDog = myDog + yourDog; // Add two dogs together! Console.WriteLine (uberDog.name + " weighs " + uberDog.weight); } // OUTPUT is CSE:1322 weighs 17 4/26/2018