-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateType.cpp
More file actions
141 lines (126 loc) · 3.02 KB
/
dateType.cpp
File metadata and controls
141 lines (126 loc) · 3.02 KB
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
#include "dateType.h"
// Constructor
dateType::dateType(int month, int day, int year)
{
// Initialize the member variables with the provided values
dMonth = month;
dDay = day;
dYear = year;
}
// Function to check if a year is a leap year
bool dateType::isLeapYear() const
{
if ((dYear % 4 == 0 && dYear % 100 != 0) || (dYear % 400 == 0))
return true;
else
return false;
}
// Overload the prefix increment operator
dateType& dateType::operator++()
{
// Increment the date by one day
dDay++;
if (dMonth == 2 && isLeapYear())
{
if (dDay > 29)
{
dMonth++;
dDay = 1;
}
}
else if ((dMonth == 4 || dMonth == 6 || dMonth == 9 || dMonth == 11) && dDay > 30)
{
dMonth++;
dDay = 1;
}
else if (dDay > 31)
{
dMonth++;
dDay = 1;
if (dMonth > 12)
{
dYear++;
dMonth = 1;
}
}
return *this;
}
// Overload the postfix increment operator
dateType dateType::operator++(int)
{
dateType temp = *this; // Create a temporary copy
++(*this); // Use the prefix increment operator
return temp; // Return the original value
}
// Overload the prefix decrement operator
dateType& dateType::operator--()
{
// Decrement the date by one day
dDay--;
if (dDay < 1)
{
dMonth--;
if (dMonth < 1)
{
dYear--;
dMonth = 12;
}
if (dMonth == 2 && isLeapYear())
dDay = 29;
else if (dMonth == 2)
dDay = 28;
else if (dMonth == 4 || dMonth == 6 || dMonth == 9 || dMonth == 11)
dDay = 30;
else
dDay = 31;
}
return *this;
}
// Overload the postfix decrement operator
dateType dateType::operator--(int)
{
dateType temp = *this; // Create a temporary copy
--(*this); // Use the prefix decrement operator
return temp; // Return the original value
}
// Overload the equality operator
bool dateType::operator==(const dateType& other) const
{
return (dYear == other.dYear && dMonth == other.dMonth && dDay == other.dDay);
}
// Overload the inequality operator
bool dateType::operator!=(const dateType& other) const
{
return !(*this == other);
}
// Overload the less than operator
bool dateType::operator<(const dateType& other) const
{
if (dYear < other.dYear)
return true;
else if (dYear == other.dYear)
{
if (dMonth < other.dMonth)
return true;
else if (dMonth == other.dMonth)
{
return dDay < other.dDay;
}
}
return false;
}
// Overload the less than or equal to operator
bool dateType::operator<=(const dateType& other) const
{
return (*this == other || *this < other);
}
// Overload the greater than operator
bool dateType::operator>(const dateType& other) const
{
return !(*this <= other);
}
// Overload the greater than or equal to operator
bool dateType::operator>=(const dateType& other) const
{
return !(*this < other);
}