Looking at the main function, we can see that I've Hard Coded the "Monday" into my setDay public function. It is easy to grab a day of the week from the user using a c-string (as I did in setDay), but how would I ask the user to add n to the day that is set, "Monday" and come up with "Thursday"? It is hard because typdef enum { INVALID, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY} doesn't interpret 9 is 0 and/or 10 as 1.
#include <iostream>
using std::cout;
using std::endl;
class DayOfTheWeek //class is encapsulation of functions and members that manipulate the data.
{
public:
DayOfTheWeek(); // Constructor
virtual ~DayOfTheWeek(); // Destructor
void setDay(const char * day); // Function to set the day
void printDay() const; // Function to Print the day.
const char * getDay() const; // Function to get the day.
const char * plusOneDay(); // Next day function
const char * minusOneDay(); // Previous day function
const char * addDays(int addValue); // function that adds days based on parameter value
private:
char * day; // variable for the days of the week.
};
DayOfTheWeek::DayOfTheWeek() : day(0)
{
// Usually I would allocate pointer member variables
// Here in the construction of the Object
}
const char * DayOfTheWeek::getDay() const
{
return day; // we can get the day simply by returning it.
}
const char * DayOfTheWeek::minusOneDay()
{
if ( strcmp( day, "Monday" ) == 0)
{
cout << "The day before " << day << " is ";
return "Sunday";
}
else if ( strcmp( day, "Tuesday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Monday";
}
else if ( strcmp( day, "Wednesday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Tuesday";
}
else if ( strcmp( day, "Thursday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Wednesday";
}
else if ( strcmp( day, "Friday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Thursday";
}
else if ( strcmp( day, "Saturday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Friday";
}
else if ( strcmp( day, "Sunday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Saturday";
}
else
{
cout << "'" << day << "'";
return "is an invalid day of the week!";
}
}
const char * DayOfTheWeek::plusOneDay()
{
if ( strcmp( day, "Monday" ) == 0)
{
cout << "The day after " << day << " is ";
return "Tuesday";
}
else if ( strcmp( day, "Tuesday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Wednesday";
}
else if ( strcmp( day, "Wednesday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Thursday";
}
else if ( strcmp( day, "Thursday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Friday";
}
else if ( strcmp( day, "Friday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Saturday";
}
else if ( strcmp( day, "Saturday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Sunday";
}
else if ( strcmp( day, "Sunday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Monday";
}
else
{
cout << "'" << day << "'";
return " is an invalid day of the week!";
}
}
const char * DayOfTheWeek::addDays(int addValue)
{
if ( addValue < 0 )
{
if ( strcmp( day, "Monday" ) == 0)
{
cout << day << " - " << -addValue << " = ";
return "Friday";
}
else if ( strcmp( day, "Tuesday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Saturday";
}
else if ( strcmp( day, "Wednesday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Sunday";
}
else if ( strcmp( day, "Thursday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Monday";
}
else if ( strcmp( day, "Friday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Tuesday";
}
else if ( strcmp( day, "Saturday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Wednesday";
}
else if ( strcmp( day, "Sunday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Thursday";
}
else
{
cout << "'" << day << "' ";
return "is an invalid day of the week! ";
}
}
else // if our parameter is greater than 0 (positive)
{
if ( strcmp( day, "Monday" ) == 0)
{
cout << day << " + " << addValue << " = ";
return "Thursday";
}
else if ( strcmp( day, "Tuesday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Friday";
}
else if ( strcmp( day, "Wednesday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Saturday";
}
else if ( strcmp( day, "Thursday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Sunday";
}
else if ( strcmp( day, "Friday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Monday";
}
else if ( strcmp( day, "Saturday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Tuesday";
}
else if ( strcmp( day, "Sunday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Wednesday";
}
else
{
cout << "'" << day << "' ";
return "is an invalid day of the week! ";
}
}
}
void DayOfTheWeek::printDay() const
{
cout << "The Value of the " << day;
}
void DayOfTheWeek::setDay(const char * day)
{
if (day)
{// Here I am allocating the object member char day pointer
this->day = new char[strlen(day)+1];
size_t length = strlen(day)+1; // +1 for trailing null char
strcpy_s(this->day , length , day); // copying c-strings
}
else day = NULL; // If their was a problem with the parameter 'day'
}
DayOfTheWeek::~DayOfTheWeek()
{
delete day; // Free the memory allocated in SetDay
}
int main()
{
DayOfTheWeek MondayObject; // declare an object
MondayObject.setDay("Monday"); // Call our public function 'setDay' to set a day of the week
MondayObject.printDay(); // Call our public function 'printDay' to print the day we set
cout << " object is " << MondayObject.getDay() << endl; // Print the value of the object
cout << MondayObject.plusOneDay() << endl;
cout << MondayObject.minusOneDay() << endl;
cout << MondayObject.addDays(3) << endl;
MondayObject.printDay();
cout << " object is still " << MondayObject.getDay() << endl; // Print the value of the object
cout << MondayObject.addDays(-3) << endl;
return 0;
}