1 / 24

Passing Objects to Methods

Passing Objects to Methods. Passing an object is actually passing a copy of the object.

akiva
Download Presentation

Passing Objects to Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related