In-Class Exercise
Custom iostream Operators
This exercise overloads the extraction and insertion
operators, enabling client programs to extract an object
directly from an input stream and insert an object directly
into an output stream.
Given Information
The following code is a solution to the Handout on
Helper Operators.
Client Module
// Helper Operator
// h9.cpp
#include <iostream>
using namespace std;
#include "Employee.h"
int main() {
Employee null,
mark(3456, "Mark"),
rose(3457, "Rose", 10.70);
null.display();
cout << endl;
null = null + 0.50; // raise of 50 cents
null.display();
cout << endl;
mark.display();
cout << endl;
mark = mark + 0.50; // raise of 50 cents
mark.display();
cout << endl;
rose.display();
cout << endl;
rose = 0.50 + rose; // raise of 50 cents
rose.display();
cout << endl;
}
|
Employee Module
Employee.h
// Helper Operators
// Employee.h
const double MIN_WAGE = 10.25;
const int MAX_NAME = 20;
class Employee {
int no;
char name[MAX_NAME + 1];
double rate;
public:
Employee();
Employee(int, const char*);
Employee(int, const char*, double);
void display() const;
Employee& operator+=(double);
};
Employee operator+(const Employee&, double);
Employee operator+(double, const Employee&);
|
Employee.cpp
// Helper Operators
// Employee.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
#include "Employee.h"
using namespace std;
Employee::Employee() {
// safe empty state
no = 0;
name[0] = '\0';
rate = MIN_WAGE;
}
Employee::Employee(int n, const char* nm) {
Employee temp(n, nm, MIN_WAGE);
*this = temp;
}
Employee::Employee(int n, const char* nm, double r) {
if (n > 0 && nm[0] != '\0' && r >= MIN_WAGE) {
no = n;
strncpy(name, nm, MAX_NAME); // copy first MAX_NAME characters
name[MAX_NAME] = '\0'; // ensure that last byte is null
rate = r;
} else {
Employee temp;
*this = temp;
}
}
void Employee::display() const {
if (no > 0) {
cout << fixed << setprecision(2);
cout << name << " (" << no << ") $" << rate;
}
else
cout << "no data available";
}
Employee& Employee::operator+=(double r) {
if (no > 0 && r > 0.0)
rate += r;
return *this;
}
Employee operator+(const Employee& e, double r) {
Employee ee = e;
ee += r;
return ee;
}
Employee operator+(double r, const Employee& e) {
return e + r;
}
|
Your Task
Overload the insertion and extraction operators for the
Employee class. You may assume that the user will
always enter the data correctly and that the name entered will
contain no more than MAX_NAME characters.
Client Module
The following client program uses the upgraded Employee class is
// Custom I/O Operators
// h10.cpp
#include <iostream>
#include "Employee.h"
int main() {
Employee mark, rose;
std::cin >> mark;
std::cin >> rose;
std::cout << mark << std::endl;
mark = mark + 0.50; // raise of 50 cents
std::cout << mark << std::endl;
std::cout << rose << std::endl;
rose = 0.50 + rose; // raise of 50 cents
std::cout << rose << std::endl;
}
|
Employee number : 3456
Employee name : Mark J.
Hourly Pay Rate : 10.25
Employee number : 3457
Employee name : Rose
Hourly Pay Rate : 10.70
Mark J. (3456) $10.25
Mark J. (3456) $10.75
Rose (3457) $10.70
Rose (3457) $11.20
|
Note that the name of an employee may contain whitespace.
Employee Module
Employee.h
Add the following to the header file for your upgraded class:
- include the iostream header file
- add std::ostream& to the parameter list of the display() function
- add the prototype for an overloaded extraction operator
- add the prototype for an overloaded insertion operator
// Custom I/O Operators
// Employee.h
const double MIN_WAGE = 10.25;
const int MAX_NAME = 20;
class Employee {
int no;
char name[MAX_NAME + 1];
double rate;
public:
Employee();
Employee(int, const char*);
Employee(int, const char*, double);
void display( ) const;
Employee& operator+=(double);
};
Employee operator+(const Employee&, double);
Employee operator+(double, const Employee&);
|
Employee.cpp
Add your overloaded operator definitions to the implementation file.
To accept input with embedded whiltespace inside your definition of the
extraction operator, use the get()
or getline() member functions on the input stream object.
// Custom I/O Operators
// Employee.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
#include "Employee.h"
Employee::Employee() {
// safe empty state
no = 0;
name[0] = '\0';
rate = MIN_WAGE;
}
Employee::Employee(int n, const char* nm) {
Employee temp(n, nm, MIN_WAGE);
*this = temp;
}
Employee::Employee(int n, const char* nm, double r) {
if (n > 0 && nm[0] != '\0' && r >= MIN_WAGE) {
no = n;
std::strncpy(name, nm, MAX_NAME); // copy first MAX_NAME characters
name[MAX_NAME] = '\0'; // ensure that last byte is null
rate = r;
} else {
Employee temp;
*this = temp;
}
}
void Employee::display(std::ostream& os) const {
if (no > 0) {
os << std::fixed << std::setprecision(2);
os << name << " (" << no << ") $" << rate;
}
else
os << "no data available";
}
Employee& Employee::operator+=(double r) {
if (no > 0 && r > 0.0)
rate += r;
return *this;
}
Employee operator+(const Employee& e, double r) {
Employee ee = e;
ee += r;
return ee;
}
Employee operator+(double r, const Employee& e) {
return e + r;
}
|
|
|
|