So i believe i got my prefix working, now i'm trying to get my postfix working so that the class object happy should have its day increased by 1. Not sure whats wrong. Maybe the way it receives the month and day in the constructor?
main
#include <iostream>
#include <string>
#include "DayOfYear.h"
using namespace std;
void print(int);
int main()
{
DayOfYear dYear, happy;
int day;
string month;
do{
cout << "\nEnter the day number and Enter 911 to stop the program";
cout << endl << "Day Entered: ";
cin >> day;
dYear.setDay(day);
if(day==911){
cout << "The Program has Ended" << endl;
}
//++dYear;
//dYear.print(day);
//Demonstrating overloaded prefix ++ operator
cout << "Demonstrating overlaoded prefix ++ operator.\n";
++happy;
cout<< happy.getDay();
happy.print(day);
//Demonstrating overloaded postfix ++ operator
cout << "\nDemonstrating overloaded postfix ++ operator.\n";
happy++;
cout<< happy.getDay();
happy.print(day);
}while(day!=911);
return 0;
}
//Send entered day number to the print() function
void DayOfYear::print(int day)
{
int index = 0;
while (DayOfYear::MonthDay[index] < day)
index = (index + 1) %12;
//Display month and day
cout << DayOfYear::Month[index] << " " << day - DayOfYear::MonthDay[index-1];
};
DayOfYear.cpp
#include "DayOfYear.h"
#include <cmath>
#include <iostream>
using namespace std;
//Set days of each month into an array
const int DayOfYear::MonthDay[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
//Set the name of each month into an array
const string DayOfYear::Month[] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
//************************************************
// Overloaded prefix ++ operator. Causes the *
// inched member to be incremented. Returns the *
// incremented object. *
//************************************************
DayOfYear DayOfYear::operator++()
{
++day;
simplify();
return *this;
}
//*************************************************
// Overloaded postfix ++ operator. Causes the *
// inches member to be incremented. Returns the *
// value of the object before the increment. *
//*************************************************
DayOfYear DayOfYear::operator++(int)
{
DayOfYear temp(months, day);
day++;
simplify();
//cout<<day;
return temp;
}
//*******************************************************
// Overloaded postfix -- operator. *
//*******************************************************
DayOfYear DayOfYear::operator--(int)
{
string month;
DayOfYear temp(month, day);
day--;
if (day == -1) // <-------------- fix to ... if (day ==-1)
{ day = day + 365; }
return temp;
}
void DayOfYear::simplify()
{
string month;
//Incrementors
for(int index=0;index<12;index++){
month=DayOfYear::Month[index];
if (month== "April" ||month== "June" || month=="September" ||month== "November" && day > 30)
{
month += (day / 30);
day = (day % 30);
}
else if ( month =="January"||month=="March"||month=="May" ||month=="July" ||month=="August" ||month=="October" && day > 31)
{
month += (day / 31);
day = day % 31;
}
else if ( month == "Feburary" && day>28 )
{
month += (day / 28);
day = day % 28;
}
else if (month == "December" && day > 31)
{
month = (day / 31);
day = day % 31;
}
}
}
/*
//Decrementors
else if (month== "April" ||month== "June" || month=="September" ||month== "November" && day < 1)
{
month -= ((abs(day) / 30) + 1);
day = day - (abs(day) % 30);
}
else if (month=="March"||month=="May" ||month=="July" ||month=="August" ||month=="October"||month == "December" && day < 1)
{
month -= ((abs(day) / 31) +1);
day = 31 - (abs(day) % 31);
}
else if (month == "Feburary" && day < 1)
{
month -= ((abs(day) / 28) +1);
day = 28 - (abs(day) % 28);
}
else if (month =="January" && day < 1)
{
month -= ((abs(day) / 31) + 1);
day = 31 - (abs(day) % 31);
}
}
}
*/
DayOfYear.h
#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H
#include <string>
using namespace std;
class DayOfYear
{
private:
int day;
string months;
static const string Month[12]; //static member variables
static const int MonthDay[12];
public:
void print(int);
void simplify();
void setDay(int d){day = d;}
int getDay(){return day;}
DayOfYear()
{
day = 0;
}
DayOfYear(int d)
{
day = d;
}
DayOfYear (string m,int d)
{
months=m;
day= d;
simplify();
}
// Overloaded operator function.
DayOfYear operator++();
DayOfYear operator++(int);
DayOfYear operator--(int);
};
#endif