Anda di halaman 1dari 2

Answers to Non-Programming Exercises

Chapter 1

1)
a)
Jane.age = 24;
Jane.gpa = 3.4;
cout << “Jane’s age is “ << Jane.age << endl;

Jane.c[ 2 ].time = “11:00 AM”;

quality = Jane.c[ 1 ].numCredits * 4;

b)
students[ 9 ].c[ 1 ].days[ 1 ] = ‘T’;

2) The public section of a class can be accessed by the main program, and consists of all
function prototypes for functions that the class designer would like the client to be able
to access. The private section includes data members that can only be accessed by the
functions within the class.

3) Implementation means “written code”.

4) Class specification file – contains the class definition. The file name has the name of
the class with a .h extension.

Class implementation file – contains the definitions for the class functions. The file
name has the name of the class with a .cpp extension.

5) It would have 21 files – 2 for each class and one for the main program. If a program
has 10 objects, we don’t know how many files it has – the objects can all be made from
the same class or from ten different classes, we don’t know.

6) The current value of a variable declared within the private section of a class is always
remembered by an object of that class, even when a function of the object is not in
execution. When a variable is declared locally in a class function, the variable (along
with its value) is destroyed upon exiting the function. We should declare a variable in
the private section when we want the object to remember its value, and we should
declare a variable locally in a class function when it only has a temporary use.

7) The main programmer would call a function which would return the value of the data
member that is needed. To change the value of a data member in the private section,
the main programmer would call a function which changes the value; in general, the
function accepts a parameter that has the value to change it to, so the main programmer
can pass it in.
8) A class interface is the public section of a class. It is called an interface because it is an
interface between the client and the data in the object.

9) A test driver is a main program written for the purpose of testing a class.

10) It takes much longer to debug each problem in a multiple-class program if no classes
have been tested, than to debug a problem in a class when in the process of testing the
class. Even though a driver has to be written to test a class, the savings in time and the
increase in program quality is well worth it. Therefore, each class should be tested as it
is made.

11) Maintenance is any work that needs to be done on a program after it is placed in
operation. If function definitions are placed in the class definition, one does not readily
know where to look for function definitions when maintaining a class. It is also
possible that the function definition will need to be moved from the class specification
file to the class implementation file in the process of maintaining a program.

12) A client (in the context of this textbook) is any thing or person that uses a class.

13) Decisions about how a program should interact with the user should be left to the client.
The client may not like the way your class interacts with the user, and this would cut
down on the reusability of the class.

14) By default, if public and private keywords are not used within a struct definition, then
all data members in the struct are public. If public and private keywords are not used
within a class definition, then by default, all members of the class would be private.

15) Sometimes maintenance involves changing the data members in a class. If the main
program is allowed to access the class data members, then when the data members
change, the main program has to be modified in each place that it accessed a previous
data member. If only class functions are allowed to access a data member, then when
the data member changes, only the class functions that accessed it need to be rewritten;
as long as the class functions have the same prototypes, no changes need to be made to
the main program. Changing the class functions of a class can be a lot easier than
changing the potentially thousands of main programs that use the class.

Anda mungkin juga menyukai