1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#include <iostream>
#include <string>
using namespace std;
class DayOfTheWeek
{
private:
string day;
public:
void setDay(string currentDay);
string getDay() const;
void printDay() const;
string plusOneDay();
string minusOneDay();
string addDays(int daysahead);
};
void DayOfTheWeek::setDay(string currentDay)
{ day = currentDay;}
string DayOfTheWeek::getDay() const
{return day;}
void DayOfTheWeek::printDay() const
{ }
string DayOfTheWeek::plusOneDay()
{
string dayarray[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int indexofday;
for(int i = 0; i < 7; i++)
{
if (day == dayarray[i])
{
indexofday = i;
break;
}
}
return dayarray[ (indexofday + 1) % 7];
}
string DayOfTheWeek::minusOneDay()
{
string dayarray[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int indexofday;
for( int i = 0; i < 7; i++)
{
if (day == dayarray[i])
{
indexofday = i;
break;
}
}
if(indexofday = 0)
return dayarray[6];
else
return dayarray[ (indexofday - 1) % 7];
}
string DayOfTheWeek::addDays(int daysahead)
{
string dayarray[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int indexofday;
for(int i = 0; i < 7; i++)
{
if (day == dayarray[i])
{
indexofday = i;
break;
}
}
return dayarray[ (indexofday + daysahead) % 7];
}
int main()
{
DayOfTheWeek monday;
monday.setDay("Monday");
string currentDay = monday.getDay();
monday.printDay();
cout <<"The value of the monday object is "<<monday.getDay()<<"."<<endl;
cout <<"The day after monday object is "<<monday.plusOneDay()<<"."<<endl;
cout <<"The day before monday object is "<<monday.minusOneDay()<<"."<<endl;
cout <<"Monday + 3 = "<<monday.addDays(3)<<"."<<endl;
cout <<"The value of the monday object is still "<<monday.getDay()<<"."<<endl;
cout <<"Monday - 3 = "<<monday.addDays(4)<<"."<<endl;
cin.ignore();
return 0;
}
|