Spring 2020

PIC 10B: Intermediate Programming

Discussion Section 2A

Wonjun Lee



Week3

Tuesday

Link to the video

Enter password:

Exercise P8.1.

Derive a class Programmer from Employee. Supply a constructor Programmer(string name, double salary) that calls the base-class constructor. Supply a function get_name that returns the name in the format "Hacker, Harry (Programmer)".


Solution
#include < iostream >
#include < string >

using namespace std;

class Employee
{
public:
  Employee();
  Employee(string pname, double psalary);
protected: // Inherited classes can use these member variables
  string name;
  double salary;
};

Employee::Employee():name(""), salary(0) {}
Employee::Employee(string pname, double psalary):name(pname), salary(psalary) {}

class Programmer: public Employee
{
pulbic:
  Programmer();
  Programmer(string pname, double psalary);
  string get_name() const;
};

Programmer::Programmer(): Employee() {}
Programmer::Programmer(string pname, double psalary): Employee() {}

string Programmer::get_name() const
{
  string new_name = name + " (Programmer)";
  return new_name;
}

int main()
{
  Programmer A("John", 100);
  cout << A.get_name() << endl;
}
Exercise P8.2.

Implement a base class Person. Derive classes Student and Instructor from Person. A person has a name and a birthday. A student has a major, and an instructor has a salary. Write the class deļ¬nitions, the constructors, and the member functions print() for all classes.


Solution
#include < iostream >
#include < string >

using namespace std;

class Person
{
public:
  Person();
  Person(string pname, string pdob);

  void print() const;
protected:
  string name;
  string dob;
};

Person::Person(){
  name = "default";
  dob = "00/0/0/000";
}
Person::Person(string pname, string pdob): name(pname), dob(pdob) {}

void Person::print() const{
  cout  << "name : " << name << " dob : " << dob << endl;
}

class Student: public Person
{
public:
  Student();
  Student(string pname, string pdob, string pmajor);

  void print() const;
private:
  string major;
};

Student::Student():Person(), major("NONE") {}
Student::Student(string pname, string pdob, string pmajor): Person(pname, pdob), major(pmajor) {}

void Student::print() const{
  cout  << "name : " << name << " dob : " << dob << " major : " << major << endl;
}

class Instructor: public Person
{
public:
  Instructor();
  Instructor(string pname, string pdob, double psalary);

  void print() const;
private:
  double salary;
};

Instructor::Instructor():Person(), salary(0) {}
Instructor::Instructor(string pname, string pdob, double psalary): Person(pname, pdob), salary(psalary) {}

void Instructor::print() const{
  cout  << "name : " << name << " dob : " << dob << " salary : " << salary << endl;
}

int main(){
  Person a1("JOhn", "1231231");
  Student a2("Paul", "2324254", "math");
  Instructor a3("George", "699393", 100);

  a1.print();
  a2.print();
  a3.print();
}

Office Hours

Link to the video (part 1/2)

Enter password:

Link to the video (part 2/2)

Enter password:

Thursday

No video (Forgot to click the recording button)
Exercise P8.3.
Solution
#include < iostream >
#include < string >

using namespace std;

class Employee
{
public:
  Employee();
  Employee(string pname, double psalary);
protected: // Inherited classes can use these member variables
  string name;
  double salary;
};

Employee::Employee():name(""), salary(0) {}
Employee::Employee(string pname, double psalary):name(pname), salary(psalary) {}

class Manager: public Employee
{
public:
  Manager();
  Manager(string pname, double psalary, string pdepartment);

  void print() const; // accessor
private:
  string department;
};

Manager::Manager()
:Employee() // base class constructor
{
  department = "";  
}

Manager::Manager(string pname, double psalary, string pdepartment)
:Employee(pname, psalary) // base class constructor
{
  department = "";  
}

void Manager::print() const
{
  cout << "Name : " << name << " salary : " << salary << " department : " << department << endl;
}

class Executive: public Manager
{
public:
  Executive();
  Executive(string pname, double psalary, string pdepartment);

  void print() const;
};

Executive::Executive()
:Manager()
{}

Executive::Executive(string pname, double psalary, string pdepartment)
:Manager(pname, psalary, pdepartment)
{}

void Executive::print() const{
  // cout << "(Executive) Name : " << name << " salary : " << salary << " department : " << department << endl;

  cout << "(Executive) ";
  Manager::print();
}

int main()
{
  Executive e("John", 100, "PIC");

  e.print(); // (Executive) Name : John salary : 100 department : PIC
}