ok line 8 is void daytype :: setdays is wrong...what is correct Write the defini
ID: 3651427 • Letter: O
Question
ok line 8 is void daytype :: setdaysis wrong...what is correct
Write the definition of the 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 preform 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.
#include <string>
#include <iostream>
using namespace std;
class dayType
{
public:
void dayType::setDays()
{
days[0] = "Sun";
days[1] = "Mon";
days[2] = "Tues";
days[3] = "Wed";
days[4] = "Thur";
days[5] = "Fri";
days[6] = "Sat";
}
dayType::dayType(string d)
{day=d;
setdays();
}
string dayType::prev()
{
int i;
for(i=0;i<7; i++)
if(day==days[i])
if(i==0)
return days[6];
else
return days[i-1];
}
void dayType::add(int n)
{
int i;
for(i=0;i<7;i++)
if(day==days[i])
{
day=days[(i+n)%7];
return;
}
}
void dayType::print()
{
cout<<day<<endl;
}
string dayType::next()
{
int i;
for(i=0;i<7; i++)
if(day==days[i])
return days[(i+1)%7];
}
void dayType::set(string d)
{
day=d;
}
string dayType::get()
{
return day;
}
private:
string day,days[7];
};
int main()
{
dayType a("Sunday");
dayType b("Tuesday");
a.print();
b.print();
cout<<a.next()<< endl;
cout<<a.prev()<<endl;
a.add(8);
a.print();
system("pause");
return 0;
}
Explanation / Answer
try this code... #include #include #include using namespace std; class dayType { public: dayType() : _num(0) { } dayType(int dayNum) : _num(abs(dayNum % 7)) { } void set(int n) { _num = abs(n % 7); } string add(int n) const { return dayNum2Str(_num + n); } string name() const { return dayNum2Str(_num); } int num() const { return _num; } string next() const { return dayNum2Str(_num + 1); } string prev() const { return dayNum2Str((_num == 0) ? 6 : _num - 1); } private: int _num; static const string dayStr[7]; string dayNum2Str(int n) const { return dayStr[abs(n % 7)]; } }; const string dayType::dayStr[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; ostream &operatorRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.