Question
USE PROGRAM BELOW TO ANSWER THE FOLLOWING: A) List the functions, both public and private, that are member functions of the Date class? B) What
USE PROGRAM BELOW TO ANSWER THE FOLLOWING:
A)List the functions, both public and private, that are member functions of the Date class?
B)What lines in Date.h prevent compilation error if a source file had multiple #include "Date.h" statements? (hint: we can define a class just once in a source file)
C)Which lines in Date.h are declaring functions that are not member functions of the Date class?
D)When overloading the << operator, why is the keyword const used in declaring the second parameter? (hint: compare line 68 and line 67 in Date.h)
E)Which lines of code in the class Date definition in Date.h is declaring overloaded operators?
F)Why or why not is the setDate() function an example of a mutator function.
1. //---------------------------------------------------------------------------
2. // CSC160 - Two.cpp
3. //---------------------------------------------------------------------------
4. #include
5. //
6. #include "Date.h"
7. #include "DateException.h"
8. using namespace std;
9. using namespace Two;
10. int main()
11. {
12. try
13. {
14. Date testOne(2, 27, 2012), testTwo, testThree;
15. Date oneWeek(7), oneDay(1);
16. for (int count = 0; count < 5; count++)
17. {
18. try
19. {
20. cout << " The starting date is " << testOne;
21. testTwo = testOne + oneWeek;
22. cout << " One week later is : " << testTwo;
23. testThree = testTwo - oneDay;
24. cout << " One day before that is : " << testThree;
25. }
26. catch (DateException e)
27. {
28. cout << "Exception: " << e.errorMessage() << endl;
29. }
30. testOne = testOne + 30;
31. }
32. }
33. catch (DateException e)
34. {
35. cout << "Exception: " << e.errorMessage() << endl;
36. }
37. catch (int e)
38. {
39. cout << "Exception: " << e << endl;
40. }
41. catch (...)
42. {
43. cout << "Exception Encountered" << endl;
44. }
45. cout << endl;
46. system("PAUSE");
47. return 0;
48. }
49. //---------------------------------------------------------------------------
50. // CSC160 - Date.h
51. //---------------------------------------------------------------------------
52.
53. using namespace std;
54. namespace Two
55. {
56. #ifndef DATE_H
57. #define DATE_H
58. class Date
59. {
60. public:
61. Date();
62. Date(int numberOfDays);
63. Date(int month, int day, int year);
64. void setDate(int month, int day, int year);
65. friend Date operator +(const Date& first, const Date& second);
66. friend Date operator -(const Date& first, const Date& second);
67. friend istream& operator >>(istream& in, Date& thisDate);
68. friend ostream& operator <<(ostream& out, const Date& thisDate);
69. private:
70. double internalDate;
71. double numDays(int month, int day, int year);
72. void getDetails(int& month, int& day, int& year);
73. bool isLeapYear(int year);
74. };
75. #endif
76. }
77. //---------------------------------------------------------------------------
78. // CSC160- Date.cpp
79. //---------------------------------------------------------------------------
80.
81. #include
82. #include "Date.h"
83. #include "DateException.h"
84. using namespace std;
85. namespace Two
86. {
87. Date::Date()
88. {
89. internalDate = 0.0;
90. }
91. Date::Date(int m, int d, int y)
92. {
93. internalDate = numDays(m, d, y);
94. }
95. Date::Date(int days)
96. {
97. internalDate = static_cast
98. }
99. void Date::setDate(int month, int day, int year)
100. {
101. if (year > 0)
102. if (month >= 1 && month <= 12)
103. if (day >= 1)
104. {
105. switch (month)
106. {
107. case 2:
108. if (isLeapYear(year))
109. if (day > 29)
110. throw DateException("Invalid Day Value: " + day);
111. else
112. if (day > 28)
113. throw DateException("Invalid Day Value: " + day);
114. break;
115. case 4:
116. case 6:
117. case 9:
118. case 11:
119. if (day > 30)
120. throw DateException("Invalid Day Value: " + day);
121. break;
122. default:
123. if (day > 31)
124. throw DateException("Invalid Day Value: " + day);
125. }
126. }
127. else
128. throw DateException("Invalid Day Value: " + day);
129. else
130. throw DateException("Invalid Month Value: " + month);
131. else
132. throw DateException("Invalid Year Value: " + year);
133. internalDate = numDays(month, day, year);
134. }
135.
136. double Date::numDays(int month, int day, int year)
137. {
Code for numDays not shown to save space: you can assume that it works and does not throw any exceptions
138. return dayCount;
139. }
140. void Date::getDetails(int& month, int& day, int& year)
141. {
Code for getDetails not shown to save space: you can assume that it works and does not throw any exceptions
142. }
143.
144. bool Date::isLeapYear(int year)
145. {
Code for isLeapYear not shown to save space: you can assume that it works and does not throw any exceptions
146. Date operator +(const Date& first, const Date& second)
147. {
148. Date temp;
149. temp.internalDate = first.internalDate + second.internalDate;
150. return temp;
151. }
152. Date operator -(const Date& first, const Date& second)
153. {
154. Date temp;
155. temp.internalDate = first.internalDate - second.internalDate;
156. return temp;
157. }
158. istream& operator >>(istream& in, Date& thisDate)
159. {
160. int month, day, year;
161. char temp;
162. in >> month >> temp >> day >> temp >> year;
163. thisDate.internalDate = thisDate.numDays(month, day, year);
164. return in;
165. }
166. ostream& operator <<(ostream& out, const Date& thisDate)
167. {
168. int month, day, year;
169. thisDate.getDetails(month, day, year);
170. out << month << '/' << day << '/' << year;
171. return out;
172. }
173. //---------------------------------------------------------------------------
174. // CSC160 - DateException.h
175. //---------------------------------------------------------------------------
176. #ifndef DATEEXCEPTION_H
177. #define DATEEXCEPTION_H
178. #include
179. using namespace std;
180. namespace Two
181. {
182. class DateException
183. {
184. public:
185. DateException();
186. DateException(string thisErrorMessage);
187. string errorMessage();
188. private:
189. string message;
190. };
191. }
192. #endif
193. //---------------------------------------------------------------------------
194. // CSC160 - DateException.cpp
195. //---------------------------------------------------------------------------
196. #include
197. #include "DateException.h"
198. using namespace std;
199. namespace Two
200. {
201. DateException::DateException()
202. {
203. message = "Error in Date Occurred: No Additional Information";
204. }
205. DateException::DateException(string thisErrorMessage)
206. {
207. message = "Date Exception" + thisErrorMessage;
208. }
209. string DateException::errorMessage()
210. {
211. return message;
212. }
213. }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started