I am having some difficulty determining how to solve a part of a problem I am doing for my C++ course. This part requires the use of operator-overloading to correctly increment the days and the month. Maybe I am overlooking some very simple math, but somehow I am a bit confused as to how. Here is the code I have for my overloaded prefix and postfix operators for DayOfYear
objects:
//Operators
DayOfYear operator++ () {return day++; }
DayOfYear operator++ (int) {return ++day;}
DayOfYear operator-- () {return day--;}
DayOfYear operator-- (int) {return --day;}
Obviously, this is redundant because day
is an int
so it only does what I could already do with ++
and --
to begin with. Below is the code for the DayOfYear
class:
class DayOfYear {
std::string month[12] = {"January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December"};
//Each month's number of days, respectively
int numDaysMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day;
//Convert day # to a string value
static std::string dayToStr;
public:
//First constructor
DayOfYear (int day) {this -> day = day;}
//Second Constructor
DayOfYear (std::string mth, int dayOfMonth) {
//Make sure the dayOfMonth is valid
if (dayOfMonth > 31 || dayOfMonth < 1) {
if (mth == "February" && dayOfMonth > 28) {
std::cout << "Invalid day. February is assumed to be 28 days for our
purposes." << std::endl;
} else if ((mth == "April" || mth == "June" || mth == "September" || mth
== "November") && dayOfMonth > 30) {
std::cout << "Invalid day. " << mth << " has only 30 days." <<
std::endl;
} else {
std::cout << "Invalid day. The day should be >0 and <=31 for most
months." << std::endl;
}
} else {
day = dayOfMonth;
}
}
//Accessors
std::string getMonth (int mID) {return month[mID];}
std::string getDay () {return dayToStr;}
//Mutators
void setDay (int d) {
day = d;
}
//Operators
DayOfYear operator++ () {return day++; }
DayOfYear operator++ (int) {return ++day;}
DayOfYear operator-- () {return day--;}
DayOfYear operator-- (int) {return --day;}
void print () {
//TO-DO
}
};
Right now, the code won't compile because the pieces aren't all there. However, I can read code fairly well I think, so I know my operators won't work. Somehow, I need to figure out how to make this code so that my main
function (below) can compile and run correctly:
int main () {
DayOfYear d (2);
d.print ();
d = 365;
d.print ();
d = 32;
d.print ();
d++;
d.print ();
d = 365;
++d;
d.print ();
d = 1;
--d;
d.print ();
d = 32;
d--;
d.print ();
DayOfYear e ("December", 31);
e.print ();
return 0;
}
Specifically, as per the prof's specifications for this lab, I need to
-
--prefix and postfix-- operators should modify the
DayOfYear
object(s) so that it represents the previous day. If the day is already the first day of the year, the new value of the object will represent the last day of the year. -
++prefix and postfix++ increment operators. These operators should modify the DayOfYear object so that it represents the next day. If the day is already the end of the year, the new value of the object will represent the first day of the year.
So, I think my solution will require some use of %
. Maybe I could overload it? I'm not sure. %
can be overloaded according to the textbook.
I've been reading the textbook and I think I know the basics of operator overloading in C++, but I think I am missing something simple or subtle. I've tried to look for something specific to this, but all I find is general information on operator overloading with structs and classes. I'm not sure what to do at the moment. I think my print ()
method is gonna be a challenge too, but the solution to this should make that last part of the assignment much easier.
Any guidance or assistance would be appreciated! Thanks.
Aucun commentaire:
Enregistrer un commentaire