In-Class Exercise
Derived Classes
This exercise distinguishes more general data from more specific
data to creating a simple class hierarchy.
Given Information
Parts of the following code are extracted from the starting
Employee example in the Handout on
Member Operators.
Client Module
Consider the client program listed on the left and the output
shown on the right:
// Client Program
// h12.cpp
#include <iostream>
#include "Employee.h"
int main() {
Employee employee(1234, "Jane Doe", 15.50);
employee.display(std::cout);
std::cout << std::endl;
}
|
Jane Doe (1234) $15.50
|
Employee Module
The Employee module used by the client program
is defined in the following two files.
Employee.h
// Employee
// Employee.h
#include <iostream>
const int MAX_NAME = 20;
class Employee {
int no;
char name[MAX_NAME + 1];
double rate;
public:
Employee();
Employee(int, const char*, double);
void display(std::ostream&) const;
};
|
Employee.cpp
// Employee
// Employee.cpp
#include <iomanip>
#include <cstring>
#include "Employee.h"
const double MIN_WAGE = 10.25;
const int SAFE_NUM = 0;
Employee::Employee() {
// safe empty state
no = SAFE_NUM;
name[0] = '\0';
rate = MIN_WAGE;
}
Employee::Employee(int n, const char* nm, double w) {
if (n > SAFE_NUM && r >= MIN_WAGE) {
no = n;
strncpy(name, nm, MAX_NAME);
name[MAX_NAME] = '\0';
rate = r;
} else {
Employee temp;
*this = temp;
}
}
void Employee::display(std::ostream& os) const {
if (no != SAFE_NUM) {
os << std::fixed << std::setprecision(2);
os << name << " (" << no << ") $" << rate;
}
else
os << "no data available";
}
|
Your Task
Divide the Employee class
into two definitions
- a Person base class
- an Employee derived class
Redefine the member functions for each class
by rearranging the existing logic.
Employee Module
Employee.h
The Person class holds a person's name
in a private data member and provides access through three member functions:
- Person() - no-argument constructor that initializes the data member
- void set(const char*) - copies the name into the data member
- void displayName(std::ostream& os) const - inserts the name into os
The Employee class holds an employee's
number and wage rate in its private data members and provides access
through two member functions:
- Employee(int, const char*, double) - three-argument constructor
that receives the number, the name and the wage rate
- void display(std::ostream& os) const - inserts all data into os
The header file contains both class definitions:
// Derived Classes
// Employee.h
#include <iostream>
const int MAX_NAME = 20;
class Person {
public:
};
class Employee {
public:
Employee();
Employee(int, const char*, double);
void display(std::ostream&) const;
};
|
Employee.cpp
The implementation file that contains the function definitions
for both classes:
// Derived Classes
// Employee.cpp
#include <iomanip>
#include <cstring>
#include "Employee.h"
Person::Person() {
}
void Person::set(const char* n) {
}
void Person::displayName(std::ostream& os) const {
}
const double MIN_WAGE = 10.25;
const int SAFE_NUM = 0;
Employee::Employee() {
// safe empty state
no = SAFE_NUM;
name[0] = '\0';
rate = MIN_WAGE;
}
Employee::Employee(int n, const char* nm, double r) {
if (n > SAFE_NUM && r >= MIN_WAGE) {
} else {
Employee temp;
*this = temp;
}
}
void Employee::display(std::ostream& os) const {
if (no != SAFE_NUM) {
os << std::fixed << std::setprecision(2);
}
else
os << "no data available";
}
|
Call the set() member function from the
Employee three-argument constructor.
Code the display() member
function to display the employee's name before their number and
wage rate.
|