In-Class Exercise

Helper Operators


This exercise overloads a helper operator, enabling client programs to use objects in expressions that create new objects from the operands that don't change in value.


Given Information

The following code is a solution to the Handout on Member Operators.

Client Module

 // Member Operator
 // h8.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 += 0.50;       // raise of 50 cents 
     null.display();
     cout << endl;
     mark.display();
     cout << endl;
     mark += 0.50;       // raise of 50 cents 
     mark.display();
     cout << endl;
     rose.display();
     cout << endl;
     rose += 0.50;       // raise of 50 cents 
     rose.display();
     cout << endl;
 }

Employee Module

Employee.h

 // Member 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.cpp

 // Member 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;
 }

Your Task

Upgrade the Employee module to overload the + operator in two ways:

  • overload the + operator for an Employee object as the left operand and a double as the right operand
  • overload the + operator for a double as the left operand and an Employee object as the right operand

In each case, use an independent helper definition (non-friend).

Client Module

The client program that uses the upgraded class looks something like

 // 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

Add your prototype declarations after the class definition:

 // Helper Operator
 // 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.cpp

Add the helper operator definitions to the implementation file:

 // Helper Operator
 // 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;
 }














  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License