Implement class Day which represents a day of the week. Overload operator+=, ope
ID: 3776071 • Letter: I
Question
Implement class Day which represents a day of the week. Overload operator+=, operator-=, operator++ (prefix), and operator-- (prefix), and operator<< as described below. use c++ language only. use only #include <iostream> library.
int main()
{
Day today = Day::WEDNESDAY;
std::cout << today << std::endl; // "Wednesday"
Day generic_day; // default constructor-- default day is Sunday
std::cout << generic_day << std::endl; // "Sunday"
++generic_day;
std::cout << generic_day << std::endl; // "Monday"
today += 5;
std::cout << today << std::endl; // "Monday"
today -= 1;
std::cout << today << std::endl; // "Sunday"
return 0;
}
Explanation / Answer
#include<iostream>
class day
{
int x;
char week_day[10];
public:
day()
{
int i;
for(i=1;i<=7;i++)
{
std::cin>>week_day[i];
}
x=3; // random vaue taken should be b/w 1 to 7 only
}
void getdata()
{
std::cout<<" "<<week_day[x];
}
void operator ++()
{
x=x+1;
}
void operator --()
{
x=x-1;
}
};
int main()
{
day s1,s2,s3; //obj created
s1.getdata(); // as x=3 so wed will get printed
s1--; // x=x-1=> 2
s1.getdata(); // tues will get printed
s1++; //x=x+1 2+1=3
s1++; // x=x+1 3+1=4
s1.getdata(); // thrus
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.