Download presentation
Presentation is loading. Please wait.
Published byArchibald Holland Modified over 9 years ago
1
611 18200 計算機程式語言 Lecture 10-1 國立台灣大學生物機電系 林達德 10 Classes: A Deeper Look, Part 2
2
611 18200 計算機程式語言 Lecture 10-2 國立台灣大學生物機電系 林達德 10.1Introduction 10.2const (Constant) Objects and const Member Functions 10.3 Composition: Objects as Members of Classes 10.4 friend Functions and friend Classes 10.5Using the this Pointer 10.6Dynamic Memory Management with Operators new and delete 10.7static Class Members 10.8Data Abstraction and Information Hiding 10.8.1Example: Array Abstract Data Type 10.8.2 Example: String Abstract Data Type 10.8.3 Example: Queue Abstract Data Type 10.9Container Classes and Iterators 10.10 Proxy Classes
3
611 18200 計算機程式語言 Lecture 10-3 國立台灣大學生物機電系 林達德 OBJECTIVES In this chapter you will learn: To specify const (constant) objects and const member functions. To create objects composed of other objects. To use friend functions and friend classes. To use the this pointer. To create and destroy objects dynamically with operators new and delete, respectively. To use static data members and member functions. The concept of a container class. The notion of iterator classes that walk through the elements of container classes. To use proxy classes to hide implementation details from a class's clients.
4
611 18200 計算機程式語言 Lecture 10-4 國立台灣大學生物機電系 林達德 10.1 Introduction Classes Data abstraction Object-based programming (OBP) Inheritance and polymorphism
5
611 18200 計算機程式語言 Lecture 10-5 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Principle of least privilege –Only allow modification of necessary objects Keyword const –Specify object not modifiable –Compiler error if attempt to modify const object –Example const Time noon( 12, 0, 0 ); Declares const object noon of class Time Initializes to 12
6
611 18200 計算機程式語言 Lecture 10-6 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions const member functions –Member functions for const objects must also be const Cannot modify object –Specify const in both prototype and definition Prototype –After parameter list Definition –Before beginning left brace
7
611 18200 計算機程式語言 Lecture 10-7 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Constructors and destructors –Cannot be const –Must be able to modify objects Constructor –Initializes objects Destructor –Performs termination housekeeping
8
611 18200 計算機程式語言 Lecture 10-8 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Declaring an object as const helps enforce the principle of least privilege. Attempts to modify the object are caught at compile time rather than causing execution-time errors. Using const properly is crucial to proper class design, program design and coding. Software Engineering Observation 10.1
9
611 18200 計算機程式語言 Lecture 10-9 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Declaring variables and objects const can improve performance — today ’ s sophisticated optimizing compilers can perform certain optimizations on constants that cannot be performed on variables. Performance Tip 10.1
10
611 18200 計算機程式語言 Lecture 10-10 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Defining as const a member function that modifies a data member of an object is a compilation error. Common Programming Error 10.1
11
611 18200 計算機程式語言 Lecture 10-11 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Defining as const a member function that calls a non- const member function of the class on the same instance of the class is a compilation error. Common Programming Error 10.2
12
611 18200 計算機程式語言 Lecture 10-12 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Invoking a non- const member function on a const object is a compilation error. Common Programming Error 10.3
13
611 18200 計算機程式語言 Lecture 10-13 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions A const member function can be overloaded with a non- const version. The compiler chooses which overloaded member function to use based on the object on which the function is invoked. If the object is const, the compiler uses the const version. If the object is not const, the compiler uses the non- const version. Software Engineering Observation 10.2
14
611 18200 計算機程式語言 Lecture 10-14 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Attempting to declare a constructor or destructor const is a compilation error. Common Programming Error 10.4
15
Outline 611 18200 計算機程式語言 Lecture 10-15 國立台灣大學生物機電系 林達德 Time.h (1 of 2) Declare const get functions.
16
Outline 611 18200 計算機程式語言 Lecture 10-16 國立台灣大學生物機電系 林達德 Time.h (2 of 2) Declare const function printUniversal.
17
Outline 611 18200 計算機程式語言 Lecture 10-17 國立台灣大學生物機電系 林達德 Time.cpp (1 of 3)
18
Outline 611 18200 計算機程式語言 Lecture 10-18 國立台灣大學生物機電系 林達德 Time.cpp (2 of 3)
19
Outline 611 18200 計算機程式語言 Lecture 10-19 國立台灣大學生物機電系 林達德 Time.cpp (3 of 3) const functions do not modify objects.
20
Outline 611 18200 計算機程式語言 Lecture 10-20 國立台灣大學生物機電系 林達德 fig10_03.cpp (1 of 2) Declare noon a const object. Note that non- const constructor can initialize const object. Attempting to invoke non- const member function on const object results in compiler error.
21
Outline 611 18200 計算機程式語言 Lecture 10-21 國立台灣大學生物機電系 林達德 fig10_03.cpp (2 of 2) Attempting to invoke non- const member function on const object results in compiler error even if function does not modify object.
22
611 18200 計算機程式語言 Lecture 10-22 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Member initializer syntax –Initializing with member initializer syntax Can be used for –All data members Must be used for –const data members –Data members that are references
23
Outline 611 18200 計算機程式語言 Lecture 10-23 國立台灣大學生物機電系 林達德 Increment.h (1 of 1) Declare increment as const data member.
24
Outline 611 18200 計算機程式語言 Lecture 10-24 國立台灣大學生物機電系 林達德 Increment.cpp (1 of 1) Member initializer list separated from parameter list by colon. Member initializer syntax can be used for non- const data member count. Member initializer syntax must be used for const data member increment. Member initializer consists of data member name ( increment ) followed by parentheses containing initial value ( c ).
25
Outline 611 18200 計算機程式語言 Lecture 10-25 國立台灣大學生物機電系 林達德 fig10_06.cpp (1 of 1)
26
611 18200 計算機程式語言 Lecture 10-26 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions A const object cannot be modified by assignment, so it must be initialized. When a data member of a class is declared const, a member initializer must be used to provide the constructor with the initial value of the data member for an object of the class. The same is true for references. Software Engineering Observation 10.3
27
611 18200 計算機程式語言 Lecture 10-27 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Not providing a member initializer for a const data member is a compilation error. Common Programming Error 10.5
28
611 18200 計算機程式語言 Lecture 10-28 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Constant data members ( const objects and const variables) and data members declared as references must be initialized with member initializer syntax; assignments for these types of data in the constructor body are not allowed. Software Engineering Observation 10.4
29
611 18200 計算機程式語言 Lecture 10-29 國立台灣大學生物機電系 林達德 10.2 const (Constant) Objects and const Member Functions Declare as const all of a class ’ s member functions that do not modify the object in which they operate. Occasionally this may seem inappropriate, because you will have no intention of creating const objects of that class or accessing objects of that class through const references or pointers to const. Declaring such member functions const does offer a benefit, though. If the member function is inadvertently written to modify the object, the compiler will issue an error message. Error-Prevention Tip 10.1
30
Outline 611 18200 計算機程式語言 Lecture 10-30 國立台灣大學生物機電系 林達德 Increment.h (1 of 1) Declare increment as const data member.
31
Outline 611 18200 計算機程式語言 Lecture 10-31 國立台灣大學生物機電系 林達德 Increment.cpp (1 of 1) Attempting to modify const data member increment results in error.
32
Outline 611 18200 計算機程式語言 Lecture 10-32 國立台灣大學生物機電系 林達德 fig10_09.cpp (1 of 2)
33
Outline 611 18200 計算機程式語言 Lecture 10-33 國立台灣大學生物機電系 林達德 fig10_09.cpp (2 of 2) Not using member initializer syntax to initialize const data member increment results in error. Attempting to modify const data member increment results in error.
34
611 18200 計算機程式語言 Lecture 10-34 國立台灣大學生物機電系 林達德 10.3Composition: Objects as Members of Classes Composition –Class has objects of other classes as members Construction of objects –Member objects constructed in order declared Not in order of constructor’s member initializer list Constructed before enclosing class objects (host objects)
35
611 18200 計算機程式語言 Lecture 10-35 國立台灣大學生物機電系 林達德 10.3Composition: Objects as Members of Classes A common form of software reusability is composition, in which a class has objects of other classes as members. Software Engineering Observation 10.5
36
Outline 611 18200 計算機程式語言 Lecture 10-36 國立台灣大學生物機電系 林達德 Date.h (1 of 1) Note no constructor with parameter of type Date. Recall compiler provides default copy constructor.
37
Outline 611 18200 計算機程式語言 Lecture 10-37 國立台灣大學生物機電系 林達德 Date.cpp (1 of 3)
38
Outline 611 18200 計算機程式語言 Lecture 10-38 國立台灣大學生物機電系 林達德 Date.cpp (2 of 3) Output to show timing of constructors. Output to show timing of destructors.
39
Outline 611 18200 計算機程式語言 Lecture 10-39 國立台灣大學生物機電系 林達德 Date.cpp (3 of 3)
40
Outline 611 18200 計算機程式語言 Lecture 10-40 國立台灣大學生物機電系 林達德 Employee.h (1 of 1) Using composition; Employee object contains Date objects as data members.
41
Outline 611 18200 計算機程式語言 Lecture 10-41 國立台灣大學生物機電系 林達德 Employee.cpp (1 of 2) Member initializer syntax to initialize Date data members birthDate and hireDate ; compiler uses default copy constructor.
42
Outline 611 18200 計算機程式語言 Lecture 10-42 國立台灣大學生物機電系 林達德 Employee.cpp (2 of 2) Output to show timing of constructors. Output to show timing of destructors.
43
Outline 611 18200 計算機程式語言 Lecture 10-43 國立台灣大學生物機電系 林達德 fig10_14.cpp (1 of 2) Create Date objects to pass to Employee constructor.
44
Outline 611 18200 計算機程式語言 Lecture 10-44 國立台灣大學生物機電系 林達德 fig10_14.cpp (2 of 2) Note two additional Date objects constructed; no output since default copy constructor used. Destructor for host object manager runs before destructors for member objects hireDate and birthDate. Destructor for Employee ’s member object hireDate. Destructor for Employee ‘s member object birthDate. Destructor for Date object hire. Destructor for Date object birth.
45
611 18200 計算機程式語言 Lecture 10-45 國立台灣大學生物機電系 林達德 10.3Composition: Objects as Members of Classes A compilation error occurs if a member object is not initialized with a member initializer and the member object ’ s class does not provide a default constructor (i.e., the member object ’ s class defines one or more constructors, but none is a default constructor). Common Programming Error 10.6
46
611 18200 計算機程式語言 Lecture 10-46 國立台灣大學生物機電系 林達德 10.3Composition: Objects as Members of Classes Initialize member objects explicitly through member initializers. This eliminates the overhead of “ doubly initializing ” member objects — once when the member object ’ s default constructor is called and again when set functions are called in the constructor body (or later) to initialize the member object. Performance Tip 10.2
47
611 18200 計算機程式語言 Lecture 10-47 國立台灣大學生物機電系 林達德 10.3Composition: Objects as Members of Classes If a class member is an object of another class, making that member object public does not violate the encapsulation and hiding of that member object ’ s private members. However, it does violate the encapsulation and hiding of the containing class ’ s implementation, so member objects of class types should still be private, like all other data members. Software Engineering Observation 10.6
48
611 18200 計算機程式語言 Lecture 10-48 國立台灣大學生物機電系 林達德 10.4 friend Functions and friend Classes friend function –Defined outside class’s scope –Right to access non-public members Declaring friend s –Function Precede function prototype with keyword friend –All member functions of class ClassTwo as friend s of class ClassOne Place declaration of form friend class ClassTwo; in ClassOne definition
49
611 18200 計算機程式語言 Lecture 10-49 國立台灣大學生物機電系 林達德 10.4 friend Functions and friend Classes Properties of friendship –Friendship granted, not taken Class B friend of class A –Class A must explicitly declare class B friend –Not symmetric Class B friend of class A Class A not necessarily friend of class B –Not transitive Class A friend of class B Class B friend of class C Class A not necessarily friend of Class C
50
611 18200 計算機程式語言 Lecture 10-50 國立台灣大學生物機電系 林達德 10.4 friend Functions and friend Classes Even though the prototypes for friend functions appear in the class definition, friends are not member functions. Software Engineering Observation 10.7
51
611 18200 計算機程式語言 Lecture 10-51 國立台灣大學生物機電系 林達德 10.4 friend Functions and friend Classes Member access notions of private, protected and public are not relevant to friend declarations, so friend declarations can be placed anywhere in a class definition. Software Engineering Observation 10.8
52
611 18200 計算機程式語言 Lecture 10-52 國立台灣大學生物機電系 林達德 10.4 friend Functions and friend Classes Place all friendship declarations first inside the class definition ’ s body and do not precede them with any access specifier. Good Programming Practice 10.1
53
611 18200 計算機程式語言 Lecture 10-53 國立台灣大學生物機電系 林達德 10.4 friend Functions and friend Classes Some people in the OOP community feel that “ friendship ” corrupts information hiding and weakens the value of the object-oriented design approach. In this text, we identify several examples of the responsible use of friendship. Software Engineering Observation 10.9
54
Outline 611 18200 計算機程式語言 Lecture 10-54 國立台灣大學生物機電系 林達德 fig10_15.cpp (1 of 2) Precede function prototype with keyword friend.
55
Outline 611 18200 計算機程式語言 Lecture 10-55 國立台灣大學生物機電系 林達德 fig10_15.cpp (2 of 2) Pass Count object since C- style, standalone function. Since setX friend of Count, can access and modify private data member x. Use friend function to access and modify private data member x.
56
Outline 611 18200 計算機程式語言 Lecture 10-56 國立台灣大學生物機電系 林達德 fig10_16.cpp (1 of 3)
57
Outline 611 18200 計算機程式語言 Lecture 10-57 國立台灣大學生物機電系 林達德 fig10_16.cpp (2 of 3) Attempting to modify private data member from non- friend function results in error.
58
Outline 611 18200 計算機程式語言 Lecture 10-58 國立台灣大學生物機電系 林達德 fig10_16.cpp (3 of 3) Attempting to modify private data member from non- friend function results in error.
59
611 18200 計算機程式語言 Lecture 10-59 國立台灣大學生物機電系 林達德 10.5Using the this Pointer this pointer –Allows object to access own address –Not part of object itself Implicit argument to non- static member function call –Implicitly reference member data and functions –Type of this pointer depends on Type of object Whether member function is const In non- const member function of Employee –this has type Employee * const Constant pointer to non-constant Employee object In const member function of Employee –this has type const Employee * const Constant pointer to constant Employee object
60
Outline 611 18200 計算機程式語言 Lecture 10-60 國立台灣大學生物機電系 林達德 fig10_17.cpp (1 of 2)
61
Outline 611 18200 計算機程式語言 Lecture 10-61 國立台灣大學生物機電系 林達德 fig10_17.cpp (2 of 2) Implicitly use this pointer; only specify name of data member ( x ). Explicitly use this pointer with arrow operator. Explicitly use this pointer; dereference this pointer first, then use dot operator.
62
611 18200 計算機程式語言 Lecture 10-62 國立台灣大學生物機電系 林達德 10.5Using the this Pointer Attempting to use the member selection operator (. ) with a pointer to an object is a compilation error — the dot member selection operator may be used only with an lvalue such as an object ’ s name, a reference to an object or a dereferenced pointer to an object. Common Programming Error 10.7
63
611 18200 計算機程式語言 Lecture 10-63 國立台灣大學生物機電系 林達德 10.5Using the this Pointer Cascaded member function calls –Multiple functions invoked in same statement –Function returns reference pointer to same object { return *this; } –Other functions operate on that pointer –Functions that do not return references must be called last
64
Outline 611 18200 計算機程式語言 Lecture 10-64 國立台灣大學生物機電系 林達德 Time.h (1 of 2) Set functions return reference to Time object to enable cascaded member function calls.
65
Outline 611 18200 計算機程式語言 Lecture 10-65 國立台灣大學生物機電系 林達德 Time.h (2 of 2)
66
Outline 611 18200 計算機程式語言 Lecture 10-66 國立台灣大學生物機電系 林達德 Time.cpp (1 of 3) Return *this as reference to enable cascaded member function calls.
67
Outline 611 18200 計算機程式語言 Lecture 10-67 國立台灣大學生物機電系 林達德 Time.cpp (2 of 3) Return *this as reference to enable cascaded member function calls.
68
Outline 611 18200 計算機程式語言 Lecture 10-68 國立台灣大學生物機電系 林達德 Time.cpp (3 of 3)
69
Outline 611 18200 計算機程式語言 Lecture 10-69 國立台灣大學生物機電系 林達德 fig10_20.cpp (1 of 2) Cascade member function calls; recall dot operator associates from left to right. Function call to printStandard must appear last; printStandard does not return reference to t.
70
Outline 611 18200 計算機程式語言 Lecture 10-70 國立台灣大學生物機電系 林達德 fig10_20.cpp (2 of 2)
71
611 18200 計算機程式語言 Lecture 10-71 國立台灣大學生物機電系 林達德 10.6 Dynamic Memory Management with Operators new and delete Dynamic memory management –Control allocation and deallocation of memory –Operators new and delete Include standard header –Access to standard version of new
72
611 18200 計算機程式語言 Lecture 10-72 國立台灣大學生物機電系 林達德 10.6 Dynamic Memory Management with Operators new and delete new –Consider Time *timePtr; timePtr = new Time; –new operator Creates object of proper size for type Time –Error if no space in memory for object Calls default constructor for object Returns pointer of specified type –Providing initializers double *ptr = new double( 3.14159 ); Time *timePtr = new Time( 12, 0, 0 ); –Allocating arrays int *gradesArray = new int[ 10 ];
73
611 18200 計算機程式語言 Lecture 10-73 國立台灣大學生物機電系 林達德 10.6 Dynamic Memory Management with Operators new and delete delete –Destroy dynamically allocated object and free space –Consider delete timePtr; –Operator delete Calls destructor for object Deallocates memory associated with object –Memory can be reused to allocate other objects –Deallocating arrays delete [] gradesArray; –Deallocates array to which gradesArray points If pointer to array of objects First calls destructor for each object in array Then deallocates memory
74
611 18200 計算機程式語言 Lecture 10-74 國立台灣大學生物機電系 林達德 10.6 Dynamic Memory Management with Operators new and delete Not releasing dynamically allocated memory when it is no longer needed can cause the system to run out of memory prematurely. This is sometimes called a “ memory leak. ” Common Programming Error 10.8
75
611 18200 計算機程式語言 Lecture 10-75 國立台灣大學生物機電系 林達德 10.6 Dynamic Memory Management with Operators new and delete Using delete instead of delete [] for arrays of objects can lead to runtime logic errors. To ensure that every object in the array receives a destructor call, always delete memory allocated as an array with operator delete []. Similarly, always delete memory allocated as an individual element with operator delete. Common Programming Error 10.9
76
611 18200 計算機程式語言 Lecture 10-76 國立台灣大學生物機電系 林達德 10.7 static Class Members static class variable –“Class-wide” data Property of class, not specific object of class –Efficient when single copy of data is enough Only the static variable has to be updated –May seem like global variables, but have class scope Only accessible to objects of same class –Initialized exactly once at file scope –Exist even if no objects of class exist –Can be public, private or protected
77
611 18200 計算機程式語言 Lecture 10-77 國立台灣大學生物機電系 林達德 10.7 static Class Members Accessing static class variables –Accessible through any object of class –public static variables Can also be accessed using binary scope resolution operator( :: ) Employee::count –private static variables When no class member objects exist –Can only be accessed via public static member function –To call public static member function combine class name, binary scope resolution operator ( ::) and function name Employee::getCount()
78
611 18200 計算機程式語言 Lecture 10-78 國立台灣大學生物機電系 林達德 10.7 static Class Members static member functions –C annot access non- static data or functions –No this pointer for static functions static data members and static member functions exist independent of objects
79
611 18200 計算機程式語言 Lecture 10-79 國立台灣大學生物機電系 林達德 10.7 static Class Members Use static data members to save storage when a single copy of the data for all objects of a class will suffice. Performance Tip 10.3
80
611 18200 計算機程式語言 Lecture 10-80 國立台灣大學生物機電系 林達德 10.7 static Class Members A class ’ s static data members and static member functions exist and can be used even if no objects of that class have been instantiated. Software Engineering Observation 10.10
81
611 18200 計算機程式語言 Lecture 10-81 國立台灣大學生物機電系 林達德 10.7 static Class Members It is a compilation error to include keyword static in the definition of a static data members at file scope. Common Programming Error 10.10
82
Outline 611 18200 計算機程式語言 Lecture 10-82 國立台灣大學生物機電系 林達德 fig10_21.cpp (1 of 1) static member function can only access static data members and member functions. static data member is class-wide data.
83
Outline 611 18200 計算機程式語言 Lecture 10-83 國立台灣大學生物機電系 林達德 Employee.cpp (1 of 3) Initialize static data member exactly once at file scope. static member function accesses static data member count.
84
Outline 611 18200 計算機程式語言 Lecture 10-84 國立台灣大學生物機電系 林達德 Employee.cpp (2 of 3) new operator dynamically allocates space. Use static data member to store total count of employees. Operator delete deallocates memory. Use static data member to store total count of employees.
85
Outline 611 18200 計算機程式語言 Lecture 10-85 國立台灣大學生物機電系 林達德 Employee.cpp (3 of 3)
86
Outline 611 18200 計算機程式語言 Lecture 10-86 國立台灣大學生物機電系 林達德 fig10_23.cpp (1 of 2) new operator dynamically allocates space. static member function can be invoked on any object of class.
87
Outline 611 18200 計算機程式語言 Lecture 10-87 國立台灣大學生物機電系 林達德 fig10_23.cpp (2 of 2) Operator delete deallocates memory. static member function invoked using binary scope resolution operator (no existing class objects).
88
611 18200 計算機程式語言 Lecture 10-88 國立台灣大學生物機電系 林達德 10.7 static Class Members Some organizations specify in their software engineering standards that all calls to static member functions be made using the class name and not an object handle. Software Engineering Observation 10.11
89
611 18200 計算機程式語言 Lecture 10-89 國立台灣大學生物機電系 林達德 10.7 static Class Members Using the this pointer in a static member function is a compilation error. Common Programming Error 10.11
90
611 18200 計算機程式語言 Lecture 10-90 國立台灣大學生物機電系 林達德 10.7 static Class Members Declaring a static member function const is a compilation error. The const qualifier indicates that a function cannot modify the contents of the object in which it operates, but static member functions exist and operate independently of any objects of the class. Common Programming Error 10.12
91
611 18200 計算機程式語言 Lecture 10-91 國立台灣大學生物機電系 林達德 10.7 static Class Members After deleting dynamically allocated memory, set the pointer that referred to that memory to 0. This disconnects the pointer from the previously allocated space on the free store. This space in memory could still contain information, despite having been deleted. By setting the pointer to 0, the program loses any access to that free-store space, which, in fact, could have already been reallocated for a different purpose. If you didn't set the pointer to 0, your code could inadvertently access this new information, causing extremely subtle, nonrepeatable logic errors. Error-Prevention Tip 10.2
92
611 18200 計算機程式語言 Lecture 10-92 國立台灣大學生物機電系 林達德 10.8Data Abstraction and Information Hiding Information hiding –Classes hide implementation details from clients –Example: stack data structure Data elements added (pushed) onto top Data elements removed (popped) from top Last-in, first-out (LIFO) data structure Client only wants LIFO data structure –Does not care how stack implemented Data abstraction –Describe functionality of class independent of implementation
93
611 18200 計算機程式語言 Lecture 10-93 國立台灣大學生物機電系 林達德 10.8Data Abstraction and Information Hiding Abstract data types (ADTs) –Approximations/models of real-world concepts and behaviors int, float are models for a numbers –Data representation –Operations allowed on those data C++ extensible –Standard data types cannot be changed, but new data types can be created
94
611 18200 計算機程式語言 Lecture 10-94 國立台灣大學生物機電系 林達德 10.8.1 Example: Array Abstract Data Type ADT array –Could include Subscript range checking Arbitrary range of subscripts –Instead of having to start with 0 Array assignment Array comparison Array input/output Arrays that know their sizes Arrays that expand dynamically to accommodate more elements
95
611 18200 計算機程式語言 Lecture 10-95 國立台灣大學生物機電系 林達德 10.8Data Abstraction and Information Hiding The programmer is able to create new types through the class mechanism. These new types can be designed to be used as conveniently as the built- in types. Thus, C++ is an extensible language. Although the language is easy to extend with these new types, the base language itself cannot be changed. Software Engineering Observation 10.12
96
611 18200 計算機程式語言 Lecture 10-96 國立台灣大學生物機電系 林達德 10.8.2 Example: String Abstract Data Type Strings in C++ –C++ does not provide built-in string data type Maximizes performance –Provides mechanisms for creating and implementing string abstract data type String ADT (Chapter 8) –ANSI/ISO standard string class (Chapter 19)
97
611 18200 計算機程式語言 Lecture 10-97 國立台灣大學生物機電系 林達德 10.8.3 Example: Queue Abstract Data Type Queue –FIFO First in, first out –Enqueue Put items in queue one at a time –Dequeue Remove items from queue one at a time Queue ADT –Implementation hidden from clients Clients may not manipulate data structure directly –Only queue member functions can access internal data –Queue ADT (Chapter 15) –Standard library queue class (Chapter 20)
98
611 18200 計算機程式語言 Lecture 10-98 國立台灣大學生物機電系 林達德 10.9Container Classes and Iterators Container classes (collection classes) –Designed to hold collections of objects –Common services Insertion, deletion, searching, sorting, or testing an item –Examples Arrays, stacks, queues, trees and linked lists Iterator objects (iterators) –Returns next item of collection Or performs some action on next item –Can have several iterators per container Book with multiple bookmarks –Each iterator maintains own “position” –Discussed further in Chapter 20
99
611 18200 計算機程式語言 Lecture 10-99 國立台灣大學生物機電系 林達德 10.10 Proxy Classes Proxy class –Hide implementation details of another class –Knows only public interface of class being hidden –Enables clients to use class’s services without giving access to class’s implementation Forward class declaration –Used when class definition only uses pointer to another class –Prevents need for including header file –Declares class before referencing –Format: class ClassToLoad;
100
Outline 611 18200 計算機程式語言 Lecture 10-100 國立台灣大學生物機電系 林達德 Implementation.h (1 of 1) public member function.
101
Outline 611 18200 計算機程式語言 Lecture 10-101 國立台灣大學生物機電系 林達德 Interface.h (1 of 1) Provide same public interface as class Implementation ; recall setValue and getValue only public member functions. Pointer to Implementation object requires forward class declaration.
102
Outline 611 18200 計算機程式語言 Lecture 10-102 國立台灣大學生物機電系 林達德 Interface.cpp (1 of 1) Proxy class Interface includes header file for class Implementation. Maintain pointer to underlying Implementation object. Invoke corresponding function on underlying Implementation object. Deallocate underlying Implementation object.
103
611 18200 計算機程式語言 Lecture 10-103 國立台灣大學生物機電系 林達德 10.10 Proxy Classes A proxy class insulates client code from implementation changes. Software Engineering Observation 10.13
104
Outline 611 18200 計算機程式語言 Lecture 10-104 國立台灣大學生物機電系 林達德 fig10_27.cpp (1 of 1) Only include proxy class header file. Create object of proxy class Interface ; note no mention of Implementation class. Invoke member functions via proxy class object.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.