Challenge 68

Description

The purpose of this challenge is to create dynamic objects using pointers.

This challenge simulates the control panel of a home alarm system. In these installations, the panel is used to arm the system. In a typical installation, sensors are placed on windows and doors of the home. These sensors detect if the doors or windows are open. The alarm can only be armed when all the windows and doors are closed…..unless they are “bypassed.”

 Requirements

  1. Define a class called Sensor as below
    class Sensor
    {
      private:
        string name;
        bool is_open;
        bool bypassed;
    
      public:
        Sensor() {}
    
        Sensor(string n, bool byp = false, bool io = false)
        {
          name = n;
          is_open = io;
          bypassed = byp;
        }
    };
  2. Below the Sensor class, write a global (non-class) function called Sensor * create(string name, bool is_bypassed, bool is_open). This function will:
    1. Dynamically create a new Sensor object. Remember that the Sensor class has an overloaded constructor you can use.
    2. Return the address of the newly created Sensor object
  3. In the Sensor class, create getter functions for is_open and bypassed
  4. Create the Location class as below:
    class Location
    {
        private:
            Sensor * sensors[100];
            int count;  // keeps track of sensors added to array
            int opensensors;
            int bypassedsensors;
            bool armed;
        public:
            Location()
            {
                count = 0;
                armed = false;
            }
    
  5. Create a function void add(string name, bool byp, bool io). In this function, call the global create() function. The address of the object returned by the create() function should be stored into the next unused slot in the sensors array. The count variable is perfect to use as an array subscript – also don’t forget to increment the count variable so it points to the next unused array element. Understand that this add() function only adds a single sensor into the array.
  6. Create a function bool arm(). This is used to “arm” the system. The system can be armed if all the sensors are closed or bypassed. This function will examine all the sensors, updating the opensensors and bypassedsensors data members accordingly. The function returns true if the system can be armed, and false otherwise.
  7. Create a destructor function. In this function, make sure to delete each dynamic sensor object stored in the sensors array.
  8. Create getter functions for the data members opensensors and bypassedsensors.
  9. You can use the main() given below. You can create additional supporting functions as needed.

Sample main()

int main()
{
    int sensors;
    string sensorname;
    char input;
    bool isopen, isbyp;

    cout << "How many sensors are installed? "; 
    cin >> sensors;

    Location home;

    for (int i = 0; i < sensors; i++)
    {
        cout << "Name this sensor: "; 
        cin >> sensorname;

        cout << "Is " << sensorname << " currently open? "; 
        cin >> input;
        isopen = input == 'y';

        cout << "Bypass " << sensorname << "? "; 
        cin >> input;
        isbyp = input == 'y';

        home.add(sensorname, isbyp, isopen);
    }

    if (home.arm())
    {
        cout << "Alarm system is armed. " << home.get_bypassed() << " sensor(s) bypassed." << endl;
    }
    else
    {
        cout << "Cannot arm system. " << home.get_opened() << " sensor(s) opened." << endl;

        // EXTRA CREDIT
        // Sample output:
        //    Cannot arm system. Open sensors : Kitchen, Garage
        // display which sensors are opened
        // make sure to not violate OOP rules of encapsulation
        // No cins/couts from within any classes
    }
  return 0;
}

 

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0068

Print Requirements