Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Section 11.3 Implicit variable access

Actually, the new version of Time::print is more complicated than it needs to be. We don’t really need to create a local variable in order to refer to the instance variables of the current object.
If the function refers to hour, minute, or second, all by themselves with no dot notation, C++ knows that it must be referring to the current object. So we could have written:
void Time::print () {
  cout << hour << ":" << minute << ":" << second << endl;
}
This kind of variable access is called implicit because the name of the object does not appear explicitly. Features like this are one reason member functions are often more concise than nonmember functions.

Checkpoint 11.3.1.

Implicit variable access in member functions allows us to access member variables __________.
  • after being granted permission
  • Incorrect! You don’t need "permission" to access member variables inside a member function.
  • only inside of that specific member function
  • Incorrect! You can access member variables implicitly inside any and all member functions.
  • using dot notation
  • Incorrect! You don’t need to use dot notation to access variables implicitly.
  • directly, without dot notation
  • Correct! Implicit variable access allows us to access variables directly-- without using dot notation.

Checkpoint 11.3.2.

When should you use the scope resolution operator ::?
  • Every time you are working with data structures!
  • Incorrect! The scope resolution operator is not always necessary!
  • When you implement member functions inside of the structure definition.
  • Incorrect! When you write member functions inside of the structure definition, you do not need to specify the scope.
  • When you implement member functions outside of the structure definition.
  • Correct! When you write member functions outside of the structure definition, you need to specify the scope, hence the :: operator!
  • Never! It is bad practice!
  • Incorrect! The scope resolution operator is good practice when used correctly!
You have attempted 1 of 2 activities on this page.