2

I am trying to make a program inside a class and add to the date one in each. So if the date was: 1/1/2014 I want it to be 2/2/2015.

I was able to figure out the part for the day and the month, however, for some reason I am getting a strange number for the year.

when I tried to debug the program I found out that it's printing the following

1/1/2014
1/1/2014
1/0/2014 // I am not sure why did it change the day to 0 but I don't care about this as I'm getting the correct result at the end
2/2/4028 // I am more concern about the 4028 ! I don't know from where did this come from
2/2/4028

Here is what I've done so far:

      #include "stdafx.h"
      #include <iostream>
      #include <iomanip>
      #include <string>
      using namespace std;

       class Date
     {

public:
    int day, year, monthnum;
    Date(int d=1, int m2 =1, int y= 2014)
    {
        monthnum = m2;
        day = d;
        year =y;
        cout << *this; // this is just for testing purposes
    }


    Date operator+(const Date&) const;


    friend ostream& operator << (ostream& out, const Date& date)
    {   
        out << date.monthnum << "/" << date.day << "/" << date.year <<endl;
        return out;
    }


};


Date Date:: operator+(const Date& date) const
{
    return Date(day+date.day,monthnum+ date.monthnum ,date.year+year); // I think there is something with the "date.year + year" because when I remove this I get my initialization of the year which is 2014, however, I need it to be 2015 when I add one to it.
}




void testprogram()
{
Date date1(1), date2(1), date3(0);
date3 = date1 + date2;
cout << date3 << endl;

}


int main()
{
    testprogram();
return 0;
}
rullzing
  • 622
  • 2
  • 10
  • 22
  • 3
    As an aside, creating your own date and time class is not easy, especially when it comes to handling leap years. Unless you are doing this for educational purposes I'd suggest that you find and learn how to use a decent 3rd part library (EG boost::Date_Time) – Peter M Jan 31 '14 at 21:19
  • Yes I am doing it for educational purposes. How do you suggest dealing with the leap years? – rullzing Feb 01 '14 at 08:14
  • I haven't studied up on it myself, but you could start with http://stackoverflow.com/questions/725098/leap-year-calculation There are a lot of "If this or that" type situations in date calculations. Especially of you go back far enough historically and encounter fundamental changes in the actual calender – Peter M Feb 01 '14 at 16:04

2 Answers2

5

Think carefully about what a Date represents, and what adding things to Dates would mean. A Date is a particular point in time. Adding them together would be like adding the latitude and longitude of Denver and Cleveland together and expecting the coordinate to mean something useful!

Your default parameters specify the year to be 2014, so when you add date1 and date2 you get date3.year = 2014 + 2014. I would caution you to avoid default parameters except in cases where the caller will almost always want the default. It's throwing you off for date3 as well, because you're specifying that day=0, monthnum=1, year=2014.

Steve Howard
  • 6,737
  • 1
  • 26
  • 37
  • Thank you very much. So I think to make it work as it should I should just add + instead of add year. – rullzing Jan 31 '14 at 21:26
2
return Date(day+date.day,monthnum+ date.monthnum ,date.year+year);

Here you are adding each year, month and day of date1 to date2. So 2014 + 2014 = 4028! if you're trying to add "1" to each part, write a function to return monthnum + 1, day + 1 and year + 1.

mhm
  • 313
  • 1
  • 5
  • 12
  • Thank you very much. So I think to make it work as it should I should just add + instead of add year. – rullzing Jan 31 '14 at 21:27