NumDaysClass.h
#ifndef NUM_DAYS_H_
#define NUM_DAYS_H_
class NumDays
{
private:
double workHours; // To hold a number of hours
public:
NumDays(double hours = 0.0)
{ workHours = hours; }
void setHours(double hours)
{ workHours = hours; }
void print();
double getHours() const
{ return workHours; }
double getDays() const
{ return workHours / 8; }
// Overloaded operator functions
NumDays operator + (const NumDays &); // Overloaded +
NumDays operator - (const NumDays &); // Overloaded -
NumDays operator ++(); // Overloaded Prefix ++
NumDays operator ++(int); // Overloaded Postfix ++
NumDays operator --(); // Overloaded Prefix --
NumDays operator --(int); // Overloaded Postfix --
};
#endif
NumDaysClass.cpp
#include "NumDays.h"
#include <iostream>
using std::cout;
/* **********************************************************
NumDays NumDays::operator +(const obj &)
********************************************************** */
NumDays NumDays::operator + (const NumDays &right)
{
NumDays temp;
temp.workHours = (workHours + right.workHours);
return temp;
}
/* **********************************************************
NumDays NumDays::operator -(const obj &)
********************************************************** */
NumDays NumDays::operator - (const NumDays &right)
{
NumDays temp;
if (workHours < right.workHours)
{
temp.workHours = (right.workHours - workHours);
}
else
{
temp.workHours = (workHours - right.workHours);
}
return temp;
}
/* **********************************************************
NumDays NumDays::operator ++() : Prefix ++
********************************************************** */
NumDays NumDays::operator ++()
{
return ++workHours;
}
/* **********************************************************
NumDays NumDays::operator ++(int) : Postfix ++
********************************************************** */
NumDays NumDays::operator ++(int)
{
return workHours++;
}
/* **********************************************************
NumDays NumDays::operator --() : Prefix --
********************************************************** */
NumDays NumDays::operator --()
{
return --workHours;
}
/* **********************************************************
NumDays NumDays::operator --(int) : Postfix --
********************************************************** */
NumDays NumDays::operator --(int)
{
return workHours--;
}
/* **********************************************************
NumDays::print()
Outputs the hour(s) and day(s) worked to screen.
********************************************************** */
void NumDays::print()
{
cout << "Total: " << (getHours()) << " hour(s) or "
<< getDays() << " day(s).\n\n";
}
NumDaysDm.cpp
#include "NumDays.h"
#include <iomanip>
using std::fixed;
using std::showpoint;
using std::setprecision;
#include <iostream>
using std::cin;
using std::cout;
int main()
{
NumDays thisWeek(26);
NumDays lastWeek(15);
NumDays total;
cout << "YAMAGATA 4th JUNIOR HIGH SCHOOL BIWEEKLY WORKSHEET SUMMARY\n\n";
// Demonstrating the overloaded postfix ++, -- operators
cout << fixed << showpoint << setprecision(2);
cout << "You worked " << thisWeek.getHours() << " hour(s) in total this week.\n";
cout << "This makes " << thisWeek.getDays() << " day(s) of work.\n\n";
cout << "We increased this amount by 1 hour\n";
thisWeek++;
thisWeek.print();
cout << "We detucted 1 hour from this amount\n";
thisWeek--;
thisWeek.print();
// Demonstrating the overloaded prefix ++, --, + and - operators
cout << "You worked " << lastWeek.getHours() << " hour(s) in total last week.\n";
cout << "This makes " << lastWeek.getDays() << " day(s) of work.\n\n";
cout << "We increased this amount by 1 hour\n";
++lastWeek;
lastWeek.print();
cout << "We detucted 1 hour from this amount\n";
--lastWeek;
lastWeek.print();
cout << "This week and last week you worked\n";
total = thisWeek + lastWeek;
total.print();
cout << "The difference in hours and days is\n";
total = thisWeek - lastWeek;
total.print();
cin.get();
cin.ignore();
return 0;
}
No comments:
Post a Comment