OOPortal OOPortal





Operator Overloading  «Prev 

Overloading Unary Operator - Exercise

Adding overloaded unary operators in C++

Objective: Add postfix decrement and increment operators to the clock class that subtract a second and add a second, respectively.

Background

The postfix operators ++ and -- can be overloaded distinctly from their prefix meanings. Postfix can be distinguished by defining the postfix overloaded function as having a single unused integer argument, as in

class T {
public:
//postfix invoked as t.operator++(0);
clock operator++(int);
clock  operator--(int);
};

There will be no implied semantic relationship between the postfix and prefix forms.

Instructions

Add postfix decrement and increment to class clock. Have them subtract a second and add a second, respectively.
Write these operators to use a default integer argument n that will be subtracted or added as an additional argument. For example:
clock c(60);
   c++;                 //adds a second
   c--;                 //subtracts a second
   c.operator++(5);     //adds 1 + 5 seconds
   c.operator--(5);     //subtracts 6 seconds

The code for the clock class is available in a file named clock.cpp, which can be found in the compressed course download file available on the Resources page.
Paste the class below and click the Submit button when you are ready to submit this exercise.