#include <iostream>
using namespace std;
class Lock {
public:
void push(int button); // Simulates a digit button push
bool open(); // Simulates a push of the open button
private:
int combination = 1729; // Predefined combination
int input = 0; // Digits entered by the user
};
void Lock::push(int button) {
input = input * 10 + button; // Update input by appending the new digit
}
bool Lock::open() {
if (input == combination) { // Check if entered combination matches predefined combination
input = 0; // Reset input
return true; // Return true if lock opens
} else {
input = 0; // Reset input regardless of correctness
return false; // Return false if lock doesn't open
}
}
int main() {
Lock my_lock;
my_lock.push(1);
my_lock.push(7);
my_lock.push(3);
my_lock.push(9);
cout << boolalpha;
cout << my_lock.open() << endl;
cout << "Expected: false" << endl;
my_lock.push(1);
my_lock.push(7);
my_lock.push(2);
my_lock.push(9);
cout << my_lock.open() << endl;
cout << "Expected: true" << endl;
my_lock.push(1);
my_lock.push(7);
my_lock.push(2);
cout << my_lock.open() << endl;
cout << "Expected: false" << endl;
my_lock.push(9);
cout << my_lock.open() << endl;
cout << "Expected: false" << endl;
my_lock.push(1);
my_lock.push(7);
my_lock.push(2);
my_lock.push(9);
cout << my_lock.open() << endl;
cout << "Expected: true" << endl;
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: