List of all available Websheets
Viewing cpp/cs104/inheritance/public_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 of Student from Person. <p> Based on the inheritance type, check whether: <ul> <li>Student member functions (e.g. <tt>print_info()</tt>) can access <tt>name_</tt> and <tt>get_name()</tt></li> <li>Non members (i.e. <tt>main()</tt> can call <tt>get_name()</tt>)</li> </ul> |
Remarks Comments, history, license, etc. | 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_; } \[ REDACTED \show: private: ]\ std::string name_; int id_; }; 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 \[ REDACTED \show: cout << 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() { Student s1("Tommy", 53221, CECS); // Experiement with access to get_name() based on public/private inh. \[ REDACTED \show: cout << s1.get_name() << endl; ]\ } |
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'.