Chapter2 - Chapter 2 ADT Basics Imagine that were dealing...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 2 – ADT Basics Imagine that we’re dealing with an application (e.g. banking) which requires a lot of date manipulation. Required operations include working out the number of days between two dates, determining whether a particular date falls on a weekend, finding the date “n” days after a given date, and do on. We’ll begin by considering a program which calculates maturity dates for T-bills. The user enters the date of issue and the number of days to maturity. The program then outputs the maturity date, as shown below Please enter issue date (DD MM YYYY): 10 12 2000 Please enter days to maturity: 100 The maturity date is 20 03 2001 The program must deal properly with leap years, and with T-Bills which run from one year to the next. Not, perhaps, a really hard problem, but also not trivial (if we have to work from scratch, and do everything in days, months, and years). Happily we’ve a friend who will help out. He’ll design a structure (“Date”) which somehow contains a date. He’ll also supply some functions which perform operations on such structures. This include a function which reads a date from an istream (such as “cin”), a function which moves a date forward by a specified number of days, and a function which writes a date to to an ostream (such as “cout”). Our friend provides us with precise specifications for these functions by writing function prototypes for them, as follows: // structure definition struct Date { // details not shown (we don’t need to know about them) }; // function prototypes // reads a date from the specified input stream and fills in the structure supplied void readDate (Date &date, istream &is); // moves a date forward by the specified number of days void moveDate (Date &date, int days); // outputs a date to the specified output stream void writeDate (const Date &date, ostream &os); Note that, in providing the prototypes, our friend hasn’t bothered to tell us exactly what components make up a “Date”. This is a key point. Eventually this information will have to be filled in, but we don’t need to know anything about it to make use of our friend’s functions. All we need to know is that there is a kind of structure called “Date”. Armed with this information, and the function prototypes, we can easily write the required program, as shown on the next page.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
int main () { Date date; int days; cout << “Please enter issue date DD MM YY: ”; readDate (date, cin); cout << “Please enter days to maturity: ”; cin >> days; moveDate (date, days); cout << “The maturity date is ”; writeDate (date, cout); cout << endl; return 0; } A lot of good things are going on here. For one thing, a reasonably complex problem has been broken down into two simpler problems (and in particular our part of the problem is, as noted, very simple indeed). Also the two problems can be worked on independently. Once we know the name of the structure (“Date”) and have the prototypes for the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

Chapter2 - Chapter 2 ADT Basics Imagine that were dealing...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online