List of all available Websheets
Viewing cpp/cs104/classes/constructor_init by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Consider the initialization of <tt>ObjB::a</tt> (i.e. we want to initialize <tt>val</tt> that is a member of <tt>a</tt>. Our goal is to initialize <tt>val</tt> with the value of <tt>w</tt> from the <tt>ObjB</tt> constructor. <p> How can we do this? <strong>Note:</strong> <tt>ObjA</tt> does not have any public <tt>set</tt>/<tt>mutator</tt> functions...only the constructor allows us to set <tt>value</tt> |
Engine | |
Template / Reference solution |
using namespace std; class ObjA { public: ObjA(int v) { val = v; } int get() const { return val; } private: int val; }; class ObjB { public: ObjB(int w) \[ REDACTED \show: { // what's wrong with this attempt? ObjA a(w); } ]\ int get() const { return a.get(); } private: ObjA a; }; int main() { ObjB b(10); cout << "B's output: " << b.get() << endl; 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 [{}] | [{}] |
Is example? i.e., just a demo |
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'.