260 likes | 385 Views
Passing Objects to Methods. Passing an object is actually passing a copy of the object.
E N D
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 Pokemon myPokes[5]; myPokes ? ? ? ? ? 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. //.
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; }