|
|
|
|
|
|
|
TimeType time1;
TimeType time2;
int inputHrs;
int inputMins;
int inputSecs;
time1.Set(5, 20, 0);
// Assert: time1 corresponds to 5:20:0
cout << Enter hours, minutes, seconds: ;
cin >> inputHrs >> inputMins >> inputSecs;
time2.Set(inputHrs, inputMins, inputSecs);
if (time1.LessThan(time2))
DoSomething();
time2 = time1; // Member-by-member assignment
time2. Write ();
// Assert: 5:20:0 is output |
|
|
|
|
|
|
|
|
Earlier we remarked that the Equal and LessThan functions have only one parameter each, even though they are comparing two TimeType objects. In the If statement of the code segment above, we are comparing time1 and time2. Because LessThan is a class member function, we invoke it by giving the name of a class object (time1), then a dot, then the function name (LessThan). Only one item remains unspecified: the class object that time1 should be compared with (time2). Therefore, the LessThan function requires only one parameter, not two. Here is another way of explaining it: If a class member function represents a binary (two-operand) operation, the first operand appears to the left of the dot operator and the second operand is in the parameter list. (To generalize, an n-ary operation has n-1 operands in the parameter list. Thus, a unary operationsuch as Write or Increment in the TimeType classhas an empty parameter list.) |
|
|
|
|
|
|
|
|
In addition to member selection and assignment, a few other built-in operators are valid for classes. These operators are used for manipulating memory addresses, and we defer discussing them until later in the book. For now, think of . and = as the only valid built-in operators. |
|
|
|
|
|
|
|
|
From the very beginning, you have been working with C++ classes in a particular context: input and output. The standard header file iostream.h contains the declarations of two classesistream and ostreamthat manage a program's I/O. The C++ standard library declares cin and cout to be objects of these classes: |
|
|
|
|
|
|
|
|
istream cin;
ostream cout; |
|
|
|
|
|