![]() Logically, this is true-you may think of objects as containing data and functions (and our discussion has certainly encouraged this view) physically, however, this is not true.ĩ.3 Class Scope and Accessing Class Members.People new to object-oriented programming often suppose that objects must be quite large because they contain data members and member functions.Once class Time has been defined, it can be used as a type in object, array, pointer and reference declarations as follows: Time sunset // object of type TimeTime arrayOfTimes // array of 5 Time objectsTime &dinnerTime = sunset // reference to a Time objectTime *timePtr = &dinnerTime // pointer to a Time object.If a member function is defined in the body of a class definition, the compiler attempts to inline calls to the member function.Even though a member function declared in a class definition may be defined outside that class definition, that member function is still within that class’s scope.Once the fill character is specified with setfill, it applies for all subsequent values that are displayed in fields wider than the value being displayed.If the number being output fills the specified field, the fill character will not be displayed.By default, the fill characters appear to the left of the digits in the number.Parameterized stream manipulator setfill specifies the fill character that is displayed when an integer is output in a field wider than the number of digits in the value.If the header has been included previously, TIME_H is defined already and the header file is not included again.If the header has not been included previously in a file, the name TIME_H is defined by the #define directive and the header file statements are included.Prevents the code between #ifndef and #endif from being included if the name TIME_H has been defined.In Fig. 9.1, the class definition is enclosed in the following preprocessor wrapper: // prevent multiple inclusions of header file#ifndefTIME_H#defineTIME_H.Our first example (Figs. 9.1–9.3) creates class Time and a driver program that tests the class.Default memberwise assignment for copying the data members in the object on the right side of an assignment into the corresponding data members of the object on the left side of the assignment.The dangers of member functions that return references to private data.The order in which constructors and destructors are called.Destructors that perform “termination housekeeping” on objects before they are destroyed.How default arguments can be used in a constructor.Utility functions-private member functions that support the operation of the class’s public member functions.Access functions that can read or display data in an object.Accessing a class’s public members via three types of “handles”-the name of an object, a reference to an object or a pointer to an object.Class scope and the relationships among class members.“Preprocessor wrappers” in header files to prevent the code in the header from being included into the same source code file more than once.In this chapter, we take a deeper look at classes. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |