CMPT 212 Assignment 3

David J. Simpson (simpson@cs.sfu.ca)
Wed, 29 Oct 1997 19:49:05 -0800


CMPT 212                      Assignment 3
Fall 1997                     ------------

Due date:  Start of class, Wednesday, November 12, 1997.

1. Goal

   - To become familiar with C++ class inheritance, abstract classes,
       and virtual functions.

2. Overview

   - This program extends Assignment 2 to include class inheritance.
   - A class hierarchy will be defined to model different kinds of
       persons.
   - As a result, the program will support more than kind of person.
   - From a user's perspective, the program will appear to be very
       much like the Assignment 2 program, except that it will have
       different kinds of persons.

3. Description

   3.1 General
       - The program will be a text-based, menu-driven program.
       - The program will maintain two lists of people.  For each person,
          certan information will be kept, depending on the kind of
          person.
       - The program will have four functions:
            a.  Add a person to the end of a list.
            b.  Display a list.
            c.  Empty a list.
            d.  Copy the first list to the second list.
       - The function are the same as in Assignment 2, except that
          "Add Person" will ask the user for the kind of person to
          add.

   3.2 Main Menu
      - The menu will look exactly like this:

              1.  Add person
              2.  Print list
              3.  Empty list
              4.  Copy list 1 to list 2
              5.  Quit

              > _

          (The "_" is the cursor, waiting for the user's input.)
      - When the user enters a choice, the program will carry out
          the operation selected by the user.
      - After the operation has been carried out, the program will
          display the Main Menu and wait for the user to enter
          another choice.  (If the user selects "Quit", the program
          will quit instead of displaying the Main Menu.)

   3.3 "Add Person" Operation
      - This operation is the same as in Assignment 2, except that
          the program asks for the kind of person and the information
          read in will depend on the kind of person.
      - When the user presses "1" at the Main Menu, the program
          will ask the user the kind of person to add, then read in
          the information about the person.  The kinds of persons are
          student, instructor, or programmer.  Here is the information
          which is read in for each kind of person:
              student:    name, department
              instructor: name, department, salary
              programmer: name, company, salary
          Note that only one name is stored for the person, not first
          and last names.
      - The user will also be asked which list (1 or 2) to add to.
      - This information about the person will be added to the end
          of the chosen list.

   3.4 "Print List" Operation
      - This operation is the same as in Assignment 2, except that
          the information displayed for each person will depend on
          the kind of person.
      - When the user presses "2" at the Main Menu, the program
          will display the contents of a list on the screen.
      - The user will be asked which list (1 or 2) to display.
      - For each person in the chosen list, the information about that
          person will be displayed.
      - After the list has been displayed (whether the list is
          empty or not), this message will be displayed:
              "There are X people in the list."
          where X is the number of people in the list.

   3.5 "Empty List" Operation
      - This operation is the same as in Assignment 2.
      - When the user presses "3" at the Main Menu, the program
          will remove every person from a list.
      - The user will be asked which list (1 or 2) to empty.

   3.5 "Copy List" Operation
      - This operation is the same as in Assignment 2.
      - When the user presses "4" at the Main Menu, the program
          will empty list 2, then copy every person from list 1
          into list 2.

   3.6 "Quit" Operation
      - This operation is the same as in Assignment 2.
      - When the user presses "5" at the Main Menu, the program
          will exit.

4. Design of the Program

   4.1 Modules
      - The program will consist of three modules: a "Person" module,
          a "Person List" module, and "Main" module.

   4.2 The Person Module
      - This module defines a class hierarchy for the different
          kinds of persons.
      - See section 4.5 for a description of the classes in the hierarchy.
      - The source files for this module will be called "person.h" and
          "person.cpp".

   4.3 The Person-List Module
      - This module defines a class called "plist" which models a
          list of people.
      - The source files for this module will be called "plist.h" and
          "plist.cpp".
      - The "plist" class must have these member functions:
           - a default constructor
                plist();
           - a copy constructor
                plist(const plist&);
           - a destructor
                ~plist();
           - an assignment operator
                const plist& operator=(const plist&);
           - an "addToEnd" function
                void addToEnd(person *);   //  Note changed parameter.
           - a "getFirst" function
                person * getFirst();       //  Note changed parameter.
           - a "getNext" function
                person * getNext();        //  Note changed parameter.
           - an "atEnd" function
                bool atEnd() const;
           - a "clearList" function
                void clearList();
      - The output (<<) operator must be overloaded so that instances
          of the "plist" class can be outputted.  You do not need
          an input operator for "plist".
                ostream& operator<<(ostream&, const plist&);
      - Each element in the list must not contain the person data, but
          must contain a pointer to an instance of either a "student"
          instance, an "instructor" instance, or a "programmer" instance.
          These instances should be allocated dynamically.

   4.4 The Main Module
      - The source file for this module will be called "asgn2.cpp".
      - This modules contains the main function of the program.

   4.5 Person Class Hierarchy

      4.5.1 Person Class
         - This class models a person.
         - This must be an abstract class.
         - Data members:
              char name[20];
         - Function members:
              default constructor
                 person(); 
              copy constructor
                 person(const person&);
              "clone" function (virtual function)  This function
              dynamically allocates a copy of the instance, and
              returns a pointer to it.
                 person * clone() const = 0;
              read function (virtual function)
                 void read() = 0;
              display function (virtual function)
                 void display() = 0;
              read name function
                 void readName();
              display name function
                 void displayName();

      4.5.2 Academic Class
         - This class models an object that belongs to a school.
         - Data members:
              department name
                 char dept[4];
         - Function members:
              default constructor
                 academic();
              copy constructor
                 academic(const academic&);
              read department function
                 void readDept();
              display department function
                 void displayDept();

      4.5.3 Industry Class
         - This class models an object that belongs to a company.
         - Data members:
              company name
                 char company[4];
         - Function members:
              default constructor
                 industry();
              copy constructor
                 industry(const industry&);
              read company name function
                 void readCompany();
              display department function
                 void displayCompany();

      4.5.4 Employee Class
         - This class models an employee.
         - The "employee" class is derived from the "person" class.
         - Addition data members:
              salary
                 int salary;
         - Additional or overridden function members:
              default constructor
                 employee();
              copy constructor
                 employee(const employee&);
              read salary function
                 void readSalary();
              display salary function
                 void displaySalary();

      4.5.5 Student Class
         - This class models a student.
         - The "student" class is derived from the "person" class
             and the "academic" class.
         - Additional data members:
              (none)
         - Additional or overridden function members:
              default constructor
                 student();
              copy constructor
                 student(const student&);
              "clone" function 
                 person * clone() const;
              read function
                 void read();
              display function
                 void display();

      4.5.6 Instructor Class
         - This class models an instructor.
         - The "instructor" class is derived from the "employee" class
             and the "academic" class.
         - Additional data members:
              (none)
         - Additional or overridden function members:
              default constructor
                 instructor();
              copy constructor
                 instructor(const instructor&);
              "clone" function 
                 person * clone() const;
              read function
                 void read();
              display function
                 void display();

      4.5.7 Programmer Class
         - This class models a programmer.
         - The "programmer" class is derived from the "employee" class
             and the "industry" class.
         - Additional data members:
              (none)
         - Additional or overridden function members:
              default constructor
                 programmer();
              copy constructor
                 programmer(const programmer&);
              "clone" function 
                 person * clone() const;
              read function
                 void read();
              display function
                 void display();

 
5. Design Features

   5.1 Error Checking
      - Very little error checking needs to be done.
      - The program does not have to check for out-of-memory errors
          or input errors.

   5.2 Given Code
      - No code is given to you.
      - This program is based on Assignment 2.  If you prefer, you
          may base your program on my solution to Assignment 2, rather
          than on your solution to Assignment 2.

   5.3 List Implementation
      - The elements in the list must be allocated using the memory
          allocation operator "new", and must be released using the
          memory release operator "delete".
      - Each element in the list must not contain the person data, but
          must contain a pointer to an instance of either a "student"
          instance, an "instructor" instance, or a "programmer" instance.
          These instances should be allocated dynamically.

   5.4 Constants
      - The maximum length of a person's name (20), the maximum length
          of a department's name (4), and the maximum length of a 
          company's name (15) must be defined as constants in the program.
Each of 
          these constants must be defined in one location so that it 
          can be changed easily.

   5.5 Bool Type
      - If you are using a compiler which does not support the "bool"
          type, you must define the "bool" type yourself in a header
          file called "bool.h".  Your program must be able to compile
          without errors under the Borland C++ in the Assignment Lab.

   5.6 I/O
      - When inputting any person (or an instance of a class derived
          from person), use the "read" virtual function instead of the
          ">>" operator.  Similarly, use the "display" virtual function 
          for outputting instead of the "<<" operator.
      - You do not need to write the "<<" or ">>" operators for the
          "person" class or any class derived from the "person" class.
      - When outputting a list of people, you must use the "<<"
          operator for the "plist" class.

   5.7 Miscellaneous
      - Use "const" wherever possible.
      - Use referenes ("&") when appropriate. 
      - Use the smallest scope and lifetime for every variable,
          constant, and function.

6. Source Code Comments
   - Your source code must be fully commented.
   - An example of good commenting style is shown in my solution to
       Assignment 2.

7. Test Runs
   - Run this test of the program:
           Print list 1
           Print list 2
           Add one student to list 1
           Print list 1
           Print list 2
           Add one instructor to list 1
           Print list 1
           Print list 2
           Add one programmer to list 2
           Print list 1
           Print list 2
           Copy list 1 to list 2
           Print list 1
           Print list 2
           Empty list 1
           Print list 1
           Print list 2
           Copy list 1 to list 2
           Print list 1
           Print list 2
           Add one programmer to list 2
           Print list 1
           Print list 2

8. What to Hand In
   - Title page containing:
        a. Course number
        b. Assignment number
        c. Student name
        d. Student number
        e. Student email address
   - Listing of the source code in this order:
        a. asgn2.cpp
        b. person.h
        c. person.cpp
        d. plist.h
        e. plist.cpp
        f. bool.h  (if written -- see Section 5.5 above)
   - Script of the test run (see Section 7 above).

9. Marking
   - Besides the usual reasons for deducting marks (such as not
       handing in all of your code, or your program doesn't work
       completely), marks may be deducted for these reasons:
         - bad coding style
         - inconsistent coding style
         - bad commenting style
         - code which is difficult to understand
         - poor variable/constant/function names
         - failing to meet the requirements described here

10. Ammendments to this Assignment
   - Ammendments to this assignment may be emailed or posted on the
       web page.  Ammendments may be made to correct errors here
       or to clarify points.  Check your email regularly.

11. Sample Output
   - Here is a sample run of my program, to give you an idea of what
       it should look like.

          1.  Add person
          2.  Print list
          3.  Empty list
          4.  Copy list 1 to list 2
          5.  Quit
          
          > 1
          
          1) student,  2) instructor,  or 3) programmer? 1
          
          Enter the person's name (up to 20 characters): Elmer
          Enter the department (up to 4 characters): CMPT
          
          Which list (1 or 2): 1
          
          1.  Add person
          2.  Print list
          3.  Empty list
          4.  Copy list 1 to list 2
          5.  Quit
          
          > 1
          
          1) student,  2) instructor,  or 3) programmer? 2
          
          Enter the person's name (up to 20 characters): Suzanne
          Enter the salary (integer): 32000
          Enter the department (up to 4 characters): HIST
          
          Which list (1 or 2): 1
          
          1.  Add person
          2.  Print list
          3.  Empty list
          4.  Copy list 1 to list 2
          5.  Quit
          
          > 1
          
          1) student,  2) instructor,  or 3) programmer? 3
          
          Enter the person's name (up to 20 characters): Dave
          Enter the salary (integer): 100
          Enter the company name (up to 15 characters): McPrograms
          
          Which list (1 or 2): 1
          
          1.  Add person
          2.  Print list
          3.  Empty list
          4.  Copy list 1 to list 2
          5.  Quit
          
          > 2
          
          Which list (1 or 2): 1
          
          STUDENT
          Name: Elmer
          Department: CMPT
          
          INSTRUCTOR
          Name: Suzanne
          Salary: 32000
          Department: HIST
          
          PROGRAMMER
          Name: Dave
          Salary: 100
          Company: McPrograms
          
          There are 3 people in the list.
          
          1.  Add person
          2.  Print list
          3.  Empty list
          4.  Copy list 1 to list 2
          5.  Quit
          
          > 5


(end of assignment)