C++ Chapter 11: Friends & Destructors

Card Set Information

C++ Chapter 11: Friends & Destructors
2012-04-03 22:09:06
friend accessor destructor

C++ Chapter 11: Friends & Destructors
Show Answers:

  1. A friend function has access to the private members of that class just as a member function does. A friend function can directly read the value of a member variable and can even directly change the value of a member variable.
  2. You make a function a friend of a class by listing the function declaration in the definition of the class and placing the keyword friend in front of the function declaration.
  3. The only reason to make a function a friend is to make the definition of the function simpler and more efficient.
  4. The friend function declaration may be placed in either the private or public section but it will be a public function in either case, so it is clearer to list it in the public section.
  5. A friend function is not a member function. A friend function is defined and called the same way as an ordinary function. You do not use the dot operator in a call to a friend function and you do not use the type qualifier in the definition of a function function.
  6. A simple rule to help you decide between member functions and nonmember functions is:
    1. Use a member function if the task being performed by the function involves only one object.
    2. Use a nonmember function if the task being performed involves more than one object.
  7. A destructor is a member function that is called automatically when an object of the class passes out of scope.
  8. You might want your destructor to perform some other cleanup details but returning memory to the freestore is the main job of the destructor.
  9. A destructor always has the same name as the class it is a member of but the destructor has the tilde symbol, ~, at the beginning of its name.
  10. A destructor has no parameters and no type for the value returned, not even void.
  11. A class can only have one destructor, you cannot overload the destructor for the class. A destructor is defined just like any other member function.