Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the following program to answer the following questions: A) The the names of

ID: 3604521 • Letter: U

Question

Use the following program to answer the following questions:

A) The the names of the functions in Date.cpp that return an object reference as the return value of the function?

B) How many private member variables are defined in each object of type class Date type?

C) What are two specific advantages of compiling classes separately from the application programs that use those classes?

D) Why would a developer choose to implement a data type implemented as a struct versus a data type implemented as a class?

E) Why is it a better idea to throw and catch exceptions using an exceptions class that we program, rather than throwing and catching exceptions using a simple data type like int?

1. //---------------------------------------------------------------------------

2. // CSC160 - Two.cpp

3. //---------------------------------------------------------------------------

4. #include <iostream>

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 <iostream>

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<double>(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 <string>

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 <string>

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. }

Explanation / Answer

A) The functions that return an object reference in Date.cpp are:  istream& operator >>(istream& in, Date& thisDate) and ostream& operator <<(ostream& out, const Date& thisDate).

Note that functions Date operator +(const Date& first, const Date& second) and Date operator -(const Date& first, const Date& second) return objects but not object reference.

B)Each object of the date class contains a single private member variable which is internalDate. We can see it in the conatructors. All of the constructors initialize the value of a single variable which is internalDate. So it is the only private memner that the class has.

C)The two advantages of compiling classes separately than the ones using them are as follows: i >We can separately write and compile a code and can reuse it by linking it in any number of classes afterwards. We do not need to again write and recompile the entore code every time we use it at any place. ii > For making any changes , we can change the source code and recompile it. We do not need to make the change at every place we use that code. We just need to ensure that all the places where the code is used now are linked to the changed version

D) A structure is a value type whereas the class is a reference type. So a structure is allocated on the stack and not contained within a reference type and thus access to a structure and its data is faster than access to a reference type in the heap. Also , the cleaning up of the memory allocated to a structure on the stack requires a simple change of the address to which the stack pointer points. This is fast as compared to allowing the garbage collector to automatically clean up reference type in the managed heap. Thus we should uses structs when  instances of the type are small and commonly short-lived or are commonly embedded in other objects.

E)The advantages of using exception classes are as follows: i > It helps us to separate the error handling code that is put into the exception classes from the regular code. ii > Exception classes has the ablity to propagate errors reporting up the call stack of methods. iii > As all the exceptions thrown in the program are objects, the grouping and categorizing of exceptions become easier.