220 likes | 312 Views
Classes a deeper look. Chapter 9. How to use a preprocessor wrapper. Used to prevent multiple class definition errors. You can only define a class once!! Prevents code in header from being included in same source code more than once!. Get code for time.h written/debugged.
E N D
Classes a deeper look Chapter 9
How to use a preprocessor wrapper • Used to prevent multiple class definition errors. You can only define a class once!! Prevents code in header from being included in same source code more than once!
Get code for time.h written/debugged • On Page 381, creates the class Time. • Where are the Prototypes for class member functions? • Where are the class data members?
Why make data members private? • You can only access these through the public member functions. This keeps your data members safe.
C++ standards • Put public items first so you can find them quickly. • Don’t put multiple public and private statements.
You Want data members private • Unless you can prove that the members need to be public!
Preprocessor wrapper • Lines 6, 7 and 23 • #ifndef – means if not defined • #endif – keeps code inside unless TIME_H hasn’t been defined.
C++ convention • Header is written in all caps for the #ifndef statements, with an underscore replacing the ‘.’
throw • Lines 28 and 29 • Creates a new object of type invalid_argument to send to the client calling program.
setfill • Fill character to display when the integer output in a field wider than number of digits in the value. • Default to the left of the value. • “Sticky setting”
setw • This is an example of a “non-sticky setting”. Just applies to the following value.
Line 42 • Uses the conditional operator ?: instead of an if statement.
You will today: • Type and debug code for: • Time.h (pg. 381) • Definitions for member functions (pp. 382, 383) • Testing code (pp. 386)
Accessing class members • 1 – use the . Operator with the name of an object, or a reference to an object. • Example: • Count counter; //create counter object of class Count • Count *counterPtr = &counter; //create pointer to counter • Count &counterRef = counter; //create reference to counter • counter.setX(1); //using the name of the object to call member function setX() • counterRef.setX(2); //using the reference to the object
Accessing class members • 2 – use the -> Operator with a pointer to an object. • Example: • counterPtr->setX(3);//uses a pointer to the object to access member function, setX()
private • Class members specified as private are accessible only to member functions of the class and friends of the class.
Public • Class members specified as public are accessible anywhere an object of the class is in scope.
Handles to an object • Handles are the way we access the properties of an object, such as the dot (.) and arrow(->) operators.
You will today: • Type and debug code for: • Access operators (pp. 388, 389)
Access Functions • Read or display data.
Utility Functions • A type of access function which tests the truth or falseness of conditions. • Also called predicate functions. • Also called helper function. • These functions are private member functions which support the operations of the class’s other member functions.
You will today • Finish the code for this chapter so far. • Determine what is output by the programs shown in the handout without using Visual C++ • Enter the code for the SalesPerson class found on pages 390 through 393 • Demonstrates a private member function, totalAnnualSales()