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

List of all available Websheets


Viewing cpp/cs104/classes/ctor-init-list2 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 <strong>NOT</strong> using the initialization list will be inefficient
calling the <strong>default constructor</strong> and then the <strong>operator=</strong> 
   
</ol>
<p><strong>Learning goal:</strong> </p>
<ul><li>You should take advantage of the constructor initialization list.</strong></li>
</ul>
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/cs104/classes/ctor-initialization-list (author: redekopp@usc.edu)
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() { 
      cout << "A1 default ctor" << endl; 
      for(double i = 0; i < 10; i += 0.2) { x += i; }
   }
   A1(double myx) { cout << "Init A1 ctor" << endl; x = myx; }
   A1& operator=(const A1& other) 
   { cout << "A1 operator=" << endl; x = other.x; return *this; }
   double x;
]\
};
class A2 {
public:
\[
REDACTED
\show:
   A2() : m1(0), m2(0) { cout << "Default A2 ctor" << endl;}
   A2(const A1& mym1, const A1& mym2) {
      cout << "Init A2 ctor" << endl;
      m1 = mym1;
      m2 = mym2;
   }
]\
   void print() const { cout << "m1=" << m1.x << " m2=" << m2.x << endl;}
private:
   A1 m1;
   A1 m2;
};
int main()
{
\[
REDACTED
\show:
   A1 mym1(3.8);  A1 mym2(3.95);
   A2 obj1(mym1, mym2);
   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'.