Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 11.5 Yet another example
The original version of
convertToSeconds
looked like this:
double convertToSeconds(const Time& time) {
int minutes = time.hour * 60 + time.minute;
double seconds = minutes * 60 + time.second;
return seconds;
}
It is straightforward to convert this to a member function:
double Time::convertToSeconds() const {
int minutes = hour * 60 + minute;
double seconds = minutes * 60 + second;
return seconds;
}
The interesting thing here is that the implicit parameter should be declared
const
, since we donβt modify it in this function. But it is not obvious where we should put information about a parameter that doesnβt exist. The answer, as you can see in the example, is after the parameter list (which is empty in this case).
The
print
function in the previous section should also declare that the implicit parameter is
const
.
Listing 11.5.1. Feel free to try out the convertToSeconds()
function in this active code!
Checkpoint 11.5.1 .
When writing a member function, where should you declare the implicit parameter to be
const
?
Before the parameter list.
Incorrect! Try again!
Inside the parameter list.
Incorrect! This would be correct if we were talking about a free-standing function.
After the parameter list.
Correct!
Implicit parameters are always const and donβt need to be declared.
Incorrect! Parameters are only const if they are not modified inside the function, implicit parameters are no exception.
You have attempted
of
activities on this page.