Description
The purpose of this challenge is to create structs and 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
- Define a struct called Sensor as below
struct Sensor { string name; bool is_open; bool is_bypassed; Sensor() {} Sensor(string n, bool byp = false, bool io = false) { name = n; is_open = io; is_bypassed = byp; } }; - Below the Sensor struct, write a global (non-class) function called Sensor * create(string name, bool is_bypassed, bool is_open). This function will:
- Dynamically create a new Sensor instance. Remember that the Sensor struct has an overloaded constructor you can use.
- Return the address of the newly created Sensor instance
- If Sensor was a class, you would have to create getter functions for is_open and is_bypassed. However, you don’t need to. Since Sensor is a struct, you can simply access data mumbers directly in the following steps.
- 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; } - 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 will be stored into the next unused/available 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.
- 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.
- Create a destructor function. In this function, make sure to delete each dynamic sensor object stored in the sensors array.
- Create getter functions for the data members opensensors and bypassedsensors.
- You can use the main() given below. You can create additional supporting functions as needed.
- In main(), notice that we are creating a dynamic object home. There’s really no reason in main to create this dynamically — we could easily have simply declared it as local/static (Location home). For this challenge, we opted to have home be a pointer to a dynamic object simply for the practice of using dynamic objects as well as the arrow operator.
Sample main()
int main()
{
int sensors;
string sensorname;
char input;
bool isopen, isbyp;
cout << "How many sensors are installed? ";
cin >> sensors;
Location * home = new Location;
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 or structs
}
delete home;
return 0;
}
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL0068b
Print Requirements
