6 La evolución de los CADEP Acacia
6.3 La experiencia de validación
ins >> x >> y;
return ins;
}
31. Overload the output operator for the throttle so that it prints 100 times the current flow, followed by a % sign.
32. What should be present in the implementation file newpoint.cxx?
2.6 THE STANDARD TEMPLATE LIBARY AND THE PAIR CLASS
As a computer scientist, it’s important for you to understand how to build and test your own classes, but frequently you’ll find that a suitable class has already been built for you to use in an application—there’s no need for you to write everything from scratch! In C++, a variety of container classes called the Stan-dard Template Library (STL) is available for all programs. This section pro-vides an introduction to one of the simplest STL classes: the pair class.
Each pair object can hold two pieces of data, perhaps an integer and a double number, or a char and a bool value, or even a couple of throttles. The two pieces of data don’t have to be the same data type, but the programmer must specify the types of the pieces when the pair object is declared. Here’s a little example that declares a pair with an integer (as the first piece) and a double (as the second):
#include <utility> // Provides the pair class
using namespace std; // The pair is part of the std namespace int main( )
{
pair<int, double> p;
p.first = 42; // first is the member variable for the int piece p.second = 9.25; // second is the member variable for the double ...
Notice that the member variables, first and second, are both public member variables. The data types of these two pieces are specified in the angle brackets, , as part of the object’s declaration. We’ll see more of these angle brackets—called the template instantiation—as we see more of the STL, and eventually we’ll write our own template classes.
pair<int, double>
Chapter Summary 87
CHAPTER SUMMARY
• Object-oriented programming (OOP) supports information hiding by placing data in packages called objects, which are implemented via classes in C++. Objects are manipulated through functions called member functions, which are defined along with their classes.
• A new data type, together with the functions to manipulate the type, is called an abstract data type or class. The term abstract refers to the fact that we emphasize the abstract specification of what has been provided, disassociated from any actual implementation.
• Private member variables support information hiding by forbidding data components of a class to be accessed outside of the class’s member func-tions. If the implementor of a new class needs other functions to have access to the member variables, then the other functions may be declared as friend functions.
• A constructor is a member function that is automatically called to initial-ize a variable when the variable is declared. Defining constructors increases the reliability of your classes by reducing the chance of using an uninitialized variable.
• To avoid conflicts between different items with the same name, your work should be placed in a namespace. When choosing a name for the namespace, use part of your real name or email address to avoid conflicts with other namespaces.
• Place the documentation and class definition for a new class in a separate header file. Place the implementations of the member functions in a sepa-rate implementation file.
• C++ provides three common kinds of parameters: With a value parame-ter, the argument provides only the initial value for the formal parameter.
With a reference parameter, any use of the parameter within the body of the function will access the argument from the calling program. A const reference parameter has the efficiency of an ordinary reference parame-ter, but there is a guarantee that the argument will not be changed by the function.
• C++ permits you to define the meaning of operators such as + and == for your new classes.
• C++ provides many prebuilt classes—the Standard Template Library—
for all programmers to use.
SOLUTIONS TO SELF-TEST EXERCISES
?
Solutions to Self-Test Exercises1. The private members.
2. Public members of a class are available to anyone using the class. Member functions are often declared public so that users can call them in order to manipulate an instance of the class.
3. true and false
4. A class is a kind of data type that defines data members and member functions that operate on the data. An object is an instance of a class, and is declared as a variable. Once a class is defined, a programmer can declare many objects of that class and manipulate the objects with its functions.
5. A constant member function cannot make any changes to the object’s member variables.
6. The scope resolution operator is used at the head of each member function implementa-tion. See the example use of the scope resolu-tion operator on page 42.
7. The program should include the following statements:
throttle exercise;
exercise.shut_off( );
exercise.shift(3);
cout << exercise.flow( ) << endl;
8. The prototype for the new member function is placed in the class definition. The function implementation is:
bool throttle::is_above_half( ) const // Precondition: shut_off has been called at // least once to initialize the throttle.
// Postcondition: The return value is true if // the current flow is above 0.5.
{
return (flow( ) > 0.5);
}
9. In the public section of the class definition:
bool is_above_half( ) const { return (flow( ) > 0.5); } 10. The compiler automatically creates a simple
default constructor.
11. The keyword void should be removed. Con-structors do not have a return type.
12. The prototype for the new constructor is placed in the class definition. The constructor implementation is:
throttle::throttle (int size, int initial) // Precondition: (0 < size) and // (0 <= initial <= size).
// Postcondition: The throttle has size // positions above the shutoff position, and // it is currently in the position given by the // parameter initial.
13. All the information needed to use the class is in the comment at the front of the header file.
14. A macro guard prevents accidental duplica-tion of a class definiduplica-tion. Normally, if a pro-gram includes a header file more than once, compilation will fail. A macro guard directs the compiler to skip duplicate class defini-tions.
15. The automatic assignment operator will copy the member variables of x to y.
16. The automatic copy constructor will initialize y as a copy of x (by copying the member vari-ables).
Solutions to Self-Test Exercises 89
17. #include "throttle.h"
using namespace main_savitch_2A;
18. Hint: Keep track of the current angle in a pri-vate member variable. If the variable goes below zero, or becomes >= 360, then readjust it so that it lies between 0 and 360.
19. You’ll find part of a solution in Figure 14.1 on page 685.
20. Any items that are not explicitly placed in a namespace become part of a global namespace, and can be used without a using statement or a scope resolution operator.
21. Never put a using statement in a header file;
the third form should be used.
22. Change the constructor’s prototype to this:
throttle(int size = 1, int initial = 0) The other two constructors are no longer needed.
23. rotate_to_upper_right can change p because the parameter is a reference parame-ter. But the call to rotations_needed cannot changep because it uses a value parameter.
24. A function’s parameter is referred to as the formal parameter to distinguish it from the value that is passed in during the function call.
The argument is the passed value.
25. x should be a value parameter if you want the actual argument to remain unchanged. It should be a reference parameter if you want changes to x to affect the actual argument. It can never be a const reference parameter because the function’s body alters the parameter.
26. Value parameters are less efficient for large data types because their values are copied.
However, reference types are less secure because they are modifiable. A const refer-ence parameter provides the best solution by providing a reference parameter that cannot be modified.
27. Here is the implementation:
int operator < ( const throttle& t1, const throttle& t2 )
// Postcondition: The return value is true if // the flow of t1 is less than the flow of t2.
{
return (t1.flow( ) < t2.flow( ));
}
28. The solution is the same as the + operator on page 77, but replace each plus sign with a minus sign.
29. This advice supports information hiding, be-cause the programmer who implements the class is the only one who knows about the private members.
30. Friend functions are not activated by a partic-ular object of a class. Therefore, the name of the object variable must precede the member variables accessed by the friend function, as follows:
ins >> target.x >> target.y;
31. Here is the implementation (you fill in the postcondition):
outs << 100*source.flow( ) << '%';
return outs;
}
32. The top of newpoint.cxx contains a short comment indicating that the documentation for how to use the point class is in the header file. The function implementations appear after the comment, including all the functions listed in Figure 2.18 on page 83, except for get_x and get_y (which are inline functions).
These implementations must be in a name-space grouping. In the header file, we used main_savitch_2B for the namespace to avoid conflict with the earlier point (which was in the namespace main_savitch_2A).