Arrays of Objects

If a class has constructors and you declare an array of that class’s objects, the class should have the default constructor. The default constructor is typically used to initialize each (array) class object.

For example, if you declare an array of 100 class objects, then it is impractical (if not impossible) to specify different constructors for each component. (We will further clarify this at the end of this section.)

Suppose that you have 100 employees who are paid on an hourly basis, and you need to keep track of their arrival and departure times. You can declare two arrays— arrivalTimeEmp and departureTimeEmp—of 100 components each, wherein each component is an object of type clockType.

Person.hpp

// Person.hpp // Created by Kevin Roark on 11/13/22. #ifndef Person_hpp #define Person_hpp #include <stdio.h> #endif /* Person_hpp */ #include <string> using namespace std; class Person { public: //getters and setters declarations void print() const; void setName(string, string, string); void setLastName(string last); void setFirstName(string first); void setMiddleName(string middle); void set(string, string, string); string getFirstName() const; string getMiddleName() const; string getLastName() const; //constructor Person(string first = "", string middle = "", string last = ""); private: //class attributes / properties string firstName; string middleName; string lastName; };

This C++ code is a header file named "Person.hpp" that defines the class declaration for the Person class.

Here's a breakdown of the code:

  • The code begins with some comment lines that provide information about the file, such as the author and creation date.

  • The code includes the necessary header files:

    • #include <stdio.h>: This header file is included but not used in the code. It can be removed.

    • #include <string>: This header file is included to use the string data type and related functions.

  • The code uses the std namespace to avoid writing std:: before each standard library function. This allows the code to directly use functions like string without the std:: prefix.

  • The class declaration begins with the class Person line, which introduces the Person class.

  • The public: section defines the public interface of the class. These are the member functions that can be accessed from outside the class.

  • The code declares the member functions (methods) of the Person class. These functions include:

    • void print() const;: This member function prints the first, middle, and last name of a person.

    • void setName(string, string, string);: This member function sets the first, middle, and last name of a person. The parameter names are omitted in the declaration.

    • void setLastName(string last);: This member function sets the last name of a person.

    • void setFirstName(string first);: This member function sets the first name of a person.

    • void setMiddleName(string middle);: This member function sets the middle name of a person.

    • void set(string, string, string);: This member function sets the first, middle, and last name of a person. The parameter names are omitted in the declaration.

    • string getFirstName() const;: This member function returns the first name of a person.

    • string getMiddleName() const;: This member function returns the middle name of a person.

    • string getLastName() const;: This member function returns the last name of a person.

  • The private: section defines the private members of the class. These are the member variables that can only be accessed within the class itself.

  • The code declares the member variables (attributes or properties) of the Person class. These variables include:

    • string firstName;: This member variable holds the first name of a person.

    • string middleName;: This member variable holds the middle name of a person.

    • string lastName;: This member variable holds the last name of a person.

  • The code also declares a constructor for the Person class. The constructor takes three string parameters first, middle, and last, and provides default values of an empty string for all three parameters. The constructor is responsible for initializing the member variables firstName, middleName, and lastName with the provided parameter values.

Overall, this header file defines the structure and interface of the Person class, including its member functions and member variables. It provides a blueprint for creating Person objects, accessing and modifying their names, and performing operations on them.

Person.cpp

// Person.cpp // Created by Kevin Roark on 11/13/22. // This is a C++ source file that defines a class called "Person". // The class has member functions to manipulate a person's first, middle, and last name. // Include necessary header files #include <iostream> // Input/output operations #include <string> // String operations #include "Person.hpp" // User-defined header file for the Person class // Use the "std" namespace to avoid writing "std::" before each standard library function using namespace std; // Define the member function "print" that prints the first, middle, and last name of a person void Person::print() const { cout << firstName << " " << middleName << " " << lastName; } // Define the member function "setName" that sets the first, middle, and last name of a person void Person::setName(string first, string middle, string last) { firstName = first; middleName = middle; lastName = last; } // Define the member function "setLastName" that sets the last name of a person void Person::setLastName(string last) { lastName = last; } // Define the member function "setFirstName" that sets the first name of a person void Person::setFirstName(string first) { firstName = first; } // Define the member function "setMiddleName" that sets the middle name of a person void Person::setMiddleName(string middle) { middleName = middle; } // Define the member function "getFirstName" that returns the first name of a person string Person::getFirstName() const { return firstName; } // Define the member function "getMiddleName" that returns the middle name of a person string Person::getMiddleName() const { return middleName; } // Define the member function "getLastName" that returns the last name of a person string Person::getLastName() const { return lastName; } // Define the constructor function that initializes the first, middle, and last name of a person using provided parameters Person::Person(string first, string middle, string last) { firstName = first; middleName = middle; lastName = last; } void Person::set(string first, string middle, string last) { firstName = first; middleName = middle; lastName = last; }

This C++ code is a source file named "Person.cpp" that defines the member functions of the Person class.

Here's a breakdown of the code:

  • The code includes necessary header files:

    • #include <iostream>: This header file provides input/output operations, such as cout for outputting data to the console.

    • #include <string>: This header file provides string operations and functions.

    • #include "Person.hpp": This includes the user-defined header file "Person.hpp", which contains the class declaration for the Person class.

  • The code uses the std namespace to avoid writing std:: before each standard library function. This allows the code to directly use functions like cout and string without the std:: prefix.

  • The code defines the member function print() that prints the first, middle, and last name of a person. It uses cout to output the values of the firstName, middleName, and lastName member variables.

  • The code defines the member function setName() that sets the first, middle, and last name of a person. The function takes three string parameters first, middle, and last and assigns their values to the corresponding member variables of the Person class (firstName, middleName, and lastName).

  • The code defines the member function setLastName() that sets the last name of a person. It takes a string parameter last and assigns its value to the lastName member variable.

  • The code defines the member function setFirstName() that sets the first name of a person. It takes a string parameter first and assigns its value to the firstName member variable.

  • The code defines the member function setMiddleName() that sets the middle name of a person. It takes a string parameter middle and assigns its value to the middleName member variable.

  • The code defines the member function getFirstName() that returns the first name of a person. It is a const member function and does not modify the object. It simply returns the value of the firstName member variable.

  • The code defines the member function getMiddleName() that returns the middle name of a person. Similar to getFirstName(), it is a const member function that returns the value of the middleName member variable.

  • The code defines the member function getLastName() that returns the last name of a person. It is also a const member function that returns the value of the lastName member variable.

  • The code defines the constructor function for the Person class. The constructor takes three string parameters first, middle, and last and initializes the corresponding member variables firstName, middleName, and lastName with the provided parameter values.

  • The code defines the member function set() that sets the first, middle, and last name of a person. It takes three string parameters first, middle, and last and assigns their values to the corresponding member variables of the Person class (firstName, middleName, and lastName).

Overall, this source file provides the definitions for the member functions of the Person class. It implements the functionality specified in the class declaration and allows objects of the Person class to be created, manipulated, and accessed.

main.cpp

// main.cpp // PersonArray // Demonstrates use of an Array of Objects // Created by Kevin Roark on 3/26/23. #include <iostream> #include "Person.hpp" using namespace std; int main() { // create an array of Objects Person Person arrayPerson[3]; cout << "Array of Objects example\n"; arrayPerson[0].set("Fred", "J.", "Flintstone"); arrayPerson[1].set("Bart", "Paul", "Simpson"); arrayPerson[2].set("Sam", "Read", "Roark"); //now read through the array for(int i = 0; i < 3; i++) { arrayPerson[i].print(); cout << endl; } return 0; }

This C++ code is a main program file named "main.cpp" that demonstrates the use of an array of objects of the Person class.

Here's a breakdown of the code:

  • The code includes the necessary header files:

    • #include <iostream>: This header file provides input/output operations, such as cout for outputting data to the console.

    • #include "Person.hpp": This includes the user-defined header file "Person.hpp", which contains the class declaration for the Person class.

  • The code uses the std namespace to avoid writing std:: before each standard library function. This allows the code to directly use functions like cout without the std:: prefix.

  • The main() function is the entry point of the program. Execution of the program starts from here.

  • In the main() function, an array of objects of the Person class named arrayPerson is created. The array has a size of 3, which means it can store 3 Person objects.

  • The program demonstrates an example of creating Person objects and setting their attributes using the set() member function. Each element of the arrayPerson array is accessed using the index and the set() function is called on that particular object to set its attributes. For example, arrayPerson[0].set("Fred", "J.", "Flintstone") sets the attributes of the first Person object in the array.

  • The program then loops through the arrayPerson array using a for loop. In each iteration, it calls the print() member function on the current Person object and outputs the first, middle, and last name to the console using cout. The loop allows all the Person objects in the array to be printed.

  • Finally, the program ends by returning 0 from the main() function, indicating successful execution.

Overall, this main program demonstrates the usage of an array of objects of the Person class. It creates an array, sets the attributes of each object in the array, and prints the attributes of each object using the print() member function.

2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark