H lets implementation to go in timetypecpp

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: k at the TimeType.h Let's implementation, to go in TimeType.cpp. TimeType.cpp void TimeType::Set(int hrs, int mins, void TimeType::Set(int hrs, int int secs) { hours = hrs; minutes = mins; seconds = secs; } We just refer to the private data by name. Applied Programming Applied Programming Here's an example. TimeType time1, time2; if (time1.Equals(time2)) cout << "time1 equals time2\n"; This is like a binary operator (==) getting its left This operand from before the . and the second and operand from the argument. operand This is actually how operators are done, as we This shall soon see. shall Applied Programming This is just some newfangled way of data hiding. This Can't we do better? Can't Sure. We can ensure that the value of a Sure. TimeType is always valid. TimeType W hat if the hours to be set exceeds 23, or the What seconds is negative? seconds There are varying levels of protection, each There requiring differing levels of intervention. requiring Applied Programming Here's how. Precede the store with Level 1 if (secs > 59) // assume non-negative { mins += secs/60; secs %= 60; } if (mins > 59) // also here { hrs += mins/60; mins %= 60; } hrs %= 24; // and again Whatever i...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online