C++ Overloading

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 as defined. Also, write a program to test various operations on this class.

Revisit the clockType class (.h and .cpp). Overload the operators such as +, ++, <<, -, --, ==, !=, <, <=, >, >=. Test your operators in a main program.

I am extremely confused. I've read and re-read the chapter on overloading in my textbook and I still can't wrap my head around what exactly I'm supposed to be doing. I don't want anyone doing my homework for me,as that defeats the purpose, but any kind of input to help me better understand what I should be doing would be amazing. I appreciate your time. Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
HEADER FILE 
 #ifndef DAY_TYPE_H
#define DAY_TYPE_H
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/******* Class dayType Specification ********/
class dayType
{
	
	private:
		string days[7];
		string currentDay;
		int numDays;
	public:
		void setDay(string newDay);
		void printDay() const;
		int showDay(int day);
		int nextDay(int day);
		int prevDay(int day) const;
		int calcDay(int day, int numDays);
				
		dayType()
		{
			days[0] = "Sunday";
			days[1] = "Monday";
			days[2] = "Tuesday";
			days[3] = "Wednesday";
			days[4] = "Thursday";
			days[5] = "Friday";
			days[6] = "Saturday";
			currentDay = days[0];
			numDays = 0;
		};
		~dayType();
	
};
#endif

IMPLEMENTATION FILE 
#include "dayType.h"
void dayType::setDay(string newDay)
{
   currentDay = newDay;
}
void dayType::printDay()
{
   cout << "Day chosen is " << currentDay << endl;
}
int dayType::showDay(int day)
{
   return day;
}
int dayType::nextDay(int day)
{
	day = day++;
	if (day > 6)
		day = day % 7;
	switch (day)
	{
	case 0: cout << "The next day is Sunday";
		break;
	case 1: cout << "The next day is Monday";
		break;
	case 2: cout << "The next day is Tuesday";
		break;
	case 3: cout << "The next day is Wednesday";
		break;
	case 4: cout << "The next day is Thursday";
		break;
	case 5: cout << "The next day is Friday";
		break;
	case 6: cout << "The next day is Saturday";
		break;
	}
	cout << endl;
	return day;
}
	
int dayType::prevDay(int day)
{
	day = day--;
	switch (day)
	{
	case -1: cout << "The previous day is Saturday.";
		break;
	case 0: cout << "The previous day is Saturday.";
		break;
	case 1: cout << "The previous day is Saturday.";
		break;
	case 2: cout << "The previous day is Saturday.";
		break;
	case 3: cout << "The previous day is Saturday.";
		break;
	case 4: cout << "The previous day is Saturday.";
		break;
	case 5: cout << "The previous day is Saturday.";
		break;
	default: cout << "The previous day is Saturday.";
	}
	cout << endl;
	return day;
}
int dayType::calcDay(int addDays, int numDays)
{
	addDays = addDays + numDays;
	if (addDays > 6)
		addDays = addDays % 7;
	switch(addDays)
	{
	case 0: cout << "The calculated day is Sunday.";
		break;
	case 1: cout << "The calculated day is Monday.";
		break;
	case 2: cout << "The calculated day is Tuesday.";
		break;
	case 3: cout << "The calculated day is Wednesday.";
		break;
	case 4: cout << "The calculated day is Thursday.";
		break;
	case 5: cout << "The calculated day is Friday.";
		break;
	case 6: cout << "The calculated day is Saturday.";
		break;
	default: cout << "Not valid choice.";
	}
	cout << endl;
	return addDays;
}

MAIN/TESTING PROGRAM
#include <iostream>  
#include <string>  
#include "dayType.h"    
using namespace std;  
void showSelections();
int main()  
{  
	dayType userDay;
	int currentDay; 
	int addDays;
	int test;
	string day;
	
	do
	{
		test = 0;
		showSelections();
		cin >> currentDay;
		cout << endl;
		if (currentDay == 0)
		{
			userDay.setDay("Sunday");
		}
		else if (currentDay == 1)
		{
			userDay.setDay("Monday");
		}
		else if (currentDay == 2)
		{
			userDay.setDay("Tuesday");
		}
		else if (currentDay == 3)
		{
			userDay.setDay("Wednesday");
		}
		else if (currentDay == 4)
		{
			userDay.setDay("Thursday");
		}
		else if (currentDay == 5)
		{
			userDay.setDay("Friday");
		}
		else if (currentDay == 6)
		{
			userDay.setDay("Saturday");
		}
		else if (currentDay == 9)
		{
			cout << "Exiting...";
			return 0;
		}
		else
		{
			cout << "Not a valid choice." << endl;
			test = -37;
		}
	}
	
	while (test < 0);
	userDay.printDay();
	cout << endl;
	userDay.showDay(currentDay);
	cout << endl;
	userDay.nextDay(currentDay);
	cout << endl;
	userDay.prevDay(currentDay);
	cout << endl;
	cout << "Enter the number of days to add: " << endl;
	cin >> addDays;
	cout << endl;
	userDay.calcDay(currentDay, addDays);
	cout << endl;
	cout << endl << endl;
	system("pause");
	return 0;
}
	
// Function to display weekday selections.
void showSelections()
{
	cout << "*****Please enter a day of the week*****" << endl;
	cout << "0 for Sunday" << endl;
	cout << "1 for Monday" << endl;
	cout << "2 for Tuesday" << endl;
	cout << "3 for Wednesday" << endl;
	cout << "4 for Thursday" << endl;
	cout << "5 for Friday" << endl;
	cout << "6 for Saturday" << endl;
	cout << "9 to exit" << endl;
}
Hi :)

i think you just have problem in overloading operators, so i skip other part of program and just talk about point:

overloading operators, is a way to ask an operator to do what you want to do with your user-defined data type.
for example in your class, operator '++' can be a way to change current day to next day in easier way, or operator '--' can be a way to change current day to prev. day easier.

+, ++, <<, -, --, ==, !=, <, <=, >, >=.

i said about '++' and '--', but about other operators, these are my ideas:

operator '+' : you can use it to summation 2 different dayType objects, for example if object d1, is set on "Monday" and object d2 is set on "Tuesday", and then, user use d1+d2, it should return "Friday", because, in your program, "Monday" is 2nd block of your array, and "Tuesday" is your 4th block of your array, and 2+4 is 6 and "Friday" is 6th of your array. its true in real world too(i was just explain how you can do it in your program).

operator '-' : you can overload it as '+' operator, but be careful about numbers which are lower than zero.

operator '==': you can compare currentDay of object1 and object2, if they was same, return true, else, return false.

operator '!=": its opposite of '==' operator.

operator '<': compare currentDay of object1 and object2, if object1.currentDay > object2.currentDay return false, else return true and so on... .

and about >> and << operators, you can use them as '++' or '--' or any thing you want.

if you have any problems to code them, feel free to ask ;)

--------
Be Best in Your Life,
S.Amir Mohammad Hassanli.
Topic archived. No new replies allowed.