also having a problem with inheritance

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2005
Posts: 2
Reputation: tommouse is an unknown quantity at this point 
Solved Threads: 0
tommouse tommouse is offline Offline
Newbie Poster

also having a problem with inheritance

 
0
  #1
Oct 11th, 2005
Code compiles w/o error but print function does not pull data from base class. I have looked over the chapter numerous times. This is a long one, sorry

  1.  
  2. class extClockType: public clockType
  3. {
  4. public:
  5. void getTimeZone(int& timeZone);
  6. void setTimeZone(int timeZone);
  7. void printTime() const;
  8. extClockType();
  9. extClockType(int hours, int minutes, int seconds, int timeZone);
  10. private:
  11. int tz;
  12. };
  13.  
  14.  
  15. #include <iostream>
  16. #include "clockType.h"
  17. #include "extClockType.h"
  18.  
  19. using namespace std;
  20.  
  21.  
  22. void extClockType::setTimeZone(int timeZone)
  23. {
  24. if(tz <= 12 && tz >= -12)
  25.  
  26. tz = timeZone;
  27.  
  28. else
  29.  
  30. tz = 0;
  31. }
  32.  
  33. void extClockType::getTimeZone(int& timeZone)
  34. {
  35. timeZone = tz;
  36. }
  37.  
  38. void extClockType::printTime() const
  39. {
  40. clockType::printTime();
  41. cout << " timezone is: ";
  42. if(tz >= 0)
  43. cout << "GMT+" << tz;
  44. else
  45. cout <<"GMT" << tz;
  46. }
  47.  
  48. extClockType::extClockType(int hours, int minutes, int seconds, int timeZone): clockType(hours, minutes, seconds)
  49. {
  50. if(tz <= 12 && tz >= -12)
  51.  
  52. tz = timeZone;
  53.  
  54. else
  55.  
  56. tz = 0;
  57. }
  58.  
  59. extClockType::extClockType()
  60. {
  61. tz = 0;
  62. }
  63.  
  64. class clockType
  65. {
  66. public:
  67. void setTime(int hours, int minutes, int seconds);
  68. void getTime(int& hours, int& minutes, int& seconds) const;
  69. void printTime() const;
  70. void incrementSeconds();
  71. void incrementMinutes();
  72. void incrementHours();
  73. bool equalTime(const clockType& otherClock) const;
  74. clockType(int hours, int minutes, int seconds);
  75. clockType();
  76. private:
  77. int hr;
  78. int min;
  79. int sec;
  80. };
  81.  
  82. #include <iostream>
  83. #include "clockType.h"
  84.  
  85. using namespace std;
  86.  
  87. void clockType::setTime(int hours, int minutes, int seconds)
  88. {
  89. if (0 <= hours && hours < 24)
  90. hr = hours;
  91. else
  92. hr = 0;
  93.  
  94. if (0 <= minutes && minutes < 60)
  95. min = minutes;
  96. else
  97. min = 0;
  98.  
  99. if (0 <= seconds && seconds < 60)
  100. sec = seconds;
  101. else
  102. sec = 0;
  103. }
  104.  
  105. void clockType::getTime(int& hours, int& minutes, int& seconds) const
  106. {
  107. hours = hr;
  108. minutes = min;
  109. seconds = sec;
  110. }
  111.  
  112. void clockType::incrementHours()
  113. {
  114. hr++;
  115. if(hr > 23)
  116. hr = 0;
  117. }
  118.  
  119. void clockType::incrementMinutes()
  120. {
  121. min++;
  122. if (min > 59)
  123. {
  124. min = 0;
  125. incrementHours();
  126. }
  127. }
  128.  
  129. void clockType::incrementSeconds()
  130. {
  131. sec++;
  132.  
  133. if (sec > 59)
  134. {
  135. sec = 0;
  136. incrementMinutes();
  137. }
  138. }
  139.  
  140. void clockType::printTime() const
  141. {
  142. if (hr < 10)
  143. cout << "0";
  144. cout << hr << ":";
  145.  
  146. if (min < 10)
  147. cout << "0";
  148. cout << min << ":";
  149.  
  150. if (sec < 10)
  151. cout << "0";
  152. cout << sec;
  153. }
  154.  
  155. bool clockType::equalTime(const clockType& otherClock) const
  156. {
  157. return (hr == otherClock.hr
  158. && min == otherClock.min
  159. && sec == otherClock.sec);
  160. }
  161.  
  162. clockType::clockType(int hours, int minutes, int seconds)
  163. {
  164. if (0 <= hours && hours < 24)
  165. hr = hours;
  166. else
  167. hr = 0;
  168.  
  169. if (0 <= minutes && minutes < 60)
  170. min = minutes;
  171. else
  172. min = 0;
  173.  
  174. if (0 <= seconds && seconds < 60)
  175. sec = seconds;
  176. else
  177. sec = 0;
  178. }
  179.  
  180. clockType::clockType()
  181. {
  182. hr = 0;
  183. min = 0;
  184. sec = 0;
  185. }
  186.  
  187. #include <iostream>
  188. #include "clockType.h"
  189. #include "extClockType.h"
  190.  
  191. using namespace std;
  192.  
  193. int main()
  194. {
  195. int hours;
  196. int minutes;
  197. int seconds;
  198. int timeZone;
  199.  
  200. clockType clockOne;
  201. clockType clockTwo;
  202.  
  203. extClockType tzClock;
  204. extClockType tzClock2;
  205.  
  206. clockOne.setTime(9, 30, 26);
  207. tzClock.setTimeZone(8);
  208. cout << "Clock One's time is currently: ";
  209. tzClock.printTime();
  210. clockOne.printTime();
  211. cout << endl;
  212.  
  213. clockTwo.setTime(9, 30, 28);
  214. tzClock2.setTimeZone(7);
  215. cout << "Clock Two's time is currently: ";
  216. tzClock2.printTime();
  217. cout << endl;
  218.  
  219. if (clockOne.equalTime(clockTwo))
  220. cout << "Both times are equal."
  221. << endl;
  222. else
  223. {
  224. cout << "Both times are not equal."
  225. << endl;
  226. cout << "Please, enter the current time"
  227. << "(hrs, mins, secs, and timezone): ";
  228. cin >> hours >> minutes >> seconds >> timeZone;
  229.  
  230. clockOne.setTime(hours, minutes, seconds);
  231. tzClock.setTimeZone(timeZone);
  232. cout << "The time is has been set to: ";
  233. tzClock.printTime();
  234. cout << endl;
  235. }
  236.  
  237. clockOne.incrementSeconds();
  238. cout << "After incrementing the time by one second: ";
  239. tzClock.printTime();
  240. cout << endl;
  241.  
  242. clockOne.getTime(hours, minutes, seconds);
  243. tzClock.getTimeZone(timeZone);
  244.  
  245. cout << "hours = " << hours
  246. << ", minutes = " << minutes
  247. << ", seconds = " << seconds << endl;
  248.  
  249. return 0;
  250. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 2
Reputation: tommouse is an unknown quantity at this point 
Solved Threads: 0
tommouse tommouse is offline Offline
Newbie Poster

Re: also having a problem with inheritance

 
0
  #2
Oct 12th, 2005
will someone please help with this problem
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC