Chapter6 - Chapter 6 Class Extras (Some Bells and Whistles)...

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

View Full Document Right Arrow Icon
Chapter 6 – Class Extras (Some Bells and Whistles) Functions may return objects. Imagine a program that uses class Date, and often needs to find the date that follows a given date (i.e. to find the next date). The function below would be useful. // returns the date after a given date. Date findNextDay (const Date &date) { Date result; result = date; // start off with the given date result.move(1); // move 1 day forwards return result; } This may look a bit scary, but it is conceptually no different from the much less frightening function below. It involves exactly the same basic steps. We declare a local variable that will contain the result (note – it doesn’t have to be called “result”, though this is a good choice). We make the variable contain the correct answer, and then we return the value of the variable. The only real difference is that the function above uses user-defined type “Date” instead of the built-in type “double”. double halfOf (double value) { double result; result = value / 2; return result; } The value returned by “findNextDay” can be used in any way that a variable of type Date can be used. Typically the values returned by such functions are assigned to variables, as shown below. Date d1, d2; d2 = findNextDay(d1); // d2 gets the day after d1 We can also take things further. If it is legal (as it is), to apply the “write” method to “d1” (a Date), it must also be legal to apply the “write” method to “findNextDay(d1)” (also a Date). d1.write (cout); // familiar territory findNextDay (d1).write (cout); // scary looking, but exactly the same basic idea It is important to remember that, while function “findNextDay” manipulates Date objects, it is not a part of the class. It is just part of a program that happens to use the Date class. Now, suppose we decide that the function is so useful that it should, in fact, be part of the class (so making it available to all class users). We can add it, together with a more
Background image of page 1

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

View Full DocumentRight Arrow Icon
general version that returns the date a specified number of days after a given date, by adding the following to the class definition (i.e. the header file). // returns the date that follows the date being operated upon. // throws an invalid_argument exception if the next day is out of the permitted range. Date nextDay () const; // returns the date "days" days beyond the date being operated on. // "days" may be positive or negative. // throws an invalid argument exception if the result is out of the permitted range. Date plusDays (int days) const; One possible implementation of method “nextDay” is shown below. Date Date::nextDay () const { Date result; // constructed using default constructor if (dayNumber == highestDayNumber) { throw invalid_argument ("Date::nextDay - next day is out of range"); } result.dayNumber = dayNumber + 1; return result; } Function “nextDay” would be much easier to write and shorter if there was a constructor which allowed Dates to be constructed using a day number. In this case, the function
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.

This note was uploaded on 02/24/2010 for the course SYSC 2002 taught by Professor Unknown during the Fall '07 term at Carleton.

Page1 / 6

Chapter6 - Chapter 6 Class Extras (Some Bells and Whistles)...

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