Chapter 12 review. Are the following statements true or false? Explain why or why not.
a) A difference between a type and a class is that a type is just a way to represent
data, whereas a class combines data and functions members.
False. The built-in types also combine data and functions. For example, associated
with double are sqrt(), sin(), as well as floating point operators +, -, *, /. A
class lets programmers create new types.
b) Knobby instructions and C++ library functions support procedural abstraction.
True. Both provide a way to hide the details of many instructions of code under one
procedural name.
c) A TV remote control is an everyday example of data abstractionimagine that!
True. People don't have to understand how a TV or a remote work in order to use it, only
how the interface causes desired behaviors. So you see, abstraction can be very
handy!
d) In C++, an object is a class or type.
False. An object is an instance of a class. Similarly, 7.7 is an
instance of a float. An instance is a particular
object, not a class of objects.
e) Information hiding can facilitate modular construction of software.
True. Information hiding separates the concerns of the supplier and client of a software
unit, so they can work independently, yet still cooperate.
f) Members declared following private: are
accessible only to agents with top security clearance.
False. Private members are accessible only to functions defined within the scope of
the same class.
g) Its a good idea to declare data members public.
False. It's better to keep data members private, so that access can be
controlled by member functions, thus avoiding hard to track side effects and also making
it possible to preserve the intended meaning of the data.
h) A constructor is a function which initializes and returns an object.
False. A constructor often initializes an object, but it is not a function, nor does
it return an object, since it does not have a return type. Functions appear in
executable code, producing value in expressions, while constructors appear in
variable declarations.
i) Each object maintains its own copy of its data and function members.
False. Each object or instance does hold its own data values, representing its
state, but not its own function members, which would be wasteful, since all objects of the
same type share the same function members. The type or class provides object
functions and operators, representing their behaviors.
j) Access to members is through a dot notation, class.member, for example, Circle.area().
False. The dot notation is not class.member but object.member, for example, aCircle.area().
k) Extractor functions can help preserve the meaning of data members.
True. Restricting access to data members makes it easier for the designer of a class
to preserve the invariant meaning of its data members.
l) Default parameter values make it possible to invoke constructors in different ways.
True. For example,
Circle(float rad, int X=0, int Y=0);
can be invoked with default parameters, Circle a(2); or invoked overriding both of its defaults, Circle b(1.5,100,200); or just one of its defaults, Circle c(1.3,300);
m) A class interface typically appears in a .cc
or .cpp file.
False. A class interface typically appears in a header file, with extension .h. However, for simple programs, the interface can appear
in the same cc or .cpp file as its implementation.
n) The Lstring class hides most of the details
of C strings.
True. For example, you can compare instances of Lstring
with relational operators ==, <=, etc., without worrying about the fact that these
operators don't work the same way for C strings.
o) You need to understand the mathematical relationship between musical notes to use
the Note class.
False. The implementor of Note needed to
understand these mathematical relationships, but you can just concentrate on
making music by constructing instances of Note
and invoking member functions such as play().
p) You need to have the talent of Mozart to compose musical programs.
False. You can create musical program by constructing instances of Note and invoking member functions such as play(). Musical talent comes in handy for making beautiful
compositions! Indeed, the design and implementation of successful computer programs
is something like musical composition, building on understanding of elements and
structure.