Topical Information

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

Question Set Information

Questions

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

    class

    
    The C++ way to implement an
    ADT.  A class typically
    consists of data and functions
    which together represent the
    data type.
    
        
    ADT

    
    Abstract Data Type:  a
    generic, abstracted
    description of a type of data
    typically used to implement
    the data type for programming
    purposes but also useful
    mathematically.
    
        
    object

    
    A variable of a class data
    type.  Also any instance —
    example — of a class within a
    program.  (This would include
    temporary and anonymous
    values, too.  And, of course,
    some objects may be held
    constant for a time during
    program execution.)
    
        
    member variable

    
    A piece of data that makes up
    part of a class' description.
    A variable declared as part of
    a class data type.  Member
    variables are typically placed
    in the private section of a
    class.
    
        
    method

    
    A function that makes up part
    of a class' description.  A
    function declared as part of a
    class data type.  Methods may
    be in either the public
    (mostly) or the private
    (sometimes) section of the
    class.
    
        
    access specifier

    
    A keyword specifying what
    parts of the program are
    allowed to see/use/access
    certain members of a class.
    The two access specifiers we
    learned about are private
    (which is the default state
    for any class member) and
    public.
    
        
    constructor ('ctor')

    
    A special class method which
    is automatically called to
    initialize a class object's
    member variables when an
    instance is created by the
    program[mer] somehow.  All
    constructors are overloaded
    based on the name of the class
    for which they construct.
    Differing argument lists
    therefore lead to different
    initialization patterns — or
    perhaps stem from the need for
    them.  (Constructors may be
    explicitly called as well.
    This leads to anonymous
    objects — perhaps to be used
    during RVO...)
    
        
    accessor

    
    A class method allowing
    programmers outside the class
    to retrieve a copy of the
    current value of one of the
    class object's [private]
    member variables.
    
        
    mutator

    
    A class method allowing
    programmers outside the class
    to request controlled change
    of a class object's [private]
    member variable(s).  The key
    is that error checking is
    embedded (or at least
    consistently called for from)
    the mutators.  Any change the
    program wants to make to the
    class member variables should
    go through the appropriate
    mutator.  This will ensure all
    member variables stay valid
    throughout their lifetime.
    
        
    member access operator

    
    The dot (.) operator which is
    used with an explicit class
    object to use/access one of
    its members (either variable
    or function).  The object's
    identifier comes first, then
    the dot, and then the desired
    member's identifier.  (If the
    member is a function,
    parentheses and arguments
    would be useful at this point.
    *grin*)
    
        
  2. A default constructor places values in an object's data when the programmer declaring the object fails to do so. A initialization/parameterized/unnamed 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.

    
    
    1. Combine multiple values into a single variable.

    2. Primitive data security with private and public access specifiers.

    3. Automatic/Configurable initialization with constructors.

  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, containerNO
    2. arrays, groupingNO
    3. loops, meansNO
    4. ADTs, typeOKAY
    5. overloading, constructionNO
  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 theNO
    2. (actually blank), C++ programs, main, libraryNO
    3. generic, libraries, interface, implementationOKAY
    4. detailed, closets, flip, indexNO
    5. broad, programs, source, objectNO
  6.  
    TRUEFALSE The methods of a class are supposed to emulate the built-in data types.
    TRUEFALSE Toward this end, a class' input method behaves similarly to how cin would act on a char or short variable.
    TRUEFALSE This means that we should always prompt the user before reading their data (within the input method).
    TRUEFALSE Output methods should likewise behave as cout would when printing a double or long variable.
    TRUEFALSE 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? Are they used more by the class itself or by non-class functions? Why must we create them?

    
    Accessor methods are for the retrieval of copies
    of member variable data.  They are used more by
    the non-class parts of the program.  We have to
    create them because the data is in the private
    area of the class and cannot be directly
    used/seen.
    
        

    Give a particular situation where accessors would prove useful.

    
    Whenever the programmer using our class wants to
    do something beyond what we planned for them.
    Maybe plotting a 2D point on the graphics screen.
    Maybe numerically integrating a polynomial whose
    coefficients we hold.  Maybe just a prettily
    labeled output format rather than the typical
    plain and non-labeled output format a class mostly
    likely provides.
    
    
  9. What is the purpose of mutator methods in a class? Are they used more by the class itself or by non-class functions? Why must we create them?

    
    Mutator methods allow for controlled change of
    member variables of a class.  They are used by
    anyone wanting to change a member variable!  Since
    data members are typically private in our class
    designs, they cannot be changed directly from
    outside the class.  (And from inside the class we
    of course depend on the embedded error checks to
    keep our data valid at all times.)
    
        

    Give a particular situation where mutators would prove useful.

    
    Any time the programmer using our class wants to
    modify their object's data in a way we did not
    anticipate during our design.  Maybe taking a 2D
    point's coordinates from a mouse click.  Maybe
    taking the derivative of a polynomial whose
    coefficients we hold.  Maybe just a prettily
    prompted input format rather than the plain,
    non-prompted input format the class most likely
    provides.
    
    
  10.  
    TRUEFALSE Constructors are used to fill in data values during the declaration of a class object.
    TRUEFALSE There are two basic types of constructors.
    TRUEFALSE One is used just like with the built-in data types to fill in default values when none are supplied by the programmer.
    TRUEFALSE Another is used when the programmer does supply default value(s) for the object's data.
  11. Name the three places/times where/when a copy constructor is automatically called by the compiler.

    1. 
      When creating a new object as an exact copy of an
      old object.
      
          
    2. 
      When passing a formal argument by value it is copy
      constructed from the actual argument.
      
          
    3. 
      When returning by value from a function, the value
      returned is copy constructed from the expression
      result in the return statement.