Part C - Encapsulation
Class with a Resource
Workshop 5
In this workshop, you are to design and code a class that
accesses a resource.
Learning Outcomes
Upon successful completion of this workshop, you will have
demonstrated the abilities to
- allocate and deallocate dynamic memory
- code a copy constructor
- code an assignment operator
Staff List - All Groups
Employee Class
Design and code a class named Employee that holds information about an
employee. Place your class definition in a header file
named Employee.h and your function
definitions in an implementation file named Employee.cpp
Upon instantiation, an Employee
object may receive no information or two pieces of
information
- the employee's number
- the employee's name
Your design does not impose any limit on the number of
characters in the employee's name.
A valid employee number is positive-valued. If upon
instantiation the object receives an invalid value or an empty
string, the object takes on a safe empty state.
Your Employee class includes the
following public member functions:
- void display() const - displays on
standard output the employee number left-justified in a field
width of 8 followed by the employee's name
- bool isGreaterThan(const Employee&) const -
returns true if the Employee's name is
alphabetically later in the list than the name of the Employee
referenced in the function parameter.
Since your Employee class can process
names without limitation on length, your design needs to use
dynamic memory allocation and should also include
- a copy constructor
- an assignment operator
- a destructor
Client Module
The client program that uses your Employee
class prompts for information and accepts input for up to
ten (10) employees and displays the information in tabular form.
The implementation file for the main module that uses your
Employee class is:
// Workshop 5 - Staff List
// w5.cpp
#include <iostream>
using namespace std;
#include "Employee.h"
void sort(Employee* employee, int n);
int main() {
int no, n = 0;
Employee employee[MAX_EMPLOYEES];
cout << "Staff List Processor\n";
cout << "====================\n";
do {
cout << " Employee number (0 to quit) ? ";
cin >> no;
error = true;
if (cin.fail()) {
cin.ignore(2000, '\n');
cerr << "Bad character. Try again." << endl;
cin.clear();
}
if (cin.get() != '\n') {
cin.ignore(2000, '\n');
cerr << "Trailing characters. Try again." << endl;
}
else if (no < 0) {
cin.ignore(2000, '\n');
cerr << "Must be positive. Try again." << endl;
error = false;
n++;
finished = true;
}
else if (no != 0) {
cout << " Employee name ? ";
string str;
getline(cin, str);
if (str.length() > 0)
employee[n++] = Employee(no, str.c_str());
}
} while (no != 0 && n < MAX_EMPLOYEES);
cout << endl;
sort(employee, n);
cout << " Staff List\n\n";
cout << " Number Name\n";
cout << "------------------------------\n";
for (int i = 0; i < n; i++) {
employee[i].display();
cout << endl;
}
}
// sort sorts the elements of employee[n] in ascending order
//
void sort(Employee* employee, int n) {
int i, j;
Employee temp;
for (i = n - 1; i > 0; i--) {
for (j = 0; j < i; j++) {
if (employee[j].isGreaterThan(employee[j+1])) {
temp = employee[j];
employee[j] = employee[j+1];
employee[j+1] = temp;
}
}
}
}
|
A sample run of this program with your Employee class might look something like:
Staff List Processor
====================
Employee number (0 to quit) ? 999999
Employee name ? Zorro, Frank
Employee number (0 to quit) ? 888888
Employee name ? Black, Barbara
Employee number (0 to quit) ? 777777
Employee name ? Windsor, Tony
Employee number (0 to quit) ? 0
Staff List
Number Name
----------------------------
888888 Black, Barbara
777777 Windsor, Tony
999999 Zorro, Frank
|
Validation - Group Y
Design and code the Employee class described above.
Add a member function that accepts data from standard input:
- bool read() - reads input data from the
standard input stream
This function includes all of the input validation logic shown in the main program
listed above.
Client Module
The implementation file for the main module that uses your
Employee class is:
// Workshop 5 - Staff List
// w5.cpp
#include <iostream>
using namespace std;
#include "Employee.h"
void sort(Employee* employee, int n);
int main() {
int n = 0;
bool more = true;
Employee employee[MAX_EMPLOYEES];
cout << "Staff List Processor\n";
cout << "====================\n";
do {
more = employee[n].read();
if (more) n++;
} while (more && n < MAX_EMPLOYEES);
cout << endl;
sort(employee, n);
cout << " Staff List\n\n";
cout << " Number Name\n";
cout << "------------------------------\n";
for (int i = 0; i < n; i++) {
employee[i].display();
cout << endl;
}
}
// sort sorts the elements of employee[n] in ascending order
//
void sort(Employee* employee, int n) {
int i, j;
Employee temp;
for (i = n - 1; i > 0; i--) {
for (j = 0; j < i; j++) {
if (employee[j].isGreaterThan(employee[j+1])) {
temp = employee[j];
employee[j] = employee[j+1];
employee[j+1] = temp;
}
}
}
}
|
This upgraded version has encapsulated more of the logic
within your Employee class.
SUBMISSION
Typescript
Create a typescript of your complete solution
using the following commands:
+ At the prompt, type: script w5.txt
+ At the prompt, type: whoami
+ At the prompt, type: cat Employee.h Employee.cpp
+ At the prompt, type: g++ -o w5 w5.cpp Employee.cpp
+ At the prompt, type: w5
+ At the input prompt, enter test data
+ At the prompt type: exit
|
These commands will produce a file named w5.txt. Download this file to your local
computer.
Moodle
Upload your typescript file to Moodle:
- Login to Moodle
- Select your course code
- Select W5 under Assignments and Tasks
- Upload w5.txt
- Write a short note to your instructor
- press "Edit"
- enter your conclusions about this workshop in the notes textbox
- press "Save Changes"
- When ready to submit, press "Send for Marking"
Other Submissions
Submit your typescript file as your instructor has specified.
|