Programming Techniques Classes II Important Class Features Spring 2009.

Slides:



Advertisements
Similar presentations
 2008 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Advertisements

Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
計算機程式語言 Lecture 10-1 國立台灣大學生物機電系 林達德 10 Classes: A Deeper Look, Part 2.
Class and Objects.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Classes: A Deeper Look Systems Programming.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Chapter 15: Operator Overloading
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4 - Classes Jan 27,
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Data Structures Using C++ 2E
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Classes: A Deeper Look, Part But what, to serve our private ends, Forbids the cheating of our friends? ◦ Charles Churchill Instead of this absurd.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 17 - C++ Classes: Part II Outline 17.1Introduction.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Chapter 17 - C++ Classes: Part II Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 CSC241: Object Oriented Programming Lecture No 02.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 CSC241: Object Oriented Programming Lecture No 05.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 CSC241: Object Oriented Programming Lecture No 03.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Lecture 5: Classes (continued) Feb 1, 2005
CSC241: Object Oriented Programming
Chapter 17 - C++ Classes: Part II
10.2 const (Constant) Objects and const Member Functions
7.4 friend Functions and friend Classes
Classes: A Deeper Look, Part 2
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Classes: A Deeper Look, Part 2
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Presentation transcript:

Programming Techniques Classes II Important Class Features Spring 2009

References C++ How to Program, C++ How to Program, Deitel, Prentice Hall, 2003 by Pearson Education, Fourth Edition Deitel, Prentice Hall, 2003 by Pearson Education, Fourth Edition Object Oriented Programming in C++, Object Oriented Programming in C++, Robert Lafore, Waite Group Press, 1995, Second Edition

Classes II Agenda: Agenda: 1. const objects and const member functions 2. friend functions and friend classes 3. Composition: Objects as Members of Classes 4. Dynamic Memory Management with Operators new and delete 5. this pointer 6. static data members and static functions 7. The Container class 8. The iterator class

1. const objects and const member functions Keyword const Keyword const Specify object not modifiable Specify object not modifiable Compiler error if attempt to modify const object Compiler error if attempt to modify const object Example Example const Time noon( 12, 0, 0 ); const Time noon( 12, 0, 0 ); Declares const object noon of class Time Declares const object noon of class Time Initializes to 12 Initializes to 12

1. const objects and const member functions const member functions const member functions Member functions for const objects must also be const Member functions for const objects must also be const Cannot modify object Cannot modify object Specify const in both prototype and definition Specify const in both prototype and definition in Prototype: in Prototype:  After parameter list in Definition in Definition  Before beginning left brace  Constructors and destructors Cannot be const as they Must be able to modify objects

1. const objects and const member functions Example 1 Example 1

1. const objects and const member functions Example 1 Example 1

1. const objects and const member functions Example 1 Example 1 const functions do not modify objects.

1. const objects and const member functions Example 1 Example 1 Declare noon a const object. Note that non- const constructor can initialize const object.

1. const objects and const member functions Example 1 Example 1 Attempting to invoke non- const member function on const object results in compiler error. Attempting to invoke non- const member function on const object results in compiler error even if function does not modify object.

1. const objects and const member functions Const member functions Const member functions Can be used for: Can be used for: All class objects All class objects Must be used for: Must be used for: Const class objects Const class objects

1. const objects and const member functions Member initializer syntax Member initializer syntax Can be used for: Can be used for: All data members All data members Must be used for: Must be used for: const data members const data members Data members that are references Data members that are references

1. const objects and const member functions Example 2 Example 2

1. const objects and const member functions Example 2 Example 2

1. const objects and const member functions Example 2 Example 2

Classes II Agenda: Agenda: 1. const objects and const member functions 2. friend functions and friend classes 3. Composition: Objects as Members of Classes 4. Dynamic Memory Management with Operators new and delete 5. this pointer 6. static data members and static functions 7. The Container class 8. The iterator class

2. friend functions and friend classes friend functions: friend functions: are defined outside class ’ s scope are defined outside class ’ s scope they have the right to access non-public members they have the right to access non-public members

2. friend functions and friend classes Declaring friends Declaring friends A function F ( ) : by preceding function prototype with keyword friend inside class A function F ( ) : by preceding function prototype with keyword friend inside class A class “ classTwo ” as friend of another class “ class One ” : by placing declaration of form: A class “ classTwo ” as friend of another class “ class One ” : by placing declaration of form: Example: Example: friend F ( ); friend F ( ); friend class ClassTwo; friend class ClassTwo; inside ClassOne definition inside ClassOne definition

2. friend functions and friend classes Properties of friendship Properties of friendship  Friendship granted, not taken i.e. Class B friend of class A Class B friend of class A Class A must explicitly declare class B friend Class A must explicitly declare class B friend  Not symmetric Class B friend of class A Class B friend of class A Class A not necessarily friend of class B Class A not necessarily friend of class B

2. friend functions and friend classes Properties of friendship (cont.) Properties of friendship (cont.)  Not transitive Class A friend of class B Class A friend of class B Class B friend of class C Class B friend of class C Class A not necessarily friend of Class C Class A not necessarily friend of Class Cfriend

2. friend functions and friend classes Example 1: Example 1:

2. friend functions and friend classes Example 1: Example 1: NOT a member function!

2. friend functions and friend classes Example 1: Example 1:

2. friend functions and friend classes Example 2: A global function as friend Example 2: A global function as friend class Distance // English Distance class { friend Distance Add_Dist (Distance, Distance); private: private: int feet; float inches; int feet; float inches; public: public: Distance ( ) { } // constructor (no args) Distance ( ) { } // constructor (no args) Distance (int ft, float in); Distance (int ft, float in);

2. friend functions and friend classes Example 2: function implementation Example 2: function implementation Distance Add_Dist(Distance d2, Distance d3) { float inches = d2.inches + d3.inches; int feet = 0; float inches = d2.inches + d3.inches; int feet = 0; if (inches >= 12.0) if (inches >= 12.0) { inches -= 12.0; feet++; inches -= 12.0; feet++;} feet += d2.feet + d3.feet; // add the feet feet += d2.feet + d3.feet; // add the feet return Distance (feet, inches); } return Distance (feet, inches); } Non-member function! Creating a member and returning its value!

2. friend functions and friend classes Example 2: Example 2: main ( ) { Distance d1 (7, 5), d2 (3, 4); Distance d1 (7, 5), d2 (3, 4); Distance d3; Distance d3; d3 = Add_Dist ( d1, d2); d3 = Add_Dist ( d1, d2);} How was it in the previous example of Add_dist? Compare!

2. friend functions and friend classes Example 3: class as a friend of another class Example 3: class as a friend of another class class X { int a, b; int a, b; friend class F; friend class F;public: X(int i=1, int j =2 ) : a(i), b(j) { } X(int i=1, int j =2 ) : a(i), b(j) { }};

2. friend functions and friend classes Example 3: class as a friend of another class Example 3: class as a friend of another class class F { public: void print( X& x ) { void print( X& x ) { cout << "a is " << x. a << endl; cout << "a is " << x. a << endl; cout << "b is " << x. b << endl; cout << "b is " << x. b << endl; }};

2. friend functions and friend classes Example 3: class as a friend of another class Example 3: class as a friend of another class int main( ) { X xobj; X xobj; F fobj; F fobj; fobj. Print ( xobj ); fobj. Print ( xobj );} Another alternative was to make only F::Print ( ) a friend to class X

Classes II Agenda: Agenda: 1. const objects and const member functions 2. friend functions and friend classes 3. Composition: Objects as Members of Classes 4. Dynamic Memory Management with Operators new and delete 5. this pointer 6. static data members and static functions 7. The Container class 8. The iterator class

3. Composition: Objects as Members of Classes Composition: an object inside another one ==> very strong automatic relationship Composition: an object inside another one ==> very strong automatic relationship Aggregation: an object inside another one, also. If implementation is done using pointers, then the outer object (may or may not be) is responsible for const and destruc. of inner objects. Aggregation: an object inside another one, also. If implementation is done using pointers, then the outer object (may or may not be) is responsible for const and destruc. of inner objects. Association: an object with pointer to another object(s). Outer object is not resp. for const or destruc of the other object. Association: an object with pointer to another object(s). Outer object is not resp. for const or destruc of the other object.

3. Composition: Objects as Members of Classes Composition versus Aggregation: Composition versus Aggregation: In both we talk about objects of one class declared inside another class. In both we talk about objects of one class declared inside another class. Aggregation differs from ordinary composition in that it pointers may be used, while in composition they are automatic. Aggregation differs from ordinary composition in that it pointers may be used, while in composition they are automatic. In both, when the owning object is destroyed, so are the contained objects. In both, when the owning object is destroyed, so are the contained objects.

3. Composition: Objects as Members of Classes Composition versus Aggregation Composition versus AggregationExample: A university owns various departments (e.g., chemistry) A university owns various departments (e.g., chemistry) Composition: If departments are implemented as static array Composition: If departments are implemented as static array Aggregation: If departments are implemented as pointers Aggregation: If departments are implemented as pointers

3. Composition: Objects as Members of Classes What about Association ? What about Association ? An object with pointer to another object(s). Outer object is NOT resp. for const or destruc of the other object. An object with pointer to another object(s). Outer object is NOT resp. for const or destruc of the other object.Example: Association: Each department has a number of professors. Association: Each department has a number of professors. When university is deleted, professors are not deleted ( Refer to website notes) When university is deleted, professors are not deleted ( Refer to website notes)

Example

3. Composition: Objects as Members of Classes Construction of member objects Construction of member objects Member objects are constructed in order of declaration Member objects are constructed in order of declaration Member objects are constructed before the enclosing outer class objects (host objects) Member objects are constructed before the enclosing outer class objects (host objects)

3. Composition: Objects as Members of Classes Example: Example:

3. Composition: Objects as Members of Classes Example: Example:

3. Composition: Objects as Members of Classes

Example: Example:

3. Composition: Objects as Members of Classes Example: Example:

3. Composition: Objects as Members of Classes Example: Example:

3. Composition: Objects as Members of Classes Example: Example:

3. Composition: Objects as Members of Classes Example: Example: Create Date objects to pass to Employee constructor.

3. Composition: Objects as Members of Classes

Aggregation: As opposed to composition Aggregation: As opposed to composition class Sales_info class Sales_info { float amount; Date ddmmyy; Date ddmmyy;…………};

3. Composition: Objects as Members of Classes Aggregation: As opposed to composition Aggregation: As opposed to composition class Sales_Person { Sales_info * sales; …………public: Sales_Person (int num_sales = 10); ~Sales_Person ( ); };

3. Composition: Objects as Members of Classes Aggregation: As opposed to composition Aggregation: As opposed to composition Sales_Person :: SalesPerson ( int c = 10) Sales_Person :: SalesPerson ( int c = 10){ sales = new sales_info [ c ]; sales = new sales_info [ c ];} Sales_Person :: ~Sales_Person ( ) { delete [ ] Sales; delete [ ] Sales;}

3. Composition: Objects as Members of Classes Association: As opposed to composition and aggregation Association: As opposed to composition and aggregation class Prof;// forward declaration class Course { char course_id [ 8 ]; ….. Prof * prof; public: Course ( char* id, ….., Prof* p); ~ Course ( ); }

3. Composition: Objects as Members of Classes Association: As opposed to composition and aggregation Association: As opposed to composition and aggregation Course :: Course ( char* id, ….., Prof* p) { ………. prof = p; } ~ Course ( ) { // No deletion of prof pointer!! // He is still allowed to exist! ! // He is still allowed to exist! !}

Classes II Agenda: Agenda: 1. const objects and const member functions 2. friend functions and friend classes 3. Composition: Objects as Members of Classes 4. Dynamic Memory Management with Operators new and delete 5. this pointer 6. static data members and static functions 7. The Container class 8. The iterator class

4. Dynamic memory management Same operators: new and delete Same operators: new and delete new operator new operator e.g.: e.g.: Time *timePtr; timePtr = new Time; timePtr = new Time; Creates object of proper size for type Time Creates object of proper size for type Time Calls default constructor for object Calls default constructor for object Error if no space in memory for object Error if no space in memory for object Returns pointer of specified type Returns pointer of specified type

4. Dynamic memory management Providing initializers Providing initializers double *ptr = new double( ); double *ptr = new double( ); Time *timePtr = new Time( 12, 0, 0 ); Time *timePtr = new Time( 12, 0, 0 ); Allocating arrays Allocating arrays int *gradesArray = new int[ 10 ]; int *gradesArray = new int[ 10 ]; Time *timesArray = new Time[5]; Time *timesArray = new Time[5];

4. Dynamic memory management delete delete Destroy dynamically allocated object and free space Destroy dynamically allocated object and free space e.g.: e.g.: delete timePtr; Operator delete Operator delete Calls destructor for object Calls destructor for object Deallocates memory associated with object Deallocates memory associated with object Memory can be reused to allocate other objects Memory can be reused to allocate other objects

4. Dynamic memory management Deallocating arrays Deallocating arrays e.g.: e.g.: delete [ ] gradesArray; Deallocates array to which gradesArray points Deallocates array to which gradesArray points If pointer to array of objects If pointer to array of objects First calls destructor for each object in array First calls destructor for each object in array Then deallocates memory Then deallocates memory

Classes II Agenda: Agenda: 1. const objects and const member functions 2. friend functions and friend classes 3. Composition: Objects as Members of Classes 4. Dynamic Memory Management with Operators new and delete 5. this pointer 6. static data members and static functions 7. The Container class 8. The iterator class

5. this Pointer Allows object to access own address Allows object to access own address Not part of object itself Not part of object itself Implicit argument to non-static member function call Implicit argument to non-static member function call Implicitly reference member data and functions Implicitly reference member data and functions Type of this pointer depends on Type of object Type of this pointer depends on Type of object

this Pointer 5. this Pointer Type of this pointer depends on whether member function is const or not: Type of this pointer depends on whether member function is const or not: In non-const member function of Employee this has type: Employee * const In non-const member function of Employee this has type: Employee * const i.e. Constant pointer to non-constant Employee object In const member function of Employee this has type: const Employee * const In const member function of Employee this has type: const Employee * const i.e. Constant pointer to constant Employee object

5. this Pointer Example: Example:

5. this Pointer Example: Example: 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.

5. this Pointer Cascaded member function calls are possible using this! Cascaded member function calls are possible using this! Multiple functions invoked in same statement (Casacded member function calls) Multiple functions invoked in same statement (Casacded member function calls) Function returns reference pointer to same object { return *this; } Function returns reference pointer to same object { return *this; } Other functions operate on that pointer Other functions operate on that pointer Functions that do not return references must be called last Functions that do not return references must be called last Will be explained more in operator moverloading!

5. this Pointer Example: Example: Cascade member function calls; recall dot operator associates from left to right. What is the return value of setHour, setMinute and setSecond?

Classes II Agenda: Agenda: 1. const objects and const member functions 2. friend functions and friend classes 3. Composition: Objects as Members of Classes 4. Dynamic Memory Management with Operators new and delete 5. this pointer 6. static data members and static functions 7. The Container class 8. The iterator class

6. static data members and static functions Static class variables are accessible through any object of class Static class variables are accessible through any object of class public static variables can also be accessed using binary scope resolution operator(::) public static variables can also be accessed using binary scope resolution operator(::) Employee :: count private static variables can only be accessed via public static member function (even when no class member objects exist) private static variables can only be accessed via public static member function (even when no class member objects exist)

6. static data members and static functions To call public static member function combine class name with binary scope resolution operator (::) and function name To call public static member function combine class name with binary scope resolution operator (::) and function name Employee :: getCount( )

6. static data members and static functions static member functions and static data members exist independent of objects, hence: static member functions and static data members exist independent of objects, hence: Static member functions cannot access non-static data or functions Static member functions cannot access non-static data or functions No this pointer for static functions No this pointer for static functions

6. static data members and static functions Example: Example: Compare with previous declaration for Name strings!

6. static data members and static functions in.cpp file in.cpp file Initialize static data member exactly once at file scope. static member function accesses static data member count.

6. static data members and static functions constructor in.cpp file constructor in.cpp file

6. static data members and static functions.cpp file:.cpp file: Operator delete deallocates memory. Use static data member to store total count of employees.

6. static data members and static functions In main In main new operator dynamically allocates space. static member function can be invoked on any object of class.

6. static data members and static functions main (cont.) main (cont.) Operator delete deallocates memory. static member function invoked using binary scope resolution operator (no existing class objects).

6. static data members and static functions Output: Output:

Classes II Agenda: Agenda: 1. const objects and const member functions 2. friend functions and friend classes 3. Composition: Objects as Members of Classes 4. Dynamic Memory Management with Operators new and delete 5. this pointer 6. static data members and static functions 7. The Container class and the iterator class 8. Proxy classes and forward declaration

7. The Container class and the iterator class Composition that is used to store several instances of the composited data type is referred to as containment. Composition that is used to store several instances of the composited data type is referred to as containment. Examples are: Examples are: arrays arrays linked lists linked lists binary trees …. binary trees ….

7. The Container class and the iterator class These are classes that hide implemen- tation details from clients These are classes that hide implemen- tation details from clients Example: stack data structure: Client only wants LIFO data structure, he does not care how stack implemented Example: stack data structure: Client only wants LIFO data structure, he does not care how stack implemented Data elements added (pushed) onto top Data elements added (pushed) onto top Data elements removed (popped) from top Data elements removed (popped) from top Last-in, first-out (LIFO) data structure Last-in, first-out (LIFO) data structure

7. The Container class and the iterator class Example 1: An array abstract data type could include Example 1: An array abstract data type could include Subscript range checking Subscript range checking Arbitrary range of subscripts instead of having to start with 0 Arbitrary range of subscripts instead of having to start with 0 Array assignment Array assignment Array comparison Array comparison Arrays that know their sizes Arrays that know their sizes Arrays that expand dynamically to accommodate more elements Arrays that expand dynamically to accommodate more elements

7. The Container class and the iterator class Example 2: Queue Abstract Data Type Example 2: Queue Abstract Data Type First in, first out (FIFO) First in, first out (FIFO) Enqueue: Put items in queue one at a time Enqueue: Put items in queue one at a time Dequeue: Remove items from queue one at a time Dequeue: Remove items from queue one at a time

7. The Container class and the iterator class Queue ADT Queue ADT Implementation hidden from clients Implementation hidden from clients Clients may not manipulate data structure directly Clients may not manipulate data structure directly Only queue member functions can access internal data Only queue member functions can access internal data

7. The Container class and the iterator class C++ provides mechanisms for creating and implementing abstract data type

7. The Container class and the iterator class Container classes (collection classes) are designed to hold collections of objects Container classes (collection classes) are designed to hold collections of objects Common services: Common services: Insertion, deletion, searching, sorting, or testing an item Insertion, deletion, searching, sorting, or testing an item Examples: Examples: Arrays, stacks, queues, trees and linked lists Arrays, stacks, queues, trees and linked lists

7. The Container class and the iterator class Iterator objects (iterators) Iterator objects (iterators) Returns next item of collection or performs some action on next item Returns next item of collection or performs some action on next item Can have several iterators per container e.g. Can have several iterators per container e.g. Book with multiple bookmarks Book with multiple bookmarks Each iterator maintains own “ position ” Each iterator maintains own “ position ” Discussed further in Chapter 20 Discussed further in Chapter 20

Classes II Agenda: Agenda: 1. const objects and const member functions 2. friend functions and friend classes 3. Composition: Objects as Members of Classes 4. Dynamic Memory Management with Operators new and delete 5. this pointer 6. static data members and static functions 7. The Container class and the iterator class 8. Proxy classes and forward declaration

Proxy class Proxy class Hide implementation details of another class Hide implementation details of another class Knows only public interface of class being hidden Knows only public interface of class being hidden Enables clients to use class ’ s services without giving access to class ’ s implementation Enables clients to use class ’ s services without giving access to class ’ s implementation

8. Proxy classes and forward declaration Example: class implementation Example: class implementation

8. Proxy classes and forward declaration class implementation (cont.) class implementation (cont.)

8. Proxy classes and forward declaration The Implementation class The Implementation class Provide same public interface as class Implementation ; recall setValue and getValue only public member functions. Pointer to Implementation object requires forward class declaration.

8. Proxy classes and forward declaration The interface implementation The interface implementation Proxy class Interface includes header file for class Implementation. Maintain pointer to underlying Implementation object. Invoke corresponding function on underlying Implementation object.

8. Proxy classes and forward declaration The interface implementation(cont.) The interface implementation(cont.) Invoke corresponding function on underlying Implementation object. Deallocate underlying Implementation object.

8. Proxy classes and forward declaration main ( ) main ( ) 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.