List of all available Websheets
Viewing cpp/cs104/classes/const_members2 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 | In this example <tt>printValue()</tt> calls another member function <tt>getValue()</tt>. <p> In the function <tt>f1()</tt> we pass the <tt>Item</tt> as a <strong>const reference</strong>. <p> Determine whether <tt>printValue()<tt> and/or <tt>getValue</tt> need to be marked as <tt>const</tt> member functions. |
Remarks Comments, history, license, etc. | Copied from problem cpp/cs104/classes/const_members (author: redekopp@usc.edu) Copied from problem cpp/cs104/classes/this_scope (author: redekopp@usc.edu) |
Engine | |
Template / Reference solution |
using namespace std; class Item { public: // experiement with placing const // after the member func. declaration void printValue() \[ REDACTED ]\ ; int getValue() \[ REDACTED ]\ { return value; } void setValue(int value); private: int value; }; // If you mark a member function const in the declaration // you need to also add 'const' in the definition void Item::printValue() \[ REDACTED ]\ { cout << this->getValue() << endl; } void Item::setValue(int val) { this->value = val; } // experiment void f1(const Item& i) { i.printValue(); } int main() { Item x; x.setValue(2); f1(x); 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 default compiler flag(s) | ["-Wno-shadow", "-Wno-self-assign"] |
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'.