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 questions below: A) Suppose ns1 and ns2

ID: 3604524 • Letter: U

Question

Use the following program to answer the questions below:

A)

Suppose ns1 and ns2 are two namespaces. If the function void myFunction() is defined in both namespaces, what directive or format must be added to code below so that a specific myFunction() call is done? (hint: there may be more than one acceptable format)

int main()

{

...

myFunction(); // call the ns1 version

...

myFunction(); //call the ns2 version

...

}

B) Briefly explain why we would typically place a name in the following namespaces:

A. the Global namespace

B. the unnamed namespace

C) Line 30 of the program does in fact work. But what's going on? Comment on why someone might reasonably think that it wouldn't work. What do you think is going on to make it work? We haven't really learned why, but we can make some very reasonable guesses.

D) What is a specific advantages of using namespaces when building applications that are compiled from separate sources from different developers or development organizations?

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)

int main() {

ns1::myFunction(); // call ns1 version

ns2::myFunction(); // call ns2 version

}

B)

The global namespace: We define The main function in the global namepsace, so that anybody can call it from anywhere without the need of using a particular namespace. We mainly put things in global namespace so that we can access them from anywhere..

The unnames namespace is just like a protected namespace, where we don't explicitly give it a name, but the functions defined inside this are only accessible within that compilation unit(source file). So if we include a header file containing the same function, it will not give conflicts.. We can freely use our function in the same file without using a namespace.

C)

Line 30: testOne = testOne + 30;

The above line basically uses the + operator overloaded method in class declaration of Date.cpp. Here It basically adds 30 days to the current date and then assign the new object back to the original reference.

D)

In case if one developer is using a header file where some function foo() is contained without namespace, and if the developer also defines a function foo() for his own use in its own file, then it will result into a compile time error, as the call looks ambiguous to the compier and hence it will report.. To resove the situation, if function foo is defined in its own namespace, then it basically discriminates the use of foo function from header file or use of foo function from this source file.. And compilation of code just executes perfectly.