1,105,208 Community Members

Overloading Operators help.

Member Avatar
destroyer333
Newbie Poster
4 posts since May 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 
Member Avatar
rubberman
Senior Poster
3,986 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 500 [?]
Skill Endorsements: 87 [?]
 
0
 

Not wanting to go through all this code, I have one suggestion - use julian date values. There are standard expressions to take a julian date (either unsigned integer for y/m/d, or double float for y/m/d/h:m:s) and extract the day of the week, accomodating leap years, and even leap-seconds (if necessary).

Also this:

    if(day==911){
        cout << "The Program has Ended" << endl;
    }

should be this:

    if(day==911){
        cout << "The Program has Ended" << endl;
        return 0;
    }
Member Avatar
rubberman
Senior Poster
3,986 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 500 [?]
Skill Endorsements: 87 [?]
 
0
 

And FWIW, I have written numerous date processing functions over the years, in language ranging from BASIC to dBase II to C++ and Java date/time classes. They all properly dealt with leap years/seconds, days of week, etc. So, first convert the date to a Julian value. Then, the rest is simple math.

Member Avatar
destroyer333
Newbie Poster
4 posts since May 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I just want to keep it simple with just the month and day because there were no instructions to do the year or the leap year. The only problem i have is how to print the information from the postfix operator using the print function or something else.

Member Avatar
tinstaafl
Postaholic
2,009 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 400 [?]
Skill Endorsements: 35 [?]
 
1
 

Here's some code to look at. I changed things around a little. I used months to hold the month and day string for displaying. I used simplify() to set it, using a stringstream. I changed the setDay function to accommodate days greater than 365 and less than 1, so that it automatically rolls over and sets the months string with the month and day value for that day. I call that in the operators instead of having a different routine to do the same thing. Not sure if it's all 100% what you want, but it all works including the operators.

main.cpp

    #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);
            dYear.print();
            cout << endl;
            if(day==911)
            {
                cout << "The Program has Ended" << endl;
            }
            //Demonstrating overloaded prefix ++ operator
            cout << "Demonstrating overlaoded prefix ++ operator.\n";
            happy.setDay(day);
            ++happy;
            happy.print();
            cout << endl;
            //Demonstrating overloaded postfix ++ operator
            cout << "\nDemonstrating overloaded postfix ++ operator.\n";
            happy++;
            happy.print();
            cout << endl;
            //Demonstrating overloaded prefix -- operator
            cout << "Demonstrating overlaoded prefix -- operator.\n";
            --happy;
            happy.print();
            cout << endl;
            //Demonstrating overloaded postfix -- operator
            cout << "\nDemonstrating overloaded postfix -- operator.\n";
            happy--;
            happy.print();
            cout << endl;
        }
        while(day!=911);
        return 0;
    }
    //Send entered day number to the print() function
    void DayOfYear::print()
    {
        //Display month and day
        cout << months;
    };

DayOfYear.cpp

#include "DayOfYear.h"
#include <cmath>
#include <iostream>
#include <sstream>
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++()
{
    setDay(day + 1);
    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(*this);
    operator ++();  
    return temp;
}
//*******************************************************
// Overloaded prefix -- operator. *
//*******************************************************
DayOfYear DayOfYear::operator--()
{
    setDay(day - 1);
    simplify(); 
    return *this;
}
//*******************************************************
// Overloaded postfix -- operator. *
//*******************************************************
DayOfYear DayOfYear::operator--(int)
{
    DayOfYear temp(*this);
    operator --();  
    return temp;
}
void DayOfYear::simplify()
{
    stringstream month;
    //Incrementors
    for(int index=0; index<12; index++)
    {
        if(MonthDay[index] >= day)
        {
            if(index == 0)
                month << Month[index] + " " << day;
            else
                month << Month[index] + " " << day - MonthDay[index-1];
            months = month.str();
            break;
        }
    }
}

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();
    void simplify();
    void setDay(int d)
    {
        day = d;
        if(day > 365)
            day = day % 365;
        if(day < 1)
            day = day % 365 + 366;
        simplify();
    }
    int getDay(){return day;}
    DayOfYear()
    {
    day = 0;
    }
    DayOfYear(int d)
    {
    day = d;
    simplify();
    }
    // Overloaded operator function.
    DayOfYear operator++();
    DayOfYear operator++(int);
    DayOfYear operator--();
    DayOfYear operator--(int);
    };
    #endif
Member Avatar
tinstaafl
Postaholic
2,009 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 400 [?]
Skill Endorsements: 35 [?]
 
0
 

You're very welcome If your question is answered, please remember to mark this solved thanks.

Member Avatar
destroyer333
Newbie Poster
4 posts since May 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I just wanted to make sure the program worked before i mark the post.I was trying to do the method by myself with the guidance of your example and it seems to work perfectly, although I didn't make the change in the print and simplify function.(hopefully there isnt anything wrong) I did used the if statement for the setDay since thats needed for the --operator. Thanks again for the help.

Question Answered as of 11 Months Ago by rubberman and tinstaafl
Member Avatar
destroyer333
Newbie Poster
4 posts since May 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Actually had to changed the simplify function so that it gets the right month, and its way easier to read.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: