Passing Objects to Methods

Slides:



Advertisements
Similar presentations
Chapter 5 Functions.
Advertisements

1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
A First Book of ANSI C Fourth Edition
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
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.
User-Defined Functions (cont’d) - Reference Parameters.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Review for Test2. Scope 8 problems, 60 points. 1 Bonus problem (5 points) Coverage: – Test 1 coverage – Exception Handling, Switch Statement – Array of.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Chapter VII: Arrays.
CSC 211 Java I for loops and arrays.
Array ISYS 350.
Two Dimensional Array Mr. Jacobs.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Chapter 5 Functions.
C++ Arrays.
A solution to a list of records
The dirty secrets of objects
CS 1430: Programming in C++.
Array ISYS 350.
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Arrays Part II Array Function Arguments
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CSC 113 Tutorial QUIZ I.
CNG 140 C Programming (Lecture set 8)
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Chapter 4 (part 2).
Review for Final Exam.
Counting Loops.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CS150 Introduction to Computer Science 1
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays .
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
CISC181 Introduction to Computer Science Dr
Review for Final Exam.
The Function Prototype
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Fundamental Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
CS150 Introduction to Computer Science 1
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Corresponds with Chapter 5
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

Passing Objects to Methods Passing an object is actually passing a copy of the object. C++ uses exactly one mode of passing arguments: pass-by-value. But there are two different situations: (1) passing a primitive type value or an object (a copy of the value is passed to the parameter) (2) passing a reference type value (e.g., an array) is passed to the parameter.

Scope of Variables The scope of instance variables is the entire class. They can be declared anywhere inside a class. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be initialized explicitly before it can be used. Recall that we use the braces to form a block in a C++ program.

Array of Objects Pokemon myPokes[5]; An array of objects is actually an array of reference variables. So invoking myPokes[1].getPower() involves two levels of referencing. “myPokes” references to the entire array. myPokes[1] references to a Pokemon object. What will happen for this statement? myPokes[0].setPower(150);

Array of Objects myPokes Pokemon myPokes[5]; ? When an array of objects is created using the each element is a variable with the default values from the default constructor. myPokes[0].setPower(150); cout << myPokes[0].getPower();

string name = “None”; int power = 0; //declare and create an array of 10 Pokemon objects. //. //Write a for-loop to prompt user to enter name and //power for each Pokemon object, and then set name //and power to the corresponding Pokemon object.

//declare and create an array of 10 Pokemon objects. . string name = “None”; int power = 0; //declare and create an array of 10 Pokemon objects. . Pokemon myPokes[10]; //Write a for-loop to prompt user to enter name and //power for each Pokemon object, and then set name //and power to the corresponding Pokemon object. //Do not forget to create the required Pokemon //objects. for (int i=0; i< 10; i++) { cout << "Enter name: "; cin >> name; cout << "Enter power: "; cin >> power; myPokes[i].setName(name); myPokes[i].setPower(power); }

findHighPower method in the main class Pre-conditions: None Post-conditions: Finds and returns the type int value giving the highest power value of all Pokemons in a given array of Pokemon objects.

findHighPower method in the main class int findHighPower(Pokemon list[]) { int max = list[0].getPower(); for (int i = 1; I < SIZE; i++) if (list[i].getPower()>max) max = list[i].getPower(); return max; }//findHighPower

findAvgPower method in the main class Pre-conditions: None Post-conditions: Finds and returns the type int value giving the average power value of all Pokemon in a given array of Pokemon objects.

findAvgPower method in the main class int findAvgPower(Pokemon list[]) { int sum = 0; for (int i = 0; I < SIZE; i++) sum = sum + list[i].getPower(); return sum/SIZE; }//findAvgPower

Example Array of Students // Declare array of Students named “School”. // Create an array of 3 Student variables. //Create the first Student object in the array with //name “Sue”, credits 16 and gpa 4.0. //Create the second Student object in the array with //name “Joe”, credits 32 and gpa 3.0. //Create the third Student object in the array with //name “Bob”, credits 60 and gpa 3.5.

Example Array of Students // Declare and initialize an array of Students named // school. Student school[3] = { (“Sue”, 16, 4.0), (“Joe”, 32, 3.0), (“Bob”, 60, 3.5)};

Using Class Objects in An Array First use the array name, brackets, and index to select which array element. Second append the dot and call the desired method of that element’s object. school[0].addCredits(16);

Printing Array of Students for(int i = 0; i < 3; i++) { school[i].toString(); }

Exercise Write a loop to find the average GPA of all the students in the school array.

Average GPA Solution double gpaSum = 0.0; for(int i = 0; i < 3; i++) gpaSum += school[i].getGpa(); double avgGpa = gpaSum / 3;

Using a Loop to Fill an Array int num = 6; Student school[num]; String name; int hrs; double gpa; //Use a for-loop to prompt user to enter name, credit // hours and gpa for each student; and then invoke the // setter methods to assign the values to the current // Student object.

Using a Loop to Fill an Array int num = 6; Student school[num]; string name; int hrs; double gpa; for(int i = 0; i < num; i++) { cout << “Enter name: “; cin >> name; school[i].setMyName(name); cout << “Enter hours:“; cin >> hrs school[i].addCredits(hours); cout << “Enter gpa: “; cin >> gpa; school[i].setMyGPA(gpa); }

Average GPA Function Write a helper function called findAvgGpa Pre-conditions: Receives an array of Student objects and the size of the array. Post-conditions: Computes and returns a type double value giving the average GPA of all students.

Average GPA Function double findAvgGpa(Student s[]) { double sum = 0.0; for(int i = 0; i < SIZE; i++) sum = sum + s[i].getMyGPA(); return sum / SIZE; }

Find Highest GPA Function Write a helper function called findMaxGpa Pre-conditions: Receives an array of Student objects and the size of the array. Post-conditions: Finds and returns a type Student object giving the information on the student with the highest GPA.

Find Highest GPA Function Student findMaxGpa(Student s[]) { Student maxStu = s[0]; for(int i = 1; i < SIZE; i++) if(s[i].getMyGPA() > maxStu.getMyGPA()) maxStu = s[i]; return maxStu; }

Find Highest GPA Function Write a helper function called findMaxGpaStudent Pre-conditions: Receives an array of Student objects and the size of the array. Post-conditions: Finds and returns a Student reference value pointing to the Student having the highest GPA.

Find Highest GPA Student Method Student findMaxGpa(Student s[]) { double maxGpa = s[0].getMyGPA(); Student maxStudent = null; for(int i = 0; i < SIZE; i++) if(s[i].getMyGPA() > maxGpa) maxGpa = s[i].getGpa(); maxStudent = s[i]; } return maxStudent;