Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

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
//  Chapter10Demo - Arrays
//  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;
};

Person.cpp

//  Person.cpp
//  Chapter10Demo//
//  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;
}

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

  • No labels