{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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 could be implemented as shown below. We no longer have to ensure that the new day number (i.e. the original day number plus one) is valid as, if it isn’t, the constructor will
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern