Study your flashcards anywhere!

Download the official Cram app for free >

  • Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key

image

Play button

image

Play button

image

Progress

1/24

Click to flip

24 Cards in this Set

  • Front
  • Back
The principle of encapsulation says that you should separate the specification of how
the class is used by a programmer from the details of how the class is implemented.
The separation should be so complete that you can change the implementation without
needing to change any program that uses the class. The way to ensure this separation
can be summarized in three rules:
1.
Make all the member variables private members of the class.
2.
Make each of the basic operations for the class either a public member function of
the class, a friend function, an ordinary function, or an overloaded operator. Group
the class definition and the function and operator declarations (prototypes)
together. This group, along with its accompanying comments, is called the interface
for the class. Fully specify how to use each such function or operator in a comment
given with the class or with the function or operator declaration.
3.
Make the implementation of the basic operations unavailable to the programmer
who uses the class. The implementation
consists of the function definitions and
overloaded operator definitions (along with any helping functions or other additional
items these definitions require).
The file that contains the program (that is, the
file that contains the
main
function) is often called the
application file
or driver file
Once the implementation file and the application file are compiled, you still need
to connect these files so that they can work together. This is called
linking the files and
is done by a separate utility called a linker.
a collection
of name definitions, such as class definitions and variable declarations.
namespace
Every bit of code you write is in some namespace. If you do not place the code in
some specific namespace, then the code is in a namespace known as the
global
namespace
Do not confuse the global namespace with the unnamed namespace. If you do not put a name
definition in a namespace grouping, then it is in the global namespace. To put a name definition
in the unnamed namespace, you must put it in a namespace grouping that starts out as follows,
without a name:
namespace
{

Names in the global namespace and names in the unnamed namespace may both be accessed
without a qualifier. However, names in the global namespace have global scope (all the program
files), whereas names in an unnamed namespace are local to a compilation unit.
This confusion between the global namespace and the unnamed namespace does not arise very
much in writing code, since there is a tendency to think of names in the global namespace as
being “in no namespace,” even though that is not technically correct. However, the confusion
can easily arise when discussing code.
a file, such as a class implementation file, along with all the files
that are #included in the file, such as the interface header file for the class.
compilation unit
1. Suppose that you are defining a class and that you then want to use this class in a program.
You want to separate the class and program parts into separate files as described
in this chapter. State whether each of the following should be placed in the interface
file, implementation file, or application file.
a. The class definition
b. The declaration for a function that is to serve as a class operation but that is
neither a member nor a friend of the class
c. The declaration for an overloaded operator that is to serve as a class operation but
that is neither a member nor a friend of the class
d. The definition for a function that is to serve as a class operation but that is neither
a member nor a friend of the class
e. The definition for a friend function that is to serve as a class operation
f. The definition for a member function
g. The definition for an overloaded operator that is to serve as a class operation but
that is neither a member nor a friend of the class
h. The definition for an overloaded operator that is to serve as a class operation and
that is a friend of the class
i. The main function of your program
1. Parts a, b, and c go in the interface file; parts d through h go in the implementation file.
(All the definitions of class operations of any sort go in the implementation file.) Part i
(that is, the main part of your program) goes in the application file.
2. Which of the following files has a name that ends in .h: the interface file for a class,
the implementation file for the class, or the application file that uses the class?
2. The name of the interface file ends in .h.
3. When you define a class in separate files, there is an interface file and an implementation
file. Which of these files needs to be compiled? (Both? Neither? Only one? If so,
which one?)
3. Only the implementation file needs to be compiled. The interface file does not need to
be compiled.
4. Suppose you define a class in separate files and use the class in a program. Now suppose
you change the class implementation file. Which of the following files, if any, needs to
be recompiled: the interface file, the implementation file, and/or the application file?
4. Only the implementation file needs to be recompiled. You do, however, need to relink
the files.
5. Suppose you want to change the implementation of the class DigitalTime given in
Displays 11.1 and 11.2. Specifically, you want to change the way the time is recorded.
Instead of using the two private variables hour and minute, you want to use a single
(private) int variable, which will be called minutes. In this new implementation the
private variable minutes will record the time as the number of minutes since the time
0:00 (that is, since midnight). For example, 1:30 is recorded as 90 minutes, since it is
90 minutes past midnight. Describe how you need to change the interface and implementation
files shown in Displays 11.1 and 11.2. You need not write out the files in
their entirety; just indicate what items you need to change and how, in a very general
way, you would change them.
5. You need to delete the private member variables hour and minute from the interface
file shown in Display 11.1 and replace them with the member variable minutes (with
an s). You do not need to make any other changes in the interface file. In the implementation
file, you need to change the definitions of all the constructors and other
member functions, as well as the definitions of the overloaded operators, so that they
work for this new way of recording time. (In this case, you do not need to change any of
the helping functions readHour, readMinute, or digitToInt, but that might not be
true for some other class or even some other reimplementation of this class.) For example,
the definition of the overloaded operator, >>, could be changed to the following:

istream& operator >>(istream& ins,
DigitalTime& theObject)
{
int inputHour, inputMinute;
DigitalTime::readHour(inputHour);
DigitalTime::readMinute(inputMinute);
theObject.minutes = inputMinute + 60*inputHour;
return ins;
}
You need not change any application files for programs that use the class. However,
since the interface file is changed (as well as the implementation file), you will need to
recompile any application files, and of course you will need to recompile the
implementation file.
6. Consider the program shown in Display 11.5. Could we use the name greeting in
place of bigGreeting?
6. No. If you replace bigGreeting with greeting, you will have a definition for the name
greeting in the global namespace. There are parts of the program where all the name
definitions in the namespace Space1 and all the name definitions in the global
namespace are simultaneously available. In those parts of the program, there would be
two distinct definitions for
void greeting( );
7. In Exercise 6, we saw that you could not add a definition for the following function to
the global namespace:
void greeting( );
Can you add a definition for the following function declaration to the global
namespace?
void greeting(int howMany );
7. Yes. The additional definition would cause no problems because overloading is always
allowed. When, for example, the namespace Space1 and the global namespace are available,
the function name greeting would be overloaded. The problem in Self-Test
Exercise 6 was that there would sometimes be two definitions of the function name
greeting with the same parameter lists.
8. What is the output produced by the following program?
#include <iostream>
using namespace std;
namespace Hello
{
void message( );
}
namespace GoodBye
{
void message( );
}
void message( );
int main( )
{
using GoodBye::message;
{
using Hello::message;
message( );
GoodBye::message( );
}
message( );
return 0;
}
void message( )
{
cout << "Global message.\n";
}
namespace Hello
{
void message( )
{
cout << "Hello.\n";
}
}
namespace GoodBye
{
void message( )
{
cout << "Good-Bye.\n";
}
}
8. Hello
Good-Bye
Good-Bye
9. Write the declaration (prototype) for a void function named wow. The function wow
has two parameters, the first of type speed as defined in the speedway namespace and
the second of type speed as defined in the indy500 namespace.
9. void wow(speedway::speed s1, indy500::speed s2);
10. Would the program in Display 11.10 behave any differently if you replaced the four
using declarations
using std::cout;
using std::cin;
using std::endl;
using DTimeSavitch::DigitalTime;
with the following two using directives?
using namespace std;
using namespace DTimeSavitch;
10. The program would behave exactly the same. However, most authorities favor using the
using declaration, as we have done in Display 11.10. Note that with either, there are
still two different functions named readHour. The one in Display 11.10 is different
from the one defined in the unnamed namespace in Display 11.9.
11. What is the output produced by the following program?
#include <iostream>
using namespace std;
namespace Sally
{
void message( );
}
namespace
{
void message( );
}
int main( )
{
{
message( );
using Sally::message;
message( );
}
message( );
return 0;
}
namespace Sally
{
void message( )
{
cout << "Hello from Sally.\n";
}
}
namespace
{
void message( )
{
cout << "Hello from unnamed.\n";
}
}
11. Hello from unnamed.
Hello from Sally.
Hello from unnamed.
12. What is the output produced by the following program?
#include <iostream>
using namespace std;
namespace Outer
{
void message( );
namespace Inner
{
void message( );
}
}
int main( )
{
Outer::message( );
Outer::Inner::message( );
using namespace Outer;
Inner::message( );
return 0;
}
namespace Outer
{
void message( )
{
cout << "Outer.\n";
}
namespace Inner
{
void message( )
{
cout << "Inner.\n";
}
}
}
12. Outer.
Inner.
Inner.
You can define a class and place the definition of the class and the implementation
of its member functions in separate files.
You can then compile the class separately
from any program that uses it, and you can use this same class in any number of
different programs.
a collection of name definitions, such as class definitions and variable
declarations.
namespace
There are three ways to use a name from a namespace:
by making all the names in
the namespace available with a using directive, by making the single name available
with a using declaration for the one name, or by qualifying the name with the
name of the namespace and the scope resolution operator.
You place a definition in a namespace by
placing the definition in a namespace
grouping for that namespace.
can be used to make a name definition local to a compilation
unit.
The unnamed namespace