Suppose there is a class Alarm. Alarm has two class variables , code which conta
ID: 3770688 • Letter: S
Question
Suppose there is a class Alarm. Alarm has two class variables , code which contains a String value representing the code that deactivates the alarm, and armed which contains a boolean describing whether or not the alarm is activated.
Alarm has a function changeCode that takes two parameters containing Strings, the first representing the current code, and the second representing the new code. If the user inputs the current code correctly, the value of code is changed to that of the new code. Call the changeCode function on theAlarm object myAlarm, whose current code is "3456", and change the code to "7921".
Explanation / Answer
#include <iostream>
using namespace std;
class Alarm
{
private:
string code;
bool armed;
public:
Alarm()
{
code = "3456";
armed = false;
}
void changeCode(string current_code,string new_code)
{
if(current_code == code)
code = new_code;
}
string get_code(){ return code; }
bool get_armed(){ return armed; }
};
int main()
{
Alarm myAlarm;
cout<<"Original code = "<<myAlarm.get_code()<<endl;
myAlarm.changeCode("1234","7921");
//1234 does not matches with the original code hence code will not change
cout<<"Modified code = "<<myAlarm.get_code()<<endl;
myAlarm.changeCode("3456","7291");
// 3456 matches with the original code hence code will be changed
cout<<"Modified code = "<<myAlarm.get_code()<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.