Design and implement a class dayType that implements the day of the week in a pr
ID: 3877838 • 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:
Step a: Set the day.
Step b: Print the day.
Step c: Return the day.
Step d: Return the next day.
Step e: Return the previous day.
Step f: 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.
Step g: Add the appropriate constructors.
Write the definitions of the functions to implement the operations for the class dayType.
dayType.cpp
#include <iostream>
#include <string>
#include "dayType.h"
using namespace std;
// array for days of the week
// part b
void dayType::print() const { }
// part d
string dayType::nextDay() const { }
// part e
string dayType::prevDay() const { }
// part f
void dayType::addDay(int nDays) { }
// part a
void dayType::setDay(string d) { }
// part c
string dayType::getDay() const { }
// part g
dayType::dayType() { }
// part g
dayType::dayType(string d) { }
dayType.h
#ifndef H_dayType
#define H_dayType
#include <string>
using namespace std;
class dayType
{
public:
static string weekDays[7];
void print() const;
string nextDay() const;
string prevDay() const;
void addDay(int nDays);
void setDay(string d);
string getDay() const;
dayType();
dayType(string d);
private:
string weekDay;
};
#endif
main.cpp
#include <iostream>
#include <string>
#include "dayType.h"
using namespace std;
int main()
{
dayType myDay("Monday");
dayType temp("Sunday");
myDay.print();
cout << endl;
cout << myDay.prevDay() << endl;
cout << myDay.nextDay() << endl;
temp.print();
cout << endl;
cout << temp.prevDay() << endl;
cout << temp.nextDay() << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include "dayType.h"
using namespace std;
// array for days of the week
// part b
void dayType::print() const {cout<<weekDay<<endl; }
// part d
string dayType::nextDay() const {
if(weekDay[0]==weekDays[0][0])
cout<<weekDays[1]<<endl;
else if(weekDay[0]==weekDays[1][0])
cout<<weekDays[2]<<endl;
else if(weekDay[0]==weekDays[2][0])
cout<<weekDays[3]<<endl;
else if(weekDay[0]==weekDays[3][0])
cout<<weekDays[4]<<endl;
else if(weekDay[0]==weekDays[4][0])
cout<<weekDays[5]<<endl;
else if(weekDay[0]==weekDays[5][0])
cout<<weekDays[6]<<endl;
else if(weekDay[0]==weekDays[6][0])
cout<<weekDays[0]<<endl;
}
// part e
string dayType::prevDay() const {
if(weekDay[0]==weekDays[0][0])
cout<<weekDays[6]<<endl;
else if(weekDay[0]==weekDays[1][0])
cout<<weekDays[0]<<endl;
else if(weekDay[0]==weekDays[2][0])
cout<<weekDays[1]<<endl;
else if(weekDay[0]==weekDays[3][0])
cout<<weekDays[2]<<endl;
else if(weekDay[0]==weekDays[4][0])
cout<<weekDays[3]<<endl;
else if(weekDay[0]==weekDays[5][0])
cout<<weekDays[4]<<endl;
else if(weekDay[0]==weekDays[6][0])
cout<<weekDays[5]<<endl;
}
// part f
void dayType::addDay(int nDays) {
int n;
if(weekDay[0]==weekDays[0][0])
n=0;
else if(weekDay[0]==weekDays[1][0])
n=1;
else if(weekDay[0]==weekDays[2][0])
n=2;
else if(weekDay[0]==weekDays[3][0])
n=3;
else if(weekDay[0]==weekDays[4][0])
n=4;
else if(weekDay[0]==weekDays[5][0])
n=5;
else if(weekDay[0]==weekDays[6][0])
n=6;
n = (n + nDays)%7;
cout<<weekDays[n]<<endl;
}
// part a
void dayType::setDay(string d) { weekDay=d;}
// part c
string dayType::getDay() const { return weekDay; }
// part g
dayType::dayType() {weekDays[0]="Monday";
weekDays[1]="Tueday";
weekDays[2]="Wednesday";
weekDays[3]="Thursday";
weekDays[4]="Friday";
weekDays[5]="Saturday";
weekDays[6]="Sunday"; }
// part g
dayType::dayType(string d) {weekDay=d; weekDays[0]="Monday";
weekDays[1]="Tueday";
weekDays[2]="Wednesday";
weekDays[3]="Thursday";
weekDays[4]="Friday";
weekDays[5]="Saturday";
weekDays[6]="Sunday";}
using namespace std;
int main()
{
dayType myDay("Monday");
dayType temp("Sunday");
myDay.print();
cout << endl;
cout << myDay.prevDay() << endl;
cout << myDay.nextDay() << endl;
temp.print();
cout << endl;
cout << temp.prevDay() << endl;
cout << temp.nextDay() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.