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

List of all available Websheets


Viewing cpp/cs104/classes/ctor-init-list 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
 
Build on your previous exercise to see how you can initialize members <strong>that require a non-default constructor to be called.</strong>
   
</ol>
<p><strong>Learning goal:</strong> </p>
<ul><li>When you need to call constructors for members, you must do so in the <strong>constructor initialization list</strong></li>
</ul>
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/cs104/classes/ctor-dtor-members (author: redekopp@usc.edu)
Copied from problem cpp/cs104/classes/ctor-default (author: redekopp@usc.edu)
Copied from problem cpp/cs104/classes/constructor_init (author: redekopp@usc.edu)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <algorithm>
using namespace std;
struct A1 {
\[
REDACTED
\show:
   A1(double myx) { cout << "Init A1 ctor" << endl; x = myx; }
   ~A1() { cout << "A1 dtor" << endl; }
   double x;
]\
};
class A2 {
public:
\[
REDACTED
\show:
   A2() { cout << "Default A2 ctor" << endl;}   
   ~A2() { cout << "A2 dtor" << endl; }
]\
   void print() const { cout << "m1=" << m1.x << " m2=" << m2.x << endl;}
private:
   A1 m1;
   A1 m2;
};
int main()
{
\[
REDACTED
\show:
   A2 obj1;
   obj1.print();
]\
   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'.