The purpose of this quiz is to give you a chance to focus your knowledge of simple classes in C++.
(Briefly) Define the following object-oriented terms:
The C++ way to implement an ADT.
An abstract data type. A description in generic terms of
the physical and behavioral characteristics of a group of
data.
A single variable instantiated from a class type.
A variable inside a class. Typically made private to
avoid accidental changes to erroneous values.
Another name for a function in a class.
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.
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.
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.
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.
Also 'dot' operator. The period which we place after an object name to
access the member listed after the operator.
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.)
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!
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.
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.
| 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). |
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.
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!
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.
| 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. |
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.