List of all available Websheets
Viewing cpp/cs104/classes/default-args 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 <tt>Student</tt> class/struct below with its many constructor options. <p>Tru using <strong>default arguments</strong> to reduce the number of constructors needed.</p> <p><strong>Learning goal:</strong> </p> <ul><li>How to take advantage of and use default arguments.</li> <li>Where to place default arguments (in either the prototype (preferred) or the implementation but not both!</li> </ul> |
Remarks Comments, history, license, etc. | Copied from problem cpp/cs104/classes/ctor-default (author: redekopp@usc.edu) Copied from problem cpp/cs104/classes/constructor_init (author: redekopp@usc.edu) |
Engine | |
Template / Reference solution |
using namespace std; class Student { public: \[ REDACTED \show: Student(); Student(string n); Student(string n, int i); Student(string n, int i, double g); void print() { cout << "Student:\n" << name << endl; cout << id << endl << gpa << endl; } ]\ private: string name; int id; double gpa; }; \[ REDACTED \show: Student::Student() { name = "Tommy Trojan"; id = 0; gpa = 3.2; } Student::Student(string n) { name = n; id = 0; gpa = 3.2; } Student::Student(string n, int i) { name = n; id = i; gpa = 3.2; } Student::Student(string n, int i, double g) { name = n; id = i; gpa = g; } ]\ int main() { \[ REDACTED \show: Student s1; s1.print(); Student s2("Alice"); s2.print(); Student s3("Bob",2); s3.print(); Student s4("Charlie",3,3.9); s4.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 |
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'.