1 / 22

Classes a deeper look

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.

majed
Download Presentation

Classes a deeper look

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes a deeper look Chapter 9

  2. 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!

  3. 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?

  4. Why make data members private? • You can only access these through the public member functions. This keeps your data members safe.

  5. C++ standards • Put public items first so you can find them quickly. • Don’t put multiple public and private statements.

  6. You Want data members private • Unless you can prove that the members need to be public!

  7. Preprocessor wrapper • Lines 6, 7 and 23 • #ifndef – means if not defined • #endif – keeps code inside unless TIME_H hasn’t been defined.

  8. C++ convention • Header is written in all caps for the #ifndef statements, with an underscore replacing the ‘.’

  9. throw • Lines 28 and 29 • Creates a new object of type invalid_argument to send to the client calling program.

  10. 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”

  11. setw • This is an example of a “non-sticky setting”. Just applies to the following value.

  12. Line 42 • Uses the conditional operator ?: instead of an if statement.

  13. You will today: • Type and debug code for: • Time.h (pg. 381) • Definitions for member functions (pp. 382, 383) • Testing code (pp. 386)

  14. 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

  15. 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()

  16. private • Class members specified as private are accessible only to member functions of the class and friends of the class.

  17. Public • Class members specified as public are accessible anywhere an object of the class is in scope.

  18. Handles to an object • Handles are the way we access the properties of an object, such as the dot (.) and arrow(->) operators.

  19. You will today: • Type and debug code for: • Access operators (pp. 388, 389)

  20. Access Functions • Read or display data.

  21. 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.

  22. 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()

More Related