Object Orientated Time Classes for Scheduler
Solution
NumDays.cpp
// NumDays class implementation for task 4.
#include “NumDays.h”
NumDays::NumDays() : hours(0)
{
update();
}
NumDays::NumDays(constint hours) : hours(hours)
{
update();
}
// overloaded operations
NumDays operator+(constNumDays&l, constNumDays&r)
{
returnNumDays(l.hours + r.hours);
}
NumDays operator-(constNumDays&l, constNumDays&r)
{
returnNumDays(l.hours – r.hours);
}
// increase or decrease hours
NumDays&NumDays::operator++()
{
hours ++;
update();
return *this;
}
NumDaysNumDays::operator++(int)
{
NumDays old = *this;
hours ++;
update();
return old;
}
NumDays&NumDays::operator–()
{
hours –;
update();
return *this;
}
NumDaysNumDays::operator–(int)
{
NumDays old = *this;
hours –;
update();
return old;
}
voidNumDays::setHours(constint hours)
{
this->hours = hours;
update();
}
voidNumDays::setDays(const double days)
{
this->days = days;
this->hours = (int)(this->days * 8);
update();
}
intNumDays::getHours() const
{
return hours;
}
doubleNumDays::getDays() const
{
return days;
}
voidNumDays::update()
{
days = hours / 8.0;
}
NumDays.h
// NumDays class defination for task 4.
#ifndef NUMDAYS_H_
#define NUMDAYS_H_
classNumDays
{
public:
NumDays();
NumDays(constint hours);
// overloaded operations
friendNumDays operator+(constNumDays&l, constNumDays&r);
friendNumDays operator-(constNumDays&l, constNumDays&r);
// increase or decrease hours
NumDays&operator++(); // prefix
NumDays operator++(int); // postfix
NumDays&operator–(); // prefix
NumDays operator–(int); // postfix
// getters and setters
voidsetHours(constint hours);
voidsetDays(const double days);
intgetHours() const;
doublegetDays() const;
private:
int hours;
double days; // store the days = hours / 8.0
void update(); // when the hours changed, update the days.
};
#endif
report.cpp
// Implementation for task 6
#include “TimeOff.h”
#include
using namespace std;
int main() {
string name, identifier;
int month;
cout<< “Enter name: “;
cin>> name;
cout<< “Enter identifier: “;
cin>> identifier;
cout<< “Enter the number of worked months: “;
cin>> month;
cout<
// now use TimeOff class to calculate the maximun number
// of sick leave and vacation days.
TimeOffto(name, identifier);
to.setMaxSickHours(month * 8);
to.setMaxVacationHours(month * 12);
// display the result
cout<< “——————” <
<< “Name: “<
<< “ID: ” <
<< “Max Sick Leave Days: “<
<< “Max Vacation Leave Days: “<
return 0;
}
TimeOff.cpp
// TimeOff class implementation for task 5.
#include “TimeOff.h”
#include “NumDays.h”
TimeOff::TimeOff()
{
}
TimeOff::TimeOff(const string &name, const string &identifier)
: name(name), identifier(identifier)
{
}
// setters and getters
voidTimeOff::setMaxSickHours(constint hours)
{
maxSickDays.setHours(hours);
}
voidTimeOff::setMaxVacationHours(constint hours)
{
maxVacation.setHours(hours);
}
voidTimeOff::setMaxUnpaid(constint hours)
{
maxUnpaid.setHours(hours);
}
voidTimeOff::setSickTakenHours(constint hours)
{
sickTaken.setHours(hours);
}
voidTimeOff::setVacationTakenHours(constint hours)
{
vacTaken.setHours(hours);
}
voidTimeOff::setUnpaidTakenHours(constint hours)
{
unpaidTaken.setHours(hours);
}
NumDays&TimeOff::getMaxSickDays()
{
returnmaxSickDays;
}
NumDays&TimeOff::getMaxVacationDays()
{
returnmaxVacation;
}
NumDays&TimeOff::getMaxUnpaidDays()
{
returnmaxUnpaid;
}
NumDays&TimeOff::getSickTakenDays()
{
returnsickTaken;
}
NumDays&TimeOff::getVacationTakenDays()
{
returnvacTaken;
}
NumDays&TimeOff::getUnpaidTakenDays()
{
returnunpaidTaken;
}
voidTimeOff::setName(const string &name)
{
this->name = name;
}
voidTimeOff::setID(const string &identifier)
{
this->identifier = identifier;
}
stringTimeOff::getName() const
{
return name;
}
stringTimeOff::getID() const
{
return identifier;
}
TimeOff.h
// TimeOff class declaration for task 5.
#ifndef TIMEOFF_H_
#define TIMEOFF_H_
#include “NumDays.h”
#include
usingstd::string;
classTimeOff
{
public:
TimeOff();
TimeOff(const string &name, const string &identifier);
// setters and getters
voidsetMaxSickHours(constint hours);
voidsetMaxVacationHours(constint hours);
voidsetMaxUnpaid(constint hours);
voidsetSickTakenHours(constint hours);
voidsetVacationTakenHours(constint hours);
voidsetUnpaidTakenHours(constint hours);
NumDays&getMaxSickDays();
NumDays&getMaxVacationDays();
NumDays&getMaxUnpaidDays();
NumDays&getSickTakenDays();
NumDays&getVacationTakenDays();
NumDays&getUnpaidTakenDays();
voidsetName(const string &name);
voidsetID(const string &identifier);
stringgetName() const;
stringgetID() const;
private:
NumDaysmaxSickDays;
NumDayssickTaken;
NumDaysmaxVacation;
NumDaysvacTaken;
NumDaysmaxUnpaid;
NumDaysunpaidTaken;
string name;
string identifier;
};
#endif