Sams Teach Yourself C++ in 21 Days

(singke) #1
Q When writing a class constructor, how do I decide what to put in the initializa-
tion and what to put in the body of the constructor?
A A simple rule of thumb is to do as much as possible in the initialization phase—
that is, initialize all member variables there. Some things, like computations
(including those used for initialization) and print statements, must be in the body
of the constructor.
Q Can an overloaded function have a default parameter?
A Yes. One or more of the overloaded functions can have its own default values, fol-
lowing the normal rules for default variables in any function.
Q Why are some member functions defined within the class declaration and oth-
ers are not?
A Defining the implementation of a member function within the declaration makes it
inline. Generally, this is done only if the function is extremely simple. Note that
you can also make a member function inline by using the keyword inline, even if
the function is declared outside the class declaration.

Workshop ............................................................................................................


The Workshop provides quiz questions to help solidify your understanding of the mater-
ial covered and exercises to provide you with experience in using what you’ve learned.
Try to answer the quiz and exercise questions before checking the answers in Appendix
D, and be certain you understand the answers before going to tomorrow’s lesson.

Quiz ................................................................................................................



  1. When you overload member functions, in what ways must they differ?

  2. What is the difference between a declaration and a definition?

  3. When is the copy constructor called?

  4. When is the destructor called?

  5. How does the copy constructor differ from the assignment operator (=)?

  6. What is the thispointer?

  7. How do you differentiate between overloading the prefix and postfix increment
    operators?

  8. Can you overload the operator+for shortintegers?

  9. Is it legal in C++ to overload the operator++so that it decrements a value in your
    class?

  10. What return value must conversion operators have in their declarations?


326 Day 10

Free download pdf