Question
IN C++ Define a class called Sensor as below class Sensor { private: string name; bool is_open; bool bypassed; public: Sensor() {} Sensor(string n, bool
IN C++
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; } };
Below the Sensor class, 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 object. Remember that the Sensor class has an overloaded constructor you can use.
Return the address of the newly created Sensor object
In the Sensor class, create getter functions for is_open and bypassed
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 should be stored into the next unused slot in the sensors array. The count variable is perfect to use as an array subscript also dont forget to increment the count variable so it points to the next unused array element.
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
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 couts from within any classes } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started