List of all available Websheets
Viewing cpp/cs104/polymorphism/inh-hier 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 where <tt>virtual</tt> is defined in the <tt>print_info</tt> hierarchy. |
Remarks Comments, history, license, etc. | Copied from problem cpp/cs104/polymorphism/private_inh (author: redekopp@usc.edu) 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; class Person { public: Person(std::string myname, int myid) : name_(myname), id_(myid) { } \[ REDACTED \show: ~Person() ]\ { cout << "~Person" << endl; } std::string get_name() const { return name_; } int get_id() const { return id_; } \[ REDACTED \show: void print_info() const ]\ { cout << "Person: " << name_ << " with ID=" << id_ << endl; } private: std::string name_; int id_; }; class Student : public Person { public: Student(string n, int ident, double gpa); ~Student() { cout << "~Student "; } double get_gpa() const { return gpa_; } \[ REDACTED \show: void print_info() const ]\ { cout << "Student: " << get_name() << " has a gpa of " << gpa_ << endl; } private: double gpa_; }; \hide[ Student::Student(string n, int ident, double gpa) : Person(n, ident) { gpa_ = gpa; } ]\ class CSStudent : public Student { public: CSStudent(string n, int ident, double gpa, string favlang); ~CSStudent() { cout << "~CSStudent "; } string get_lang() const { return favlang_; } \[ REDACTED \show: void print_info() const ]\ { cout << "CS Student: " << get_name(); cout << " has favorite lang: " << favlang_ << endl; } private: string favlang_; }; \hide[ CSStudent::CSStudent(string n, int ident, double gpa, string favlang) : Student(n, ident, gpa) { favlang_ = favlang; } ]\ int main() { bool shouldDelete = \[ REDACTED \show: false ]\ ; Person *p1 = new Student("Tommy", 1234, 3.2); Person *p2 = new CSStudent("Jill", 5678, 3.7, "c++"); Student *p3 = new CSStudent("Tanya", 9876, 3.5, "!c++"); p1->print_info(); p2->print_info(); p3->print_info(); if(shouldDelete) { delete p1; delete p2; delete p3; } 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'.