1. Parts (a), (b), and (c) go in the interface file; parts (d) through (h) go in the implementation file. (All the definitions of ADT 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. The name of the interface file ends in .h
.
3. Only the implementation file needs to be compiled. The interface file does not need to be compiled.
4. Only the implementation file needs to be recompiled. You do, however, need to relink the files.
5. You need to delete the private member variables hour
and minute
from the interface file shown in Display 12.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;
readHour(ins, inputHour);
readMinute(ins, 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. The short answer is that an ADT is simply a class that you defined following good programming practices of separating the interface from the implementation. Also, when we describe a class as an ADT, we consider the nonmember basic operations such as overloaded operators to be part of the ADT, even though they are not technically speaking part of the C++ class.
7. No. If you replace bigGreeting
with greeting
, then 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 savitch1
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( );
8. Yes, the additional definition would cause no problems. This is because overloading is always allowed. When, for example, the namespaces savitch1
and the global namespace are available, the function name greeting
would be overloaded. The problem in Self-Test Exercise 7 was that there would sometimes be two definitions of the function name greeting
with the same parameter lists.
9. Yes, a namespace can have any number of groupings. For example, the following are two groupings for the namespace savitch1
that appear in Display 12.5:
namespace savitch1
{
void greeting( );
} namespace savitch1
{
void greeting( )
{
cout << "Hello from namespace savitch1.\n";
}
}
10. void wow(speedway::speed s1, indy500::speed s2);
void input(std::istream& ins);
void output(std::ostream& outs) const;
12. The program would behave exactly the same.
13. Hello from unnamed. Hello from Sally. Hello from unnamed.
14. Yes, you can eliminate the grouping for the helping function declarations, as long as the grouping with the helping function definitions occurs before the helping functions are used. For example, you could remove the namespace with the helping function declarations and move the grouping with the helping function definitions to just before the namespace grouping for the namespace dtimesavitch
.