Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USE THE PROGRAM BELOW TO ANSWER THE FOLLOWING PLEASE: A)Which lines in the operator definitions at lines 146-172 create the need for the use the

USE THE PROGRAM BELOW TO ANSWER THE FOLLOWING PLEASE:

A)Which lines in the operator definitions at lines 146-172 create the need for the use the keyword friend in the corresponding operator declarations in class Date in Date.h?

B)If any, what is the line number of the function of class Date that is being called by main() at line 24 in Two.cpp?

C)How many objects of type Date are declared using the default constructor in main() in Two.cpp?

D)How many different constructor functions are called in Two.cpp?

E)Is the Date class an ADT or "abstract data type"? Explain why or why not.

F)Assuming that the variables testTwo and testThree are Date type objects, the statement:

cin >> testTwo >> testThree;

which function, if any, implemented in Date.cpp is invoked?

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(days);

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions