List of all available Websheets
Viewing cpp/classes/recorder by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Make a short recorder that can store a message to be played back. It should have the following API: <ul> <li><tt>Recorder(string initialMsg) // create new recorder w/this message initially <li><tt>string playback() // return the saved message <li><tt>void record(string newMsg) // save this message instead</tt> </ul> |
Public permissions | |
Engine | |
Template / Reference solution |
using namespace std; // normally this would go in recorder.h class Recorder { public: Recorder(string initialMsg); string playback(); void record(string newMsg); private: string savedMsg; }; // normally this would go in recorder.cpp // constructor Recorder::Recorder(\[string initialMsg]\) { // save the initial message \[savedMsg = initialMsg;]\ } // accessor: get back the saved message string Recorder::\[playback()]\ { return \[savedMsg]\; } // mutator: replace the saved message \[ void Recorder::record(string newMsg) { // now the new message is saved (the old one is forgotten) savedMsg = newMsg; } ]\ // test client int main() { Recorder r("hello"); cout << "The recorded message is: " << r.playback() << endl; cout << "Let's play it again: " << r.playback() << endl; r.record("bonjour"); cout << "The changed message is: " << r.playback() << endl; Recorder card("happy birthday!"); cout << "The new recorder says: " << card.playback() << endl; cout << "The old one still says: " + r.playback() << endl; } |
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 |
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'.