63 844
Assignments Done
99,2%
Successfully Done
In August 2018

# Answer to Question #58334 in C++ for jonney

Question #58334
Write a class Work whose objects represent working times (in whole minutes)
and salary rates (in whole cents per minute). With this class, the following operation shall
be possible:
Work* w = new Work(25, 60); // 25 cent/min, 60 min
w->printSalary(); // prints salary "31,25" (25*125 Cents)
bool okay = w->subtract(60); // attempts to subtract 60 minutes
// returns false, if not sufficient time
// available (time remains unchanged)
Work::reset(*w); // reset working time to zero
Work v(30); // 30 cent/min, 0 min
int r = w.compare(*v); // 0 if salaries of w and v are equal,
// 1, if w’s salary is bigger, -1, else
Work u(v); // u becomes a copy of v
#include <iostream>
using namespace std;

class Work {
protected:
int minutes;
int cents;

public:
Work(int cents, int minutes) : cents(cents), minutes(minutes) {}
Work(int cents) : cents(cents), minutes(0) {}

}

bool subtract(int subtractionAmount) {
if (subtractionAmount <= minutes)
minutes -= subtractionAmount;
return (subtractionAmount <= minutes);
}

void printSalary() {
cout << minutes * cents / 100.0;
}

int compare(Work instance) {
if (cents*minutes > instance.cents*instance.minutes)
return 1;
else if (cents*minutes < instance.cents*instance.minutes)
return -1;
else return 0;
}

static void reset(Work &instance) {
instance.minutes = 0;
}
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!