Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

Similar presentations


Presentation on theme: "Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program ©1992-2010 by Pearson Education, Inc. All Rights Reserved."— Presentation transcript:

1 Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

2

3 9.1 Introduction (cont.) Coverage includes (cont.): ◦ How default arguments can be used in a constructor. ◦ Destructors that perform “termination housekeeping” on objects before they are destroyed. ◦ The order in which constructors and destructors are called. ◦ The dangers of member functions that return references to private data. ◦ 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. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

4 9.2 Time Class Case Study ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

5

6

7 9.2 Time Class Case Study (cont.) ◦ Important note: you can define several overloaded constructors for a class. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

8 9.2 Time Class Case Study (cont.) 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. By default, the fill characters appear to the left of the digits in the number. If the number being output fills the specified field, the fill character will not be displayed. 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 (setfill is a “sticky setting”). ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

9 9.2 Time Class Case Study (cont.) Parameterized stream manipulator setw(x) specifies that the next value output should appear in a field width of x i.e. using setw(2): cout prints the value with at least 2 character position ◦ If the value to be output is less than 2 characters positions wide, the value is right justified in the field by default. ◦ If the value to be output is more than 2 characters positions wide, the field width is extended to accommodate the entire value. setw is applied only to the next value displayed (setw is a “nonsticky” setting) ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

10 9.2 Time Class Case Study (cont.) 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. If a member function is defined in the body of a class definition, the compiler attempts to inline calls to the member function. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

11 9.2 Time Class Case Study (cont.) C++ provides inline functions to help reduce function call overhead –especially for small functions. Placing the qualifier inline before a function’s return type in the function definition advises the compiler to generate a copy of the function’s code in place to avoid a function call. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

12

13 9.2 Time Class Case Study (cont.) 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 Time Time arrayOfTimes[ 5 ]; // array of 5 Time objects Time &dinnerTime = sunset; // reference to a Time object Time *timePtr = &dinnerTime; // pointer to a Time object ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

14

15

16

17 9.3 Class Scope and Accessing Class Members A class’s data members and member functions belong to that class’s scope. Nonmember functions are defined at global namespace scope. Within a class’s scope, class members are immediately accessible by all of that class’s member functions and can be referenced by name. Outside a class’s scope, public class members are referenced through one of the handles on an object—an object name, a reference to an object or a pointer to an object. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

18 9.3 Class Scope and Accessing Class Members (cont.) Member functions of a class can be overloaded only by other member functions of that class. Variable declared in a member function have a local scope and are known only to the function. If a member function defines a variable with the same name as a variable with class scope, the class-scope variable is hidden by the block- scope variable in the local scope. ◦ Such a hidden variable can be accessed by preceding the variable name with the class name followed by the scope resolution operator ( :: ). ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

19 9.3 Class Scope and Accessing Class Members (cont.) The dot member selection operator (. ) is preceded by an object’s name or with a reference to an object to access the object’s members. The arrow member selection operator ( -> ) is preceded by a pointer to an object to access the object’s members. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

20

21

22

23 9.4 Separating Interface from Implementation Separating classes into two files—a header file for the class definition (i.e., the class’s interface) and a source code file for the class’s member-function definitions (i.e., the class’s implementation) makes it easier to modify programs. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

24

25 9.5 Access Functions and Utility Functions Access functions can read or display data. A common use for access functions is to test the truth or falsity of conditions— such functions are often called predicate functions. A utility function is a private member function that supports the operation of the class’s public member functions. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

26

27

28

29

30

31

32 9.6 Time Class Case Study: Constructors with Default Arguments Like other functions, constructors can specify default arguments. The default arguments to the constructor ensure that, even if no values are provided in a constructor call, the constructor still initializes the data members to maintain the Time object in a consistent state. A constructor that defaults all its arguments is also a default constructor—i.e., a constructor that can be invoked with no arguments. There can be at most one default constructor per class. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

33

34

35

36

37

38

39 9.6 Time Class Case Study: Constructors with Default Arguments (cont.) Calling setHour, setMinute and setSecond from the constructor may be slightly more efficient because the extra call to setTime would be eliminated. Similarly, copying the code from lines 27, 33 and 39 into constructor would eliminate the overhead of calling setTime, setHour, setMinute and setSecond. This would make maintenance of this class more difficult. ◦ If the implementations of setHour, setMinute and setSecond were to change, the implementation of any member function that duplicates lines 27, 33 and 39 would have to change accordingly. Calling setTime and having setTime call setHour, setMinute and setSecond enables us to limit the changes to the corresponding set function. ◦ Reduces the likelihood of errors when altering the implementation. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

40

41

42

43

44 9.6 Time Class Case Study: Constructors with Default Arguments (cont.) Time ’s set and get functions are called throughout the class’s body. In each case, these functions could have accessed the class’s private data directly. Consider changing the representation of the time from three int values (requiring 12 bytes of memory) to a single int value representing the total number of seconds that have elapsed since midnight (requiring only four bytes of memory). If we made such a change, only the bodies of the functions that access the private data directly would need to change. ◦ No need to modify the bodies of the other functions. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

45 9.6 Time Class Case Study: Constructors with Default Arguments (cont.) Designing the class in this manner reduces the likelihood of programming errors when altering the class’s implementation. Duplicating statements in multiple functions or constructors makes changing the class’s internal data representation more difficult. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.


Download ppt "Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program ©1992-2010 by Pearson Education, Inc. All Rights Reserved."

Similar presentations


Ads by Google