In-Class Exercise

Member Operators


This exercise overloads an operator to enable client programs to use objects as operands in expressions.


Given Information

Consider the main program shown on the left and the output shown on the right

Client Module

 // Employee
 // main.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;
     mark.display();
     cout << endl;
     rose.display();
     cout << endl;
 }











 no data available

 Mark (3456) $10.25

 Rose (3457) $10.70


Employee Module

Employee.h

 // Employee - Header File
 // 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.cpp

 // Employee - Implementation File
 // Employee.cpp

 #include <iostream>
 #include <iomanip>
 #include <cstring>
 using namespace std;
 #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 w) {
     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";
 }

Your Task

Upgrade the Employee module to overload the following operator:

  • += operator increases the wage rate of the current object by the amount of the right operand and returns a reference to the current object

Client Module

The client program that uses the upgraded class looks something like

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











 no data available


 no data available

 Mark (3456) $10.25


 Mark (3456) $10.75 

 Rose (3457) $10.70


 Rose (3457) $11.20 


Employee Module

Employee.h

Add the prototype declaration to the class definition:

 // Employee - Header File
 // 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.cpp

Add your operator definition to the implementation file:

 // Employee - Implementation File
 // 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";
 }














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