Design and implement a class dayType that implements the day of the week in a pr
ID: 3767214 • Letter: D
Question
Design and implement a class dayType that implements the day of the week in a program. The class dayType should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type dayType: Set the day. Print the day. Return the day. Return the next day. Return the previous day. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday. Add the appropriate constructors. Write the definitions of the functions to implement the operations for the class dayType. Also, write a program to test various operations on this class.Explanation / Answer
Program to design and implement a class dayType that implements the day of the week in a program:
#include <iostream>
#include "dayType.h"
using namespace std;
void dayType::printDay()
{
switch(weekDay)
{
case Sun:
cout<<"Sunday";
break;
case Mon:
cout<<"Monday";
break;
case Tue:
cout<<"Tuesday";
break;
case Wed:
cout<<"Wednesday";
break;
case Thu:
cout<<"Thursday";
break;
case Fri:
cout<<"Friday";
break;
case Sat:
cout<<"Saturday";
}
}
weekDayType dayType::nextDay()
{
return weekDayType((weekDay + 1) % 7);
}
weekDayType dayType::prevDay()
{
if (weekDay == Sun)
return Sat;
else
return weekDayType(weekDay - 1);
}
void dayType::addDay(int nDays)
{
weekDay = weekDayType((weekDay + nDays) % 7);
}
void dayType::setDay(weekDayType d)
{
weekDay = d;
}
weekDayType dayType::getDay()
{
return weekDay;
}
dayType::dayType()
{
weekDay = Sun;
}
dayType::dayType(weekDayType d)
{
weekDay = d;
}
Main program :
#include <iostream>
#include "dayType.h"
using namespace std;
int main()
{
dayType myDay(Thu);
dayType tempDay;
cout<<"Today: ";
myDay.printDay();
cout<<endl;
tempDay.setDay(myDay.nextDay());
cout<<"Next Day: ";
tempDay.printDay();
cout<<endl;
tempDay.setDay(myDay.prevDay());
cout<<"Previous Day: ";
tempDay.printDay();
cout<<endl;
myDay.addDay(7);
cout<<"After 7 Days: ";
myDay.printDay();
cout<<endl;
cout<<endl;
return 0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.