Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/classes/combolock by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Create a 'combination lock' which has a <tt>string</tt> password that you can change,
which you can lock and unlock. It should have this API:
<ul>
<style>li {margin-bottom: 9pt;} </style>
<li><tt>
// construct new lock with this initial password, initially unlocked<br>
CombinationLock(string initpass) 
</tt>
<li><tt>// is this lock currently locked?<br>bool isLocked()</tt>
<li><tt>// if current password equals attempt, unlock (no effect if already unlocked)<br>
void tryUnlock(string attempt)</tt>
<li><tt>// make this lock become locked (no effect if already locked)<br>void lock()</tt>
<li><tt>// if locked, print "ERROR: LOCKED" else change password to newpass<br>
void changePassword(string newpass)</tt>
</ul>
<i>Note: in practice, passwords are not stored directly, but only as <a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function#Password_verification">hashes</a>.
Public permissions remove
Remarks
Comments, history, license, etc.
 
Originally by Mark Redekopp (redekopp@usc.edu) and Dave Pritchard (daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <string>
using namespace std;
\[
class CombinationLock {
public:
   CombinationLock(string initpass);
   void tryUnlock(string attempt);
   bool isLocked();
   void lock();
   void changePassword(string newpass);
private:
   bool locked; // is it locked right now?
   string password; // current password
};
// construct new lock with this initial password, initially unlocked
CombinationLock::CombinationLock(string initpass) {
   password = initpass; // save password
   locked = false; // initially unlocked
}
// is this lock currently locked? 
bool CombinationLock::isLocked() {
   return locked;
}
// if current password equals attempt, unlock (no effect if already unlocked)
void CombinationLock::tryUnlock(string attempt) {
   if (attempt == password)
      locked = false;
}
// make this lock become locked (no effect if already locked)
void CombinationLock::lock() {
   locked = true;
}
// if locked, print "ERROR: LOCKED" else change password to newpass
void CombinationLock::changePassword(string newpass) {
   if (locked) 
      cout << "ERROR: LOCKED" << endl;
   else
      password = newpass;
}
]\
int main() {
   cout << boolalpha;
   CombinationLock myLock("0000");
   cout << myLock.isLocked() << endl; // false
   myLock.lock();
   cout << myLock.isLocked() << endl; // true
   myLock.changePassword("new-pass"); // -- ERROR: LOCKED
   cout << myLock.isLocked() << endl; // true
   myLock.tryUnlock("2014"); // -- shouldn't unlock, password is 0000
   cout << myLock.isLocked() << endl; // true
   myLock.tryUnlock("0000"); // -- unlocks!
   cout << myLock.isLocked() << endl; // false
   myLock.changePassword("hello123"); // -- password change OK
   cout << myLock.isLocked() << endl; // false
   myLock.lock();
   cout << myLock.isLocked() << endl; // true
   myLock.tryUnlock("0000"); // -- shouldn't unlock, wrong password now
   cout << myLock.isLocked() << endl; // true
   myLock.tryUnlock("hello123"); // -- unlocks!
   cout << myLock.isLocked() << endl; // false
   myLock.lock();
   cout << myLock.isLocked() << endl; // true
   return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {}
]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.