# 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->add(65); // add 65 minutes working time

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

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->add(65); // add 65 minutes working time

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

Expert's answer

#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) {}

void add(int additionAmount) {

minutes += additionAmount;

}

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;

}

};

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) {}

void add(int additionAmount) {

minutes += additionAmount;

}

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 orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment