Increment and Decrement Operator Overloding

Hi,
i have my submission in 8 hours and the code is almost complete. I asked one of my fellow friends they said that there is an issue with increment and i don't understand how to make an overloaded pre and post decrement operators.
please help me out my course grade depends on it
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include<iostream>
#include<string>

using namespace std;

class date
{
private:
  int month;
  string month[13] = { " ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  int day;
  int year;
public:
  static int months[];
  static int dayn[];
  date() :day(0), month(0), year(0)  {}

  bool leapyear(int y)
  {
    if (y % 4 == 0)
    {
      if (y % 100 == 0)
      {
        if (y % 400 == 0)
        {
          return true;
        }
        else
        {
          return false;
        }
      }
      else
      {
        return true;
      }
    }
    else
    {
      return false;
    }
  }
  date(int d, int m, int y) : day(d), month(m), year(y)
  {
    if (leapyear(year))
    {
      dayn[2] = 29;
      cout << "Leap year " << endl;
    }
    else
    {
      dayn[2] = 28;
      cout << "Not a Leap year " << endl;
    }
    while (d > dayn[months[m]] || m > 12)
    {
      cout << "Day of month does not exist, re-enter day : " << endl;
      cin >> d >> m >> y;
    }
    day = d; month = m; year = y;
  }

  date operator++()
  {
    date unreal;
    unreal.year = year;
    unreal.month = month;
    unreal.day = day;
    if (dayn[months[month]] == day)
    {
      if (unreal.month == 12)
      {
        unreal.year = ++year;  unreal.month = month = 1; unreal.day = day = 1;
      }
      else if (unreal.month != 12)
      {
        unreal.month = ++month; unreal.day = day = 1;
      }
    }
    else
    {
      unreal.day = ++day;
    }
    return unreal;
  }

  date operator++(int)
  {
    date unreal;
    unreal.year = year;
    unreal.month = month;
    unreal.day = day;
    if (dayn[months[month]] == day)
    {
      if (unreal.month == 12)
      {
        year++;  month = 1; day = 1;
      }
      else if (unreal.month != 12)
      {
        month++;  day = 1;
      }
    }
    else
    {
      day++;
    }
    return unreal;
  }

  date operator--()
  {
    date unreal;
    unreal.year = year;
    unreal.month = month;
    unreal.day = day;
    if (months[month] == month)
    {
      if (day == 1 && month == 1)
      {
        unreal.day = day = dayn[months[12]]; unreal.month = month = 12; unreal.year = --year;
      }
      else if (day == 1)
      {
        unreal.day = day = dayn[months[month - 1]]; unreal.month = --month;
      }
      else
      {
        unreal.day = --day;
      }
    }
    return unreal;
  }

  date operator--(int)
  {
    date unreal;
    unreal.year = year;
    unreal.month = month;
    unreal.day = day;
    if (months[month] == month)
    {
      if (day == 1 && month == 1)
      {
        day = dayn[months[12]]; month = 12; --year;
      }
      else if (day == 1)
      {
        day = dayn[months[month - 1]]; --month;
      }
      else
      {
        --day;
      }
    }
    return unreal;
  }

  date operator-(date &d3)// 1-4-2019 d1  -   3-2-2020 d3
  {
    date unreal;//d3.day,d3.month,d3.year
    int days = 0, dd;
    if (year == d3.year && month == d3.month && day == d3.day)
    {
      cout << "0" << endl;
    }
    else
    {
      int i;
      for (i = 0; i < months[month]; i++)
      {
        days += dayn[months[i]];
      }
      /*dd = days - (dayn[months[month]] - day);*/ cout /*<< dd << endl */<< days << endl;//62,91
    }
    return unreal;
  }
  int date::months[13] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
  int date::dayn[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  bool operator<(date &d3) //d1<d2
  {
    if (year<d3.year)
    {
      return true;
    }
    else if (year == d3.year && month<d3.month)
    {
      return true;
    }
    else if (year == d3.year && month == d3.month && day<d3.day)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  bool operator>(date &d3) //d1>d2
  {
    if (year>d3.year)
    {
      return true;
    }
    else if (year == d3.year && month>d3.month)
    {
      return true;
    }
    else if (year == d3.year && month == d3.month && day>d3.day)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  bool operator<=(date &d3)
  {
    if ((year < d3.year) || (year == d3.year && month == d3.month && day == d3.day))
    {
      return true;
    }
    else if (year == d3.year && month<d3.month)
    {
      return true;
    }
    else if (year == d3.year && month == d3.month && day<d3.day)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  bool operator>=(date &d3)
  {
    if (year > d3.year || (year == d3.year && month == d3.month && day == d3.day))
    {
      return true;
    }
    else if (year == d3.year && month>d3.month)
    {
      return true;
    }
    else if (year == d3.year && month == d3.month && day>d3.day)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  bool operator==(date &d3)
  {
    if (year == d3.year && month == d3.month && day == d3.day)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  bool operator!=(date &d3)
  {
    if (year!=d3.year)
    {
      return true;
    }
    else if (year == d3.year && month!=d3.month)
    {
      return true;
    }
    else if (year == d3.year && month == d3.month && day!=d3.day)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  friend ostream& operator <<(ostream &d, date &obj);
  friend istream& operator >>(istream &d, date &obj);
};
int date::months[13] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int date::dayn[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
ostream& operator <<(ostream &d, date &obj)
{
  d << obj.day << " " << obj.month << " " << obj.year;
  return d;
}
istream& operator >>(istream &d, date &obj)
{
  d >> obj.day >> obj.month >> obj.year;
  while (obj.day > obj.dayn[obj.months[obj.month]] || obj.month > 12)
  {
    cout << "Wrong day or month entered, re-enter date : " << endl;
    d >> obj.day >> obj.month >> obj.year;
  }
  return d;
}
int main()
{
  date d1, d2(1, 4, 2019), d3, d4(3, 2, 2020);
  d2 - d4;
  cout << (d2 < d4) << endl;//1-4-2019 d1  -   3-2-2020
  cout << (d2 > d4) << endl;
  cout << (d2 <= d4) << endl;
  cout << (d2 >= d4) << endl;
  cout << (d2 == d4) << endl;
  cout << (d2 != d4) << endl;
  cin >> d1;
  cout << d1;
  d1 = ++d2;  cout << d1 << endl << d2 << endl;
  d1 = d4++;  cout << d1 << endl << d2 << endl;
  d3 = ++d4;  cout << d3 << endl << d4 << endl;
  d1 = --d2;  cout << d1 << endl << d2 << endl;
  d1 = d4--;  cout << d1 << endl << d2 << endl;
  d3 = --d4;  cout << d3 << endl << d4 << endl;

  return 0;
}
Last edited on
Your increment and decrement ops are supposed to modify the 'this' object, not a separate object.

I don't see the point of the months array since it seems to just contain its index (months[0]==0, months[1]==1, etc).

Since dayn is static, there's only one copy for all date objects. You are modifying February's day count for a leapyear date, but that will modify it for all dates. It might be best to not modify dayn and handle month==2 as a separate case when necessary.

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
// leapyear should be static in date:
  static bool leapyear(int y) { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); }

// this is written assuming dayn[2] is always 28

// dayn[month], plus one if it's Feb and a leap year.
int days_in_month(int month, int year)
  { return date::dayn[month] + (month == 2 && date::leapyear(year)); }

date& date::operator++()
{
  if (++day > days_in_month(month, year))
  {
    day = 1;
    if (++month > 12)
    {
      month = 1;
      ++year;
    }
  }
  return *this;
}

date date::operator++(int)
{
  date cpy = *this; // copy original value
  operator++();     // call pre-increment  (you can also write ++*this;)
  return cpy;       // return original value
}


Your relational ops can all be done in terms of == and <, and are usually made non-member functions. (std::tie is in <tuple> and creates a tuple out of its arguments, which can then be compared lexicographically, like words in a dictionary, except that the letters are the elements in the tuple.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Inside the class:
//  friend bool operator==(const date& lhs, const date& rhs);
//  friend bool operator<(const date& lhs, const date& rhs);

// Outside the class:
inline bool operator==(const date& lhs, const date& rhs)
  { return tie(lhs.year, lhs.month, lhs.day)
        == tie(rhs.year, rhs.month, rhs.day); }
inline bool operator!=(const date& lhs, const date& rhs)
  { return !(lhs == rhs); }
inline bool operator<(const date& lhs, const date& rhs)
  { return tie(lhs.year, lhs.month, lhs.day)
         < tie(rhs.year, rhs.month, rhs.day); }
inline bool operator> (const date& lhs, const date& rhs)
  { return rhs < lhs; }
inline bool operator<=(const date& lhs, const date& rhs)
  { return !(lhs > rhs); }
inline bool operator>=(const date& lhs, const date& rhs)
  { return !(lhs < rhs); }

Last edited on
Topic archived. No new replies allowed.