Not logged in. Log in with GitHub. About Websheets.

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.
PropertyValue
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)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <string>
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 remove
Is example?
i.e., just a demo
remove


Optional properties:

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'.