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

List of all available Websheets


Viewing cpp/cs104/inheritance/person_stu_ex 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
 
Consider the Person class and the derived Student class.  Write the derived constructor.
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_; } 
\[
REDACTED
\show:
  private: 
]\
   std::string name_;
   int id_;
};
class Student : public 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;
 private:
   int major_;
   double gpa_;
};
\[
REDACTED
\show:
Student::Student(string n, int ident, int mjr)
{
   name_ = n;
   id_ = ident;
   major_ = mjr;
   gpa_ = 3.7;         
}
]\
void Student::print_info() const
{   
\[
REDACTED
\show:
   cout << get_name() << " has ID=" << get_id() << " and major " << major_ << endl;
]\
}
int main()
{
   Student s1("Tommy", 53221, CECS);
   cout << s1.get_name() << endl;
   s1.print_info();
}
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'.