Passing Objects to Methods

24
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. 1

description

Passing Objects to Methods. Passing an object is actually passing a copy of the object. - PowerPoint PPT Presentation

Transcript of Passing Objects to Methods

Page 1: Passing Objects to Methods

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.

1

Page 2: Passing Objects to Methods

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.

2

Page 3: Passing Objects to Methods

Array of ObjectsPokemon 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.

3

What will happen for this statement?

myPokes[0].setPower(150);

Page 4: Passing Objects to Methods

Array of ObjectsPokemon myPokes[5];

myPokes

4

myPokes[0]

myPokes[1]

myPokes[2]

myPokes[3]

myPokes[4]

?

?

?

?

?

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();

Page 5: Passing Objects to Methods

5

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. //.

Page 6: Passing Objects to Methods

6

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);}

Page 7: Passing Objects to Methods

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.

Page 8: Passing Objects to Methods

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

Page 9: Passing Objects to Methods

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.

Page 10: Passing Objects to Methods

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

Page 11: Passing Objects to Methods

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.

Page 12: Passing Objects to Methods

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)};

Page 13: Passing Objects to Methods

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);

Page 14: Passing Objects to Methods

Printing Array of Students

for(int i = 0; i < 3; i++)

{

school[i].toString();

}

Page 15: Passing Objects to Methods

Exercise

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

Page 16: Passing Objects to Methods

Average GPA Solution

double gpaSum = 0.0;

for(int i = 0; i < 3; i++)

gpaSum += school[i].getGpa();

double avgGpa = gpaSum / 3;

Page 17: Passing Objects to Methods

Using a Loop to Fill an Arrayint 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.

Page 18: Passing Objects to Methods

Using a Loop to Fill an Arrayint 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); }

Page 19: Passing Objects to Methods

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.

Page 20: Passing Objects to Methods

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;

}

Page 21: Passing Objects to Methods

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.

Page 22: Passing Objects to Methods

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;

}

Page 23: Passing Objects to Methods

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.

Page 24: Passing Objects to Methods

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;}