Practice Programs can generally be solved with a short program that directly applies the programming principles presented in this chapter.
Add the following member function to the ADT class DigitalTime
defined in Displays 12.1 and 12.2:
void DigitalTime::intervalSince(const DigitalTime& aPreviousTime, int&
hoursInInterval,int& minutesInInterval) const
This function computes the time interval between two values of type DigitalTime
. One of the values of type DigitalTime
is the object that calls the member function intervalSince
, and the other value of type DigitalTime
is given as the first argument. For example, consider the following code:
DigitalTime current(5, 45), previous(2, 30);
int hours, minutes;
current.intervalSince(previous, hours, minutes);
cout << "The time interval between " << previous
<< " and " << current << endl
<< "is " << hours << " hours and "
<< minutes << " minutes.\n";
In a program that uses your revised version of the DigitalTime
ADT, this code should produce the following output:
The time interval between 2:30 and 5:45
is 3 hours and 15 minutes.
Allow the time given by the first argument to be later in the day than the time of the calling object. In this case, the time given as the first argument is assumed to be on the previous day. You should also write a program to test this revised ADT class.
Do Self-Test Exercise 5 in full detail. Write out the complete ADT class, including interface and implementation files. Also write a program to test your ADT class.
Redo Practice Programs 1 from Chapter 11, but this time define the Money
ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program.
Redo Practice Programs 2 from Chapter 11, but this time define the Pairs
ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program.
This Practice Program explores how the unnamed namespace works. Listed below are snippets from a program to perform input validation for a username and password. The code to input and validate the username is in a file separate from the code to input and validate the password.
File user.cpp:
namespace Authenticate
{
void inputUserName()
{
do {
cout << "Enter your username (8 letters only)" << endl;
cin >> username;
} while (!isValid());
}
string getUserName()
{
return username;
}
}
Define the username
variable and the isValid()
function in the unnamed namespace so the code will compile. The isValid()
function should return true
if username contains exactly eight letters. Generate an appropriate header file for this code.
Repeat the same steps for the file password.cpp
, placing the password
variable and the isValid()
function in the unnamed namespace. In this case, the isValid()
function should return true
if the input password has at least eight characters including at least one nonletter:
File password.cpp:
namespace Authenticate
{
void inputPassword()
{
do {
cout << "Enter your password (at least 8 characters " <<
"and at least one nonletter)" << endl;
cin >> password;
} while (!isValid());
}
string getPassword()
{
return password;
}
}
At this point you should have two functions named isValid(),
each in different unnamed namespaces. Place the following main
function in an appropriate place. The program should compile and run.
int main()
{
inputUserName();
inputPassword();
cout << "Your username is " << getUserName() <<
" and your password is: " <<
getPassword() << endl;
return 0;
}
Test the program with several invalid usernames and passwords.