I am doing a refresh in C++ after being out of it for 3.5 years. I am using Microsoft Visual Studio Express 2013. The practice project that I am working on is not displaying the day of the month correctly. I am using Date.h, Date.cpp, and fig10_08.cpp. After looking over the example in both my book and the example files that came with it, I could not locate any errors. Even the solution builder did not locate any errors. Could someone help me find the error plz? Thanks.
Date.h code:
// Fig. 10.6: Date.h
// Date class definition with overloaded increment operators.
#ifndef DATE_H
#define DATE_H
#include <array>
#include <iostream>
class Date
{
friend std::ostream &operator<<(std::ostream &, const Date &);
public:
Date(int m = 1, int d = 1, int y = 1900); // default constructor
void setDate(int, int, int); // set month, day, and year
Date &operator++(); // prefix increment operator
Date operator++(int); // postfix increment operator
Date &operator+=(unsigned int); // add days, modify object
static bool leapYear(int); // is date in a leap year?
bool endOfMonth(int) const; // is date at the end of month?
private:
unsigned int month;
unsigned int day;
unsigned int year;
static const std::array<unsigned int, 13> days; // days per month
void helpIncrement(); // utility function for incrementing date
};// end class Date
#endif
Date.cpp code:
// Fig. 10.7: Date.cpp
// Date class member- and friend-function definitions.
#include <iostream>
#include <string>
#include "Date.h"
using namespace std;
// Date constructor
Date::Date(int month, int day, int year)
{
setDate(month, day, year);
}// end Date constructor
// set month, day and year
void Date::setDate(int mm, int dd, int yy)
{
if (mm >= 1 && mm <= 12)
month = mm;
else
throw invalid_argument("Month must be 1-12");
if (yy >= 1900 && yy <= 2100)
year = yy;
else
throw invalid_argument("Year must be >= 1900 and <= 2100");
// test for a leap year
if ((month == 2 && leapYear(year) && dd >= 1 && dd <= 29) || (dd >= 1 && dd <= days[month]))
day == dd;
else
throw invalid_argument("Day is out of range for current month and year");
}// end function setDate
// overloaded prefix increment operator
Date &Date::operator++()
{
helpIncrement(); // increment date
return *this; // reference return to create an lvalue
}// end function operator++
// overloaded postfix increment operator; note that the
// dummy integer parameter does not have a parameter name
Date Date::operator++(int)
{
Date temp = *this; // hold current state of object helpIncrement();
helpIncrement();
// return unincremented, saved, temporary object
return temp; // value return; not a reference return
}// end function operator++
// add specified number of days to date
Date &Date::operator+=(unsigned int additionalDays)
{
for (int i = 0; i < additionalDays; ++i)
helpIncrement();
return *this; // enables cascading
}// end function operator +=
// if the year is a leap year, return true; otherwise, return false
bool Date::leapYear(int testYear)
{
if (testYear % 400 == 0 ||
(testYear % 100 != 0 && testYear % 4 == 0))
return true; // a leap year
else
return false; // a leap year
}// end function leapYear
// determine whether the day is the last day of the month
bool Date::endOfMonth(int testDay) const
{
if (month == 2 && leapYear(year))
return testDay == 29; // last day of Feb. in leap year
else
return testDay == days[month];
}// end function endOfMonth
// function to help increment the date
void Date::helpIncrement()
{
// day is not end of month
if (!endOfMonth(day))
++day; // increment day
else
if (month < 12) // day is end of month and month < 12
{
++month; // increment month
day = 1; // first day of new month
}// end if
else // last day of year
{
++year; // increment year
month = 1; // first month of new year
day = 1; // first day of new month
}// end else
}// end function helpIncrement
After looking over the example in both my book and the example files that came with it, I could not locate any errors. Even the solution builder did not locate any errors.
You should also pay attention to warnings.
warning C4553: '==' : operator has no effect; did you intend '='?