Topical Information

The purpose of this quiz is to give you a chance to focus your knowledge of simple classes in C++.

Quiz Information

Questions

  1. (Briefly) Define the following object-oriented terms:

    1. class
      
          The C++ way to implement an ADT.
      
      
    2. ADT
      
          An abstract data type.  A description in generic terms of
          the physical and behavioral characteristics of a group of
          data.
      
      
    3. object
      
          A single variable instantiated from a class type.
      
      
    4. member variable
      
          A variable inside a class.  Typically made private to
          avoid accidental changes to erroneous values.
      
      
    5. method
      
          Another name for a function in a class.
      
      
    6. access specifier
      
          One of the keywords private or public which tell the compiler
          that the following members of a class are to be accessible only in
          a certain way.  private disallows access from outside the class
          whereas public allows anyone in the entire program to access the
          members.
      
      
    7. constructor
      
          A special kind of class function that is called automatically to
          initialize the member variables of a class object when it is first
          created.  These functions are named the same as the class and are
          not allowed to have a return type or return statement.
      
      
    8. accessor
      
          A type of class method that retrieves a copy of the private
          member variable data for those outside the class to use in ways
          the class designer didn't foresee or expect.
      
      
    9. mutator
      
          A type of class method that makes controlled changes to private
          member variables for those outside the class.  The control here is
          typically error checking on sent values before they are stored in the
          member variables.  Often a bool is sent back to indicate success
          or failure of the change.
      
      
    10. member access operator
      
          Also 'dot' operator.  The period which we place after an object name to
          access the member listed after the operator.
      
      

  2. A default constructor places values in an object's data when the programmer declaring the object fails to do so. A parameterized constructor fills in values supplied by the programmer. (If the latter has only one input, the syntax can take one of two forms: className object = value; — like the built-in types are normally initialized — or className object (value); — which is more obviously a method call.) Finally, a copy constructor fills in values from a previously constructed object. (This constructor, too, can use both the above syntaxes. It is also called in several other places than normal variable declaration.)

  3. Name three of the most important benefits that classes give us over the built-in types.

    
        One is that they can hold more than a single value together in a single
        variable name.
    
        Two is that they have basic data protection with the private and
        public keywords.
    
        Three is that they can initialize themselves with constructors when
        created — even when the programmer forgets to do so!
    
    

  4. classes are the C++ mechanism for implementing _____. Basically this is a way to describe an entity from the real world to the computer so that we can use it in our program as a _____ to represent our data.

    1. structures, container NO
    2. arrays, grouping NO
    3. loops, means NO
    4. ADTs, type YES
    5. overloading, construction NO

  5. A class is supposed to be a _____ description of all the objects in a group/set. Because of this re-useful-ness, classes are often placed in _____. The class definition goes in the _____ file, while the definitions of its methods go in the associated _____ file.

    1. simplified, braces, top of the, end of the NO
    2. (actually blank), C++ programs, main, library NO
    3. generic, libraries, interface, implementation YES
    4. detailed, closets, flip, index NO
    5. broad, programs, source, object NO

  6.  
    TRUE   FALSE   The methods of a class are supposed to emulate the built-in data types.
    TRUE   FALSE   Toward this end, a class' input method behaves similarly to how cin would act on a char or short variable.
    TRUE   FALSE   This means that we should always prompt the user before reading their data (within the input method).
    TRUE   FALSE   Output methods should likewise behave as cout would when printing a double or long variable.
    TRUE   FALSE   This means that we should always label the output data to help the user understand what's being printed (within the output method).
  7. A private keyword (which is optional/assumed) can be placed in a class declaration to show that certain parts of the class cannot be accessed from outside the class. The public keyword is placed to show that following items may be accessed from inside or outside the class. Both of these keywords must be followed by a : (colon) symbol.

  8. What is the purpose of accessor methods in a class? What are they used for? Why do we create them? Give a particular situation where they would prove useful.

    
        These methods supply the caller with a copy of the requested member data.
        They are useful because data is made private for its protection and is
        therefore inaccessible outside the class.  Without the accessors, the
        rest of the program would have no idea what values were stored inside an
        object at any particular time.
    
        A particular situation where they could be useful would be if the
        programmer using a Time class wanted to draw an analog clock on the screen
        instead of printing the time out digitally.  The basic Time class
        implementation isn't going to have methods for drawing an analog version of
        the clock on screen.  That's too labor intensive and platform specific.
        Allowing the programmer outside the class a copy of the class' private data
        would fix this situation right up!
    
    

  9. What is the purpose of mutator methods in a class? What are they used for? Why do we create them? Give a particular situation where they would prove useful.

    
        Mutator methods make controlled changes to class member data.  Such
        a feature is highly prized by the programmer outside the class who
        wants to make a change to some data within a class object they own.
    
        This is necessary because we always make data members private to
        avoid accidental changes to bad values.  But the mutators avoid such
        accidents by doing error checks before storing the sent value into the
        member variable.  If it isn't good data, it isn't stored after all!
    
        To reflect this possibility, mutators often return a bool to show
        that they were or were not successful.
    
    

  10.  
    TRUE   FALSE   Constructors are used to fill in data values during the declaration of a class object.
    TRUE   FALSE   There are two basic types of constructors.
    TRUE   FALSE   One is used just like with the built-in data types to fill in default values when none are supplied by the programmer.
    TRUE   FALSE   Another is used when the programmer does supply default value(s) for the object's data.
  11. Name three places when a copy constructor is automatically called by the compiler.

    
        When a new object is being made as a copy of a previously declared
        object, the copy constructor is called to make the new object as a
        copy of the old object.
    
        When a class object is passed by value to a function, the copy
        constructor is called to make the formal argument as a copy of the
        actual argument.
    
        When an object is returned from a function by value, the value
        given to the caller is copy constructed from the expression value
        found on the return statement.
    
        And, just for good measure, when a catch block catches the thrown
        exception by value, the caught exception is copy constructed from the
        originally thrown exception.