OOP - Week 3, Lecture 3: Arrays of Objects
Storing Many Students
A program that handles one student can use one Student object. But a course has many students, and you do not want to write s1, s2, s3, β¦ s50, and repeat every line 50 times.
In Week 1 you solved this problem for numbers: an array stores many values under one name, and a loop visits them. The same idea works for objects. An array can hold objects of any class:
Student students[50]; // 50 Student objects
In this lecture we use a small Student class with an id and a gpa:
class Student {
private:
int id;
double gpa;
public:
Student(int i = 0, double g = 0.0); // constructor with default parameters
~Student();
void setId(int i);
void setGpa(double g);
int getId();
double getGpa();
void print();
};
1. Declaring an Array of Objects
Student students[3]; // three Student objects: students[0], students[1], students[2]
When this line runs, C++ creates three objects, and it runs a constructor for each element, in order of the index. No values are given for them, so every element is created with the default constructor.
This means that the class must have a default constructor:
| The class has | Student students[3]; |
|---|---|
| no constructor at all | works: the compilerβs default constructor runs (members are garbage) |
| a default constructor, or a constructor with default parameters | works: that constructor runs for every element |
only Student(int i, double g) | error: no matching constructor |
2. Giving the Elements Values
There are two ways to give the elements their values.
Call constructors in the declaration. Write a constructor call for each element between braces:
Student students[3] = { Student(101, 3.2), Student(102, 3.9), Student(103, 2.8) };
The first constructor call creates students[0], the second creates students[1], and so on. If you write fewer calls than the size of the array, the remaining elements use the default constructor:
Student students[4] = { Student(101, 3.2), Student(102, 3.9) };
// students[0] is 101, students[1] is 102, students[2] and students[3] use the default values
Use the setters after the declaration. Create the array first, then change each element:
Student students[4];
students[0].setId(101);
students[0].setGpa(3.2);
3. Using the Elements
Each element is a normal object. Write the index first, then a dot, then the member function:
students[2].setId(103); // students[2] is the object, setId is the function
students[2].print();
cout << students[2].getGpa() << endl;
for (int i = 0; i < 4; i++)
students[i].print();
4. Arrays of Objects and Functions
A function can receive an array of objects, together with its size:
double averageGpa(Student arr[], int size) {
double sum = 0;
for (int i = 0; i < size; i++)
sum += arr[i].getGpa();
return sum / size;
}
double avg = averageGpa(students, 4);
An array is passed by its address, so no
Studentis copied. The function works on the original students.
5. Order of Construction and Destruction
As in Week 2, Lecture 3, objects are destroyed in the reverse order of their creation. For an array this means:
- Constructors run in the order of the index:
students[0], thenstudents[1], and so on. - Destructors run in the reverse order: the last element is destroyed first and
students[0]is destroyed last.
6. Putting It Together
This program creates an array of four students (two with values, two with the default values), changes the last two with setters, prints them all, and calls averageGpa from above and bestStudent, which returns the index of the student with the highest gpa. The constructor and the destructor print a message, so you can see the order.
#include <iostream>
using namespace std;
class Student {
private:
int id;
double gpa;
public:
Student(int i = 0, double g = 0.0) { // default parameters: also the default constructor
id = 0; // safe values first
gpa = 0.0;
setId(i);
setGpa(g);
cout << "constructor " << id << endl;
}
~Student() { // destructor
cout << "destructor " << id << endl;
}
void setId(int i) {
if (i > 0) // validation
id = i;
}
void setGpa(double g) {
if (g >= 0.0 && g <= 4.0) // validation
gpa = g;
}
int getId() {
return id;
}
double getGpa() {
return gpa;
}
void print() {
cout << "id " << id << ", gpa " << gpa << endl;
}
};
double averageGpa(Student arr[], int size) { // the array and its size
double sum = 0;
for (int i = 0; i < size; i++)
sum += arr[i].getGpa();
return sum / size;
}
int bestStudent(Student arr[], int size) { // the index of the highest gpa
int best = 0;
for (int i = 1; i < size; i++) {
if (arr[i].getGpa() > arr[best].getGpa())
best = i;
}
return best;
}
int main() {
Student students[4] = { Student(101, 3.2), Student(102, 3.9) }; // the other two use the default values
students[2].setId(103);
students[2].setGpa(2.8);
students[3].setId(104);
students[3].setGpa(3.5);
for (int i = 0; i < 4; i++)
students[i].print();
cout << "average gpa " << averageGpa(students, 4) << endl;
int best = bestStudent(students, 4);
cout << "best student " << students[best].getId() << endl;
return 0;
} // destructors run hereClick each line of output to see what caused it.