List of all available Websheets
Viewing cpp/cs104/polymorphism/private_inh 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 | Experiment with public vs. private inheritance and polymorphism |
Remarks Comments, history, license, etc. | Copied from problem cpp/cs104/inheritance/public_private_inh (author: redekopp@usc.edu) Copied from problem cpp/cs104/inheritance/person_stu_ex (author: redekopp@usc.edu) |
Engine | |
Template / Reference solution |
using namespace std; enum {CS=0, CECS, CSBA, EE}; class Person { public: Person() : name_(), id_(0) { } Person(std::string myname, int myid) : name_(myname), id_(myid) { } ~Person() {} std::string get_name() const { return name_; } int get_id() const { return id_; } virtual void print_info() const { cout << name_ << " with ID=" << id_ << endl; } private: std::string name_; int id_; }; // Change from public to private and see the effect class Student : \[ REDACTED ]\ Person { public: Student() {} // we should initialize members // but let's not worry about this Student(string n, int ident, int mjr); int get_major() const { return major_; } double get_gpa() const { return gpa_; } void print_info() const { // Experiment accessing Person::name_ or Person::get_name() // based on public/private inheritance cout << get_name() << " has a gpa of " << gpa_ << endl; } private: int major_; double gpa_; }; \hide[ Student::Student(string n, int ident, int mjr) : Person(n, ident) { major_ = mjr; gpa_ = 3.7; } ]\ int main() { \[ REDACTED ]\ = new Student("Tommy", 53221, CECS); \[ REDACTED ]\->print_info(); 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 [{}] | [{}] |
Solution visibility | |
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'.