Homework 5 - Basic Inheritance
Due: Wednesday, February 22, 2012 at 5:30pm
NOTE: No late assignments will be accepted beyond Friday February 24th
so that the solutions can be posted before Midterm 2.
Coding Conventions
Use the same coding conventions as described in
Homework 1. Additionally, make sure to indent each
section of the class in the class definition.
Assignment
The purpose of this assignment is to create two children classes.
You will define the following class hierarchy:
Person
/ \
/ \
/ \
Student Faculty
Use hw5main.cpp as your main
function. If you use seperate compilation, you will need to modify hw5main.cpp
to include the filenames you use for your header files. Also, be sure to
submit all files (header files, implementation files and the modified
hw5main.cpp) if you use seperate compilation. Alternatively, you can just
code all of these class within one monolithic hw5.cpp file and submit just
that file.
Use the following lists of features to code each class.
Person class
The is the parent class. It defines the name, ID number and email address
for a person.
- Private section
-
- A C-style or C++ style string for the name
- An integer the ID number
- A C-style or C++ style string for the email address
- Public section
-
- Default constructor - Sets the name and email to the empty string. Sets
the ID to 0.
- Copy constructor - Copies the name, ID and email from the source object.
- Assignment operator - Copies the name, ID and email from the source object.
- bool setName(char *) - Takes a C-style or C++ style string (change the
parameter list if you use a C++ string). Copies that into the name variable.
Returns true for all strings.
- const char* getName() const - Returns a C-style or C++ style string (change
the return type if using a C++ string) containing the current name.
- bool setID(int) - Verifies that the proposed ID is between 1 and 999999 or
that the proposed ID is between 900000000 and 999999999. If the proposed ID
is valid, set the ID member variable and return true. Otherwise, return
false.
- const int getID() const - Returns the current ID number.
- bool setEmail(char *) - Verifies that the proposed email address contains
one @ character and at least one period character. If the proposed email
is valid, copy it to the email member variable and return true. Otherwise,
return false.
- const char* getEmail() const - Returns a C-style or C++ style string (change
the return type if using a C++ string) containing the current email address.
- void inputPerson() - Prompts the user for the name, ID and email address.
Uses the set functions (setName, setID, etc) to set the values. If any of
the set functions returns false (invalid data has been given), the user
will be prompted to re-enter that piece of information.
- void printPerson() const - Print the name, ID and email address to the screen.
Student class
This class inherits from the Person class publically. It defines a student
with a number of units taken, total weighted grades (the grade for each course
multiplied by the units for that course is the weighted grade) and major.
- Private section
-
- A double for the number of units taken
- A double for the total weighted grades
- A C-style or C++ style string for the major
- Public section
-
- Default constructor - Sets the number of units and total weighted grades
to 0. Sets the major to the empty string. Invokes the parent default
constructor to initialize name, ID and email address.
- Copy constructor - Copies the number of units, total weighted grades, and
major from the source object. Invokes the parent's copy constructor to
copy the name, ID, and email address.
- Assignment operator - Calls the parent assignment operator to copy the
name, ID and email address from the source object. The copies the number
of units, total weighted grades, and major from the source object.
- bool setUnits(double) - Verifies that the given double is greater than
0. If it is, it copies the double over to number of units and returns
true. If it is not, it leaves the number of units untouched and returns
false.
- double getUnits() const - Returns the current number of units.
- bool setWeightedGrades(double) - Verifies that the given double is
positive (0 is fine too). If it is, it copies the double over to the total
weighted grades and returns true. Otherwise, return false.
- double getWeightedGrades() const - Returns the totale weighted grades.
- bool setMajor(char *) - Sets the major to the given string and returns true.
- const char *getMajor() const - Returns the current major.
- double calcGPA() const - Calculates the current GPA of the student by dividing
the total weighted grades by the number of units taken. If the number of
units taken is 0, return 0. Otherwise, return the result of division.
- void inputPerson() - Calls the parent version of inputPerson to read in the
name, ID and email address. Then it prompts for the units taken, total
total weighted grades and major. Uses the set functions (setUnits, etc)
to set the values. If any of the set functions returns false (invalid data
has been given), the user will be prompted to re-enter that piece of
information.
- void printPerson() const - Calls the parent version of printPerson to print
the name, ID and email address. Then prints out the current GPA (call the
calcGPA()
function) and major of the student.
Faculty class
This class inherits from the Person class publically. It defines an faculty
member who works for a department and gets paid a monthly salary.
- Private section
-
- A C-style or C++ style string for the department
- A double for the monthly salary
- Public section
-
- Default constructor - Sets the salary to 0. Sets the department to the
empty string. Invokes the parent default constructor to initialize the
name, ID and email address.
- Copy constructor - Copies the salary from the source object. Copies the
department from the source object. Invokes the parent copy constructor to
copy the name, ID and email address.
- Assignment operator - Calls the parent assignment operator to copy the
name, ID and email address from the source object. The copies the salary
and department from the source object.
- bool setSalary(double) - Verifies that the given double is greater than
0. If it is, it copies the double to the salary and returns true. Otherwise,
it leaves the salary untouched and returns false.
- double getSalary() const - Returns the current salary.
- bool setDepartment(char *) - Sets the department to the given string and
returns true.
- const char *getDepartment() const - Returns the current department.
- void inputPerson() - Calls the parent version of inputPerson to read in the
name, ID and email address. Then it prompts for the salary and department.
Uses the set functions (setUnits, etc) to set the values. If any of the
set functions returns false (invalid data has been given), the user will
be prompted to re-enter that piece of information.
- void printPerson() const - Calls the parent version of printPerson to print
the name, ID and email address. Then prints out the salary and department
of the faculty member.
Email either all the separate compilation files or the one monolithic hw5.cpp
file to submit the assignment.