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

List of all available Websheets


Viewing cpp/cs104/classes/constructor_init2 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
 
Consider the initialization of <tt>ObjB::a</tt> (the <tt>a</tt> member of the <tt>ObjB</tt>).  Our goal is to initialize <tt>ObjB::a</tt> with the value of <tt>w</tt> being passed into the constructor of <tt>ObjB</tt>.
<p>
What output do you expect?  Run the given code.
<p>
Try to make the execution of the constructor as efficient has possible (no unnecessary code execution).
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/cs104/classes/constructor_init (author: redekopp@usc.edu)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <algorithm>
using namespace std;
class ObjA
{
public:
   ObjA() { cout << "Default" << endl;
            val = 5; }
   ObjA(int v) {
      val = v;  
   }
   int get() const { return val; }
   void set(int v) { val = v; }
private:
   int val;
};
class ObjB
{
public:
   ObjB(int w) 
\[
REDACTED
\show:
   {
      cout << a.get() << endl;
      a.set(w);
      cout << a.get() << endl;
   }
]\
   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
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'.