Sams Teach Yourself C++ in 21 Days

(singke) #1
Q Is there ever a reason to use a structure in a C++ program?
A Many C++ programmers reserve the structkeyword for classes that have no func-
tions. This is a throwback to the old C structures, which could not have functions.
Frankly, it is confusing and poor programming practice. Today’s methodless struc-
ture might need methods tomorrow. Then, you’ll be forced either to change the
type to classor to break your rule and end up with a structure with methods. If
you need to call a legacy C function that requires a particular struct, then you
would have the only good reason to use one.
Q Some people working with object-oriented programming use the term “instan-
tiation.” What is this?
A Instantiation is simply a fancy word for the process of creating an object from a
class. A specific object defined as being of the type of a class is a single instance
of a class.

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


The Workshop provides quiz questions to help you solidify your understanding of the
material 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 continuing to tomorrow’s
lesson, where you will learn more about controlling the flow of your program.

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



  1. What is the dot operator, and what is it used for?

  2. Which sets aside memory—a declaration or a definition?

  3. Is the declaration of a class its interface or its implementation?

  4. What is the difference between public and private data members?

  5. Can member functions be private?

  6. Can member data be public?

  7. If you declare two Catobjects, can they have different values in their itsAgemem-
    ber data?

  8. Do class declarations end with a semicolon? Do class method definitions?

  9. What would the header be for a Catfunction,Meow, that takes no parameters and
    returns void?

  10. What function is called to initialize a class?


172 Day 6

Free download pdf