Arrays of Objects


We can create arrays of objects in the same fashion as other arrays.

To access an object that is an element of an array of objects, use the usual following syntax:

    array-name[subscript]

To access a data member of an object that is an element of an array of objects, use the following syntax:

   array-name[subscript].member-name

To call a member function for an object that is an element of an array of objects, use the following syntax:

   array-name[subscript].member-function-name(argument(s))

Here is a full program example illustrating the creation and use of an array of Student objects:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstring>

using std::cerr;
using std::cout;
using std::endl;
using std::fixed;
using std::ifstream;
using std::setprecision;

// Student class definition
class Student
{
private:

    char name[31]{"None"};
    double gpa{0.0};

public:

    // Constructors
    Student() = default;
    Student(const char*, double);

    // Accessor methods
    void set_name(const char*);
    const char* get_name() const;
    void set_gpa(double);
    double get_gpa() const;

    // Other methods
    void print() const;
};

// Function prototypes
int build(Student[]);
void print(const Student[], int);
void sort(Student[], int);

int main()
{
    Student student_array[10];
    int count;

    count = build(student_array);

    cout << "Unsorted List of Students\n\n";

    print(student_array, count);

    sort(student_array, count);

    cout << "\nSorted List of Students\n\n";

    print(student_array, count);

    return 0;
}

int build(Student student_array[])
{
    ifstream in_file;
    char first_name[11], last_name[21], name[31];
    double gpa;
    int count = 0;

    // Open the input file and test for failure
    in_file.open("students1.txt");
    if (!in_file)
    {
        cerr << "Error - unable to open input file\n";
        exit(1);
    }

    in_file >> first_name;
    while (in_file)
    {
        in_file >> last_name;
        in_file >> gpa;

        strcpy(name, last_name);
        strcat(name, ", ");
        strcat(name, first_name);

        // Create a Student object and copy it into
        // the array.
        student_array[count] = Student(name, gpa);
        
        /* Could do these lines instead to alter the
        existing Student object in the array.
        
        student_array[count].set_name(name);
        student_array[count].set_gpa(gpa); */

        count++;

        in_file >> first_name;
    }

    in_file.close();

    return count;
}

void print(const Student student_array[], int n)
{
    int i;
    cout << fixed << setprecision(2);

    for (i = 0; i < n; ++i)
    {
        student_array[i].print();
        cout << endl;
    }
}

void sort(Student student_array[], int n)
{
    int i, j, min;
    Student temp;

    for (i = 0; i < n - 1; ++i)
    {
        min = i;

        for (j = i + 1; j < n; ++j)
        {
            if (strcmp(student_array[j].get_name(), student_array[min].get_name()) < 0)
                min = j;
        }

        temp = student_array[i];
        student_array[i] = student_array[min];
        student_array[min] = temp;
    }
}

// Student constructor

Student::Student(const char* new_name, double new_gpa)
{
    set_name(new_name);
    set_gpa(new_gpa);
}

// Student accessor and mutator member functions

void Student::set_name(const char* new_name)
{
    strcpy(name, new_name);
}

const char* Student::get_name() const
{
    return name;
}

void Student::set_gpa(double new_gpa)
{
    if (new_gpa < 0.0)
        gpa = 0.0;
    else if (new_gpa > 4.0)
        gpa = 4.0;
    else
        gpa = new_gpa;
}

double Student::get_gpa() const
{
    return gpa;
}

// Other member functions

void Student::print() const
{
    cout << "Name: " << name << endl
         << "GPA: " << fixed << setprecision(2) << gpa << endl;
}

Download the full program example, including a makefile and sample data file