Write the progrsm in c++ Do the following problems. For assignment submission, u
ID: 3829792 • Letter: W
Question
Write the progrsm in c++
Do the following problems. For assignment submission, use the format listed in the syllabus. Use comments in your code (3 points will be deducted from each problem missing comments) 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: a. Set the day. b. Print the day. c. Return the day. d. Return the next day. e. Return the previous day. 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. g. 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
Please find the required program and output below: Please find the comments against each line for the description:
#include <iostream>
#include <string>
using namespace std;
class dayType {
private:
string day;
string days[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
public:
dayType() { //constructor
day = "Sun";
}
void setDay(string d){ //set the day
day = d;
}
void printDay(){ //print the day
cout << "The day is : " << day <<endl;
}
string returnDay(){ //return the current day
return day;
}
string nextDay(){ //get the next day
int newidx = (getDayIndex()+1) % 7;
return days[newidx];
}
string previousDay(){ //get the Previous day
int newidx = getDayIndex()-1;
if(newidx < 0)
newidx = 7 - (-1 * newidx);
return days[newidx];
}
string addDay(int d){ //add days and return the resultant day
int newidx = (getDayIndex()+d) % 7;
return days[newidx];
}
int getDayIndex() //get the index of current day
{
string d = returnDay();
for (int i = 0; i < 7; ++i)
{
if (d.compare(days[i]) == 0) return i;
}
return -1;
}
};
int main( ) {
dayType day;
day.setDay("Sat");
day.printDay();
cout << "Next day = " << day.nextDay() << endl;
cout << "Previous day = " << day.previousDay() << endl;
cout << "after 2 days = " << day.addDay(2) << endl;
return 0;
}
--------------------------------------------------------------------------
OUTPUT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.