1 Chapter 7 Classes Part II Outline 7

  • Slides: 79
Download presentation
1 Chapter 7: Classes Part II Outline 7. 1 7. 2 7. 3 7.

1 Chapter 7: Classes Part II Outline 7. 1 7. 2 7. 3 7. 4 7. 5 7. 6 7. 7 7. 8 7. 9 7. 10 Introduction const (Constant) Objects and const Member Functions Composition: Objects as Members of Classes friend Functions and friend Classes Using the this Pointer Dynamic Memory Management with Operators new and delete static Class Members Data Abstraction and Information Hiding 7. 8. 1 Example: Array Abstract Data Type 7. 8. 2 Example: String Abstract Data Type 7. 8. 3 Example: Queue Abstract Data Type Container Classes and Iterators Proxy Classes 2003 Prentice Hall, Inc. All rights reserved.

2 7. 1 Introduction • Classes • Data abstraction • Object-based programming (OBP) –

2 7. 1 Introduction • Classes • Data abstraction • Object-based programming (OBP) – Chapters 6 -8 • Inheritance and polymorphism – Chapters 9 and 10 2003 Prentice Hall, Inc. All rights reserved.

7. 2 const (Constant) Objects and const Member Functions • Principle of least privilege

7. 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 2003 Prentice Hall, Inc. All rights reserved. 3

7. 2 const (Constant) Objects and const Member Functions • const member functions –

7. 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 2003 Prentice Hall, Inc. All rights reserved. 4

7. 2 const (Constant) Objects and const Member Functions • Constructors and destructors –

7. 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 2003 Prentice Hall, Inc. All rights reserved. 5

1 2 3 4 5 // Fig. 7. 1: time 5. h // Definition

1 2 3 4 5 // Fig. 7. 1: time 5. h // Definition of class Time. // Member functions defined in time 5. cpp. #ifndef TIME 5_H #define TIME 5_H 6 7 class Time { 8 9 10 public: Time( int = 0, int = 0 ); Outline // default constructor 11 12 13 14 15 16 // set functions void set. Time( int, int ); void set. Hour( int ); void set. Minute( int ); void set. Second( int ); 17 18 19 20 21 // get functions (normally declared const) int get. Hour() const; // return hour int get. Minute() const; // return minute int get. Second() const; // return second 22 23 24 25 // print functions (normally declared const) void print. Universal() const; // print universal time void print. Standard(); // print standard time // // set set time hour minute second time 5. h (1 of 2) Declare const get functions. Declare const function print. Universal. 2003 Prentice Hall, Inc. All rights reserved. 6

26 27 28 29 30 private: int hour; int minute; int second; 31 32

26 27 28 29 30 private: int hour; int minute; int second; 31 32 }; // end class Time 33 34 #endif Outline // 0 - 23 (24 -hour clock format) // 0 - 59 time 5. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 7

1 2 3 // Fig. 7. 2: time 5. cpp // Member-function definitions for

1 2 3 // Fig. 7. 2: time 5. cpp // Member-function definitions for class Time. #include <iostream> 4 5 using std: : cout; 6 7 #include <iomanip> 8 9 10 using std: : setfill; using std: : setw; 11 12 13 // include definition of class Time from time 5. h #include "time 5. h" 14 15 16 17 18 19 20 // constructor function to initialize private data; // calls member function set. Time to set variables; // default values are 0 (see class definition) Time: : Time( int hour, int minute, int second ) { set. Time( hour, minute, second ); 21 22 } // end Time constructor Outline time 5. cpp (1 of 4) 23 2003 Prentice Hall, Inc. All rights reserved. 8

24 25 26 27 28 29 // set hour, minute and second values void

24 25 26 27 28 29 // set hour, minute and second values void Time: : set. Time( int hour, int minute, int second ) { set. Hour( hour ); set. Minute( minute ); set. Second( second ); 30 31 } // end function set. Time 32 33 34 35 36 // set hour value void Time: : set. Hour( int h ) { hour = ( h >= 0 && h < 24 ) ? h : 0; 37 38 } // end function set. Hour 39 40 41 42 43 // set minute value void Time: : set. Minute( int m ) { minute = ( m >= 0 && m < 60 ) ? m : 0; 44 45 } // end function set. Minute Outline time 5. cpp (2 of 4) 46 2003 Prentice Hall, Inc. All rights reserved. 9

47 48 49 50 // set second value void Time: : set. Second( int

47 48 49 50 // set second value void Time: : set. Second( int s ) { second = ( s >= 0 && s < 60 ) ? s : 0; 51 52 } // end function set. Second 53 54 55 56 57 // return hour value int Time: : get. Hour() const { return hour; 58 59 } // end function get. Hour 60 61 62 63 64 // return minute value int Time: : get. Minute() const { return minute; 65 66 } // end function get. Minute Outline const functions do not modify objects. time 5. cpp (3 of 4) 67 2003 Prentice Hall, Inc. All rights reserved. 10

68 69 70 71 72 73 // return second value int Time: : get.

68 69 70 71 72 73 // return second value int Time: : get. Second() const { return second; } // end function get. Second Outline const functions do not modify objects. 74 75 76 77 78 79 80 // print Time in universal format void Time: : print. Universal() const { cout << setfill( '0' ) << setw( 2 ) << hour << ": " << setw( 2 ) << minute << ": " << setw( 2 ) << second; 81 82 } // end function print. Universal 83 84 85 86 87 88 89 90 // print Time in standard format void Time: : print. Standard() // note lack of const declaration { cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ": " << setfill( '0' ) << setw( 2 ) << minute << ": " << setw( 2 ) << second << ( hour < 12 ? " AM" : " PM" ); 91 92 } // end function print. Standard time 5. cpp (4 of 4) 2003 Prentice Hall, Inc. All rights reserved. 11

1 2 3 // Fig. 7. 3: fig 07_03. cpp // Attempting to access

1 2 3 // Fig. 7. 3: fig 07_03. cpp // Attempting to access a const object with // non-const member functions. 4 5 6 // include Time class definition from time 5. h #include "time 5. h" 7 8 9 10 11 int main() { Time wake. Up( 6, 45, 0 ); const Time noon( 12, 0, 0 ); Outline Declare noon a const object. // non-constant object // constant object 12 Note that non-constructor can initialize const object. fig 07_03. cpp (1 of 2) 2003 Prentice Hall, Inc. All rights reserved. 12

13 14 wake. Up. set. Hour( 18 ); // OBJECT // non-const MEMBER FUNCTION

13 14 wake. Up. set. Hour( 18 ); // OBJECT // non-const MEMBER FUNCTION non-const 15 16 noon. set. Hour( 12 ); // const non-const 17 18 wake. Up. get. Hour(); // non-const 19 20 21 noon. get. Minute(); // const noon. print. Universal(); // const 22 23 noon. print. Standard(); 24 25 return 0; // const Attempting to invoke nonmember function on const object results in compiler error. non-const Outline const Attempting to invoke non} // end main const member function on const object results in d: cpphtp 4_examplesch 07fig 07_01. cpp(16) : error C 2662: compiler error even if 'set. Hour' : cannot convert 'this' pointer from 'const class Time' function does not modify to 'class Time &' object. Conversion loses qualifiers 26 27 fig 07_03. cpp (2 of 2) fig 07_03. cpp output (1 of 1) d: cpphtp 4_examplesch 07fig 07_01. cpp(23) : error C 2662: 'print. Standard' : cannot convert 'this' pointer from 'const class Time' to 'class Time &' Conversion loses qualifiers 2003 Prentice Hall, Inc. All rights reserved. 13

7. 2 const (Constant) Objects and const Member Functions • Member initializer syntax –

7. 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 2003 Prentice Hall, Inc. All rights reserved. 14

1 2 3 4 // Fig. 7. 4: fig 07_04. cpp // Using a

1 2 3 4 // Fig. 7. 4: fig 07_04. cpp // Using a member initializer to initialize a // constant of a built-in data type. #include <iostream> 5 6 7 using std: : cout; using std: : endl; 8 9 class Increment { 10 11 12 public: Increment( int c = 0, int i = 1 ); 13 14 15 16 void add. Increment() { count += increment; 17 18 } // end function add. Increment 19 20 void print() const; // default constructor Outline fig 07_04. cpp (1 of 3) // prints count and increment 21 2003 Prentice Hall, Inc. All rights reserved. 15

22 23 24 private: int count; const increment; 25 26 }; // end class

22 23 24 private: int count; const increment; 25 26 }; // end class Increment 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 Outline // const data member Member initializer list Declare increment as const datalist member. separated from parameter // constructor Member initializer syntax can Increment: : Increment(byint c, int i ) colon. be used for non-const data Member initializermember syntax : count( c ), // initializer for non-const member must count. be used for const data increment( i ) // required initializer for const member { member increment. // empty body } // end Increment constructor Member initializer consists of data member name (increment) followed by // print count and increment values void Increment: : print() const parentheses containing initial { value (c). fig 07_04. cpp (2 of 3) cout << "count = " << count << ", increment = " << increment << endl; } // end function print 44 2003 Prentice Hall, Inc. All rights reserved. 16

45 46 47 int main() { Increment value( 10, 5 ); Outline 48 49

45 46 47 int main() { Increment value( 10, 5 ); Outline 48 49 50 cout << "Before incrementing: "; value. print(); 51 52 53 54 55 56 for ( int j = 0; j < 3; j++ ) { value. add. Increment(); cout << "After increment " << j + 1 << ": "; value. print(); } 57 58 return 0; 59 60 fig 07_04. cpp output (1 of 1) } // end main Before incrementing: count After increment 1: count = After increment 2: count = After increment 3: count = fig 07_04. cpp (3 of 3) = 10, increment 15, increment = 20, increment = 25, increment = = 5 5 2003 Prentice Hall, Inc. All rights reserved. 17

1 2 3 4 // Fig. 7. 5: fig 07_05. cpp // Attempting to

1 2 3 4 // Fig. 7. 5: fig 07_05. cpp // Attempting to initialize a constant of // a built-in data type with an assignment. #include <iostream> 5 6 7 using std: : cout; using std: : endl; 8 9 class Increment { 10 11 12 public: Increment( int c = 0, int i = 1 ); 13 14 15 16 void add. Increment() { count += increment; 17 18 } // end function add. Increment 19 20 void print() const; // default constructor Outline fig 07_05. cpp (1 of 3) // prints count and increment 21 2003 Prentice Hall, Inc. All rights reserved. 18

22 23 24 private: int count; const increment; 25 26 }; // end class

22 23 24 private: int count; const increment; 25 26 }; // end class Increment 27 28 29 30 31 32 33 34 Outline // const data member Declare increment as const data member. // constructor Attempting to modify const Increment: : Increment( int c, int i ) data memberisincrement { // Constant member 'increment' not initialized count = c; // allowed because count is not constant results in error. increment = i; // ERROR: Cannot modify a const object } // end Increment constructor 35 36 37 38 39 40 // print count and increment values void Increment: : print() const { cout << "count = " << count << ", increment = " << increment << endl; 41 42 } // end function print fig 07_05. cpp (2 of 3) 43 2003 Prentice Hall, Inc. All rights reserved. 19

44 45 46 int main() { Increment value( 10, 5 ); Outline 47 48

44 45 46 int main() { Increment value( 10, 5 ); Outline 47 48 49 cout << "Before incrementing: "; value. print(); 50 51 52 53 54 55 for ( int j = 0; j < 3; j++ ) { value. add. Increment(); cout << "After increment " << j + 1 << ": "; value. print(); } 56 57 return 0; 58 59 } // end main Not using member initializer syntax to initialize const data member increment results in error. fig 07_05. cpp (3 of 3) fig 07_05. cpp output (1 of 1) D: cpphtp 4_examplesch 07Fig 07_03. cpp(30) : error C 2758: 'increment' : must be initialized in constructor base/member Attempting to modify const initializer list data member increment D: cpphtp 4_examplesch 07Fig 07_03. cpp(24) : results in error. see declaration of 'increment' D: cpphtp 4_examplesch 07Fig 07_03. cpp(32) : error C 2166: l-value specifies const object 2003 Prentice Hall, Inc. All rights reserved. 20

7. 3 Composition: Objects as Members of Classes • Composition – Class has objects

7. 3 Composition: 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) 2003 Prentice Hall, Inc. All rights reserved. 21

1 2 3 4 5 // Fig. 7. 6: date 1. h // Date

1 2 3 4 5 // Fig. 7. 6: date 1. h // Date class definition. // Member functions defined in date 1. cpp #ifndef DATE 1_H #define DATE 1_H 6 7 class Date { 8 9 10 11 12 public: Date( int = 1, int = void print() const; // print ~Date(); // provided to confirm destruction order 13 14 15 16 17 private: int month; int day; int year; 18 19 20 Outline Note no constructor with parameter of type Date. Recall provides 1900 ); compiler // default constructor default copy constructor. format date in month/day/year date 1. h (1 of 1) // 1 -12 (January-December) // 1 -31 based on month // any year // utility function to test proper day for month and year int check. Day( int ) const; 21 22 }; // end class Date 23 24 #endif 2003 Prentice Hall, Inc. All rights reserved. 22

1 2 3 // Fig. 7. 7: date 1. cpp // Member-function definitions for

1 2 3 // Fig. 7. 7: date 1. cpp // Member-function definitions for class Date. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 9 // include Date class definition from date 1. h #include "date 1. h" 10 11 12 13 14 15 16 // constructor confirms proper value for month; calls // utility function check. Day to confirm proper value for day Date: : Date( int mn, int dy, int yr ) { if ( mn > 0 && mn <= 12 ) // validate the month = mn; 17 18 19 20 21 else { // invalid month set to 1 month = 1; cout << "Month " << mn << " invalid. Set to month 1. n" ; } 22 23 24 year = yr; day = check. Day( dy ); Outline date 1. cpp (1 of 3) // should validate yr // validate the day 25 2003 Prentice Hall, Inc. All rights reserved. 23

26 27 28 29 // output Date object to show when its constructor is

26 27 28 29 // output Date object to show when its constructor is called cout << "Date object constructor for date " ; print(); cout << endl; 30 31 } // end Date constructor 32 33 34 35 36 // print Date object in form void Date: : print() const { cout << month << '/' << day << '/' << year; 37 38 } // end function print 39 40 41 42 43 44 45 46 47 Outline No arguments; each member Output to show timing of function contains implicit constructors. handle to object on which it month/day/year operates. date 1. cpp (2 of 3) to show // output Date object to show when its. Output destructor is timing calledof Date: : ~Date() destructors. { cout << "Date object destructor for date " ; print(); cout << endl; } // end destructor ~Date 48 2003 Prentice Hall, Inc. All rights reserved. 24

49 50 51 52 53 54 // utility function to confirm proper day value

49 50 51 52 53 54 // utility function to confirm proper day value based on // month and year; handles leap years, too int Date: : check. Day( int test. Day ) const { static const int days. Per. Month[ 13 ] = { 0, 31, 28, 31, 30, 31 }; 55 56 57 58 // determine whether test. Day is valid for specified month if ( test. Day > 0 && test. Day <= days. Per. Month[ month ] ) return test. Day; 59 60 61 62 63 64 // February 29 check for leap year if ( month == 2 && test. Day == 29 && ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) ) return test. Day; 65 66 cout << "Day " << test. Day << " invalid. Set to day 1. n" ; 67 68 return 1; 69 70 Outline date 1. cpp (3 of 3) // leave object in consistent state if bad value } // end function check. Day 2003 Prentice Hall, Inc. All rights reserved. 25

1 2 3 4 5 // Fig. 7. 8: employee 1. h // Employee

1 2 3 4 5 // Fig. 7. 8: employee 1. h // Employee class definition. // Member functions defined in employee 1. cpp. #ifndef EMPLOYEE 1_H #define EMPLOYEE 1_H 6 7 8 // include Date class definition from date 1. h #include "date 1. h" 9 10 class Employee { 11 12 13 14 public: Employee( const char *, const Date &, const Date & ); 15 16 17 void print() const; ~Employee(); // provided to confirm destruction order 18 19 20 21 22 23 private: char first. Name[ 25 ]; char last. Name[ 25 ]; const Date birth. Date; const Date hire. Date; 24 25 }; // end class Employee // composition: member object Outline 26 employee 1. h (1 of 2) Using composition; Employee object contains Date objects as data members. 2003 Prentice Hall, Inc. All rights reserved.

26 27 #endif 1 2 3 // Fig. 7. 9: employee 1. cpp //

26 27 #endif 1 2 3 // Fig. 7. 9: employee 1. cpp // Member-function definitions for class Employee. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <cstring> // strcpy and strlen prototypes 9 10 11 #include "employee 1. h" #include "date 1. h" // Employee class definition // Date class definition Outline 27 employee 1. h (2 of 2) employee 1. cpp (1 of 3) 12 2003 Prentice Hall, Inc. All rights reserved.

13 14 15 16 17 18 19 20 21 22 23 24 25 26

13 14 15 16 17 18 19 20 21 22 23 24 25 26 // constructor uses member initializer list to pass initializer // values to constructors of member objects birth. Date and // hire. Date [Note: This invokes the so-called "default copy // constructor" which the C++ compiler provides implicitly. ] Employee: : Employee( const char *first, const char *last, const Date &date. Of. Birth, const Date &date. Of. Hire ) : birth. Date( date. Of. Birth ), // initialize birth. Date hire. Date( date. Of. Hire ) // initialize hire. Date { syntax to // copy first into first. Name and be sure Member that it initializer fits initialize Date data members int length = strlen( first ); length = ( length < 25 ? length : 24 ); birth. Date and hire. Date; compiler uses strncpy( first. Name, first, length ); first. Name[ length ] = ''; default copy constructor. 27 28 29 30 31 32 // copy last into last. Name and be sure that it fits length = strlen( last ); length = ( length < 25 ? length : 24 ); strncpy( last. Name, last, length ); last. Name[ length ] = ''; Output to show 33 34 35 36 // output Employee object to show when constructor is called cout << "Employee object constructor: " << first. Name << ' ' << last. Name << endl; Outline employee 1. cpp (2 of 3) timing of constructors. 37 2003 Prentice Hall, Inc. All rights reserved. 28

38 } // end Employee constructor 39 40 41 42 43 44 45 46

38 } // end Employee constructor 39 40 41 42 43 44 45 46 47 // print Employee object void Employee: : print() const { cout << last. Name << ", " << first. Name << "n. Hired: "; hire. Date. print(); cout << " Birth date: "; birth. Date. print(); cout << endl; 48 49 } // end function print 50 51 52 53 54 55 56 57 Outline to show its Output destructor is timing calledof destructors. // output Employee object to show when Employee: : ~Employee() { cout << "Employee object destructor: " << last. Name << ", " << first. Name << endl; employee 1. cpp (3 of 3) } // end destructor ~Employee 2003 Prentice Hall, Inc. All rights reserved. 29

1 2 3 // Fig. 7. 10: fig 07_10. cpp // Demonstrating composition--an object

1 2 3 // Fig. 7. 10: fig 07_10. cpp // Demonstrating composition--an object with member objects. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include "employee 1. h" 9 10 11 12 13 14 Create Date objects to pass int main() { to Employee constructor. Date birth( 7, 24, 1949 ); Date hire( 3, 12, 1988 ); Employee manager( "Bob", "Jones", birth, hire ); // Employee class definition 15 16 17 cout << 'n'; manager. print(); 18 19 20 21 cout << "n. Test Date constructor with invalid values: n" ; Date last. Day. Off( 14, 35, 1994 ); // invalid month and day cout << endl; 22 23 return 0; 24 25 Outline fig 07_10. cpp (1 of 1) } // end main 2003 Prentice Hall, Inc. All rights reserved. 30

Date object constructor for date 7/24/1949 Date object constructor for date 3/12/1988 Employee object

Date object constructor for date 7/24/1949 Date object constructor for date 3/12/1988 Employee object constructor: Bob Jones, Bob Hired: 3/12/1988 Birth date: 7/24/1949 Test Date constructor with invalid values: Month 14 invalid. Set to month 1. Day 35 invalid. Set to day 1. Date object constructor for date 1/1/1994 Date object destructor for date 1/1/1994 Employee object destructor: Jones, Bob Date object destructor for date 3/12/1988 Date object destructor for date 7/24/1949 Outline Note two additional Date objects constructed; no output since default copy constructor used. Destructor for host object Destructor Employee’s manager runsfor before member object hire. Date. Destructor Employee‘s fig 07_10. cpp destructors for member Destructor for Date object (1 of 1) member object birth. Date. objects hire. Date and output Destructor for Date object hire. birth. Date. birth. 2003 Prentice Hall, Inc. All rights reserved. 31

32 7. 4 friend Functions and friend Classes • friend function – Defined outside

32 7. 4 friend Functions and friend Classes • friend function – Defined outside class’s scope – Right to access non-public members • Declaring friends – Function • Precede function prototype with keyword friend – All member functions of class Class. Two as friends of class Class. One • Place declaration of form friend class Class. Two; in Class. One definition 2003 Prentice Hall, Inc. All rights reserved.

33 7. 4 friend Functions and friend Classes • Properties of friendship – Friendship

33 7. 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 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // Fig. 7. 11: fig 07_11. cpp // Friends can access private members of a class. #include <iostream> using std: : cout; using std: : endl; Outline Precede function prototype with keyword friend. // Count class definition class Count { friend void set. X( Count &, int ); // friend declaration public: // constructor Count() : x( 0 ) // initialize x to 0 { // empty body fig 07_11. cpp (1 of 3) } // end Count constructor 21 2003 Prentice Hall, Inc. All rights reserved. 34

22 23 24 25 // output x void print() const { cout << x

22 23 24 25 // output x void print() const { cout << x << endl; 26 27 } // end function print 28 29 30 private: int x; 31 32 }; // end class Count 33 34 35 36 37 38 39 40 Outline // data member Pass Count object since Cstyle, data standalone function. // function set. X can modify private of Count Since set. X friend // because set. X is declared as a friend ofof. Count, void set. X( Count &c, int val )can access and modify private data { c. x = val; // legal: set. X x. is a friend of Count member fig 07_11. cpp (2 of 3) } // end function set. X 41 2003 Prentice Hall, Inc. All rights reserved. 35

42 43 44 int main() { Count counter; Outline // create Count object 45

42 43 44 int main() { Count counter; Outline // create Count object 45 46 47 Use"friend function to cout << "counter. x after instantiation: ; access and modify private counter. print(); 48 49 set. X( counter, 8 ); 50 51 52 cout << "counter. x after call to set. X friend function: "; counter. print(); 53 54 return 0; 55 56 data member x. // set x with a friend } // end main fig 07_11. cpp (3 of 3) fig 07_11. cpp output (1 of 1) counter. x after instantiation: 0 counter. x after call to set. X friend function: 8 2003 Prentice Hall, Inc. All rights reserved. 36

1 2 3 4 // Fig. 7. 12: fig 07_12. cpp // Non-friend/non-member functions

1 2 3 4 // Fig. 7. 12: fig 07_12. cpp // Non-friend/non-member functions cannot access // private data of a class. #include <iostream> 5 6 7 using std: : cout; using std: : endl; 8 9 10 11 // Count class definition // (note that there is no friendship declaration) class Count { 12 13 14 15 16 17 18 19 20 21 public: Outline fig 07_12. cpp (1 of 3) // constructor Count() : x( 0 ) // initialize x to 0 { // empty body } // end Count constructor 22 2003 Prentice Hall, Inc. All rights reserved. 37

23 24 25 26 // output x void print() const { cout << x

23 24 25 26 // output x void print() const { cout << x << endl; 27 28 } // end function print 29 30 31 private: int x; 32 33 }; // end class Count 34 35 36 37 38 39 40 41 Outline // data member // function tries to modify private data of Count, Attempting to modify // but cannot because function is not a friend of Count private data member from void cannot. Set. X( Count &c, int val ) non-friend function results { in error. c. x = val; // ERROR: cannot access private member in Count fig 07_12. cpp (2 of 3) } // end function cannot. Set. X 42 2003 Prentice Hall, Inc. All rights reserved. 38

43 44 45 int main() { Count counter; Outline // create Count object 46

43 44 45 int main() { Count counter; Outline // create Count object 46 47 cannot. Set. X( counter, 3 ); // cannot. Set. X is not a friend 48 49 return 0; 50 51 } // end main D: cpphtp 4_examplesch 07Fig 07_12. cpp(39) : error C 2248: 'x' : cannot access private member declared in class 'Count' D: cpphtp 4_examplesch 07Fig 07_12. cpp(31) : see declaration of 'x' Attempting to modify private data member from non-friend function results in error. fig 07_12. cpp (3 of 3) fig 07_12. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 39

40 7. 5 Using the this Pointer • this pointer – Allows object to

40 7. 5 Using 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 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 // Fig. 7. 13: fig 07_13. cpp // Using the this

1 2 3 // Fig. 7. 13: fig 07_13. cpp // Using the this pointer to refer to object members. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 class Test { 9 10 11 12 public: Test( int = 0 ); // default constructor void print() const; 13 14 15 private: int x; 16 17 }; // end class Test 18 19 20 21 22 23 24 25 Outline fig 07_13. cpp (1 of 3) // constructor Test: : Test( int value ) : x( value ) // initialize x to value { // empty body } // end Test constructor 2003 Prentice Hall, Inc. All rights reserved. 41

26 27 28 29 30 31 32 33 34 35 36 37 38 39

26 27 28 29 30 31 32 33 34 35 36 37 38 39 // print x using implicit and explicit this pointers; // parentheses around *this required Implicitly use this pointer; void Test: : print() const only specify name of data { member (x). // implicitly use this pointer to access member x Explicitly use this pointer cout << " x = " << x; Outline with arrow operator. // explicitly use this pointer to access member x cout << "n this->x = " << this->x; // explicitly use dereferenced this pointer and // the dot operator to access member x cout << "n(*this). x = " << ( * this ). x << endl; 40 41 } // end function print 42 43 44 45 int main() { Test test. Object( 12 ); 46 47 test. Object. print(); 48 49 return 0; Explicitly use this pointer; dereference this pointer first, then use dot operator. fig 07_13. cpp (2 of 3) 50 2003 Prentice Hall, Inc. All rights reserved. 42

51 } // end main Outline x = 12 this->x = 12 (*this). x

51 } // end main Outline x = 12 this->x = 12 (*this). x = 12 fig 07_13. cpp (3 of 3) fig 07_13. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 43

44 7. 5 Using the this Pointer • Cascaded member function calls – Multiple

44 7. 5 Using 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 2003 Prentice Hall, Inc. All rights reserved.

1 2 // Fig. 7. 14: time 6. h // Cascading member function calls.

1 2 // Fig. 7. 14: time 6. h // Cascading member function calls. 3 4 5 6 7 // Time class definition. // Member functions defined in time 6. cpp. #ifndef TIME 6_H #define TIME 6_H 8 9 class Time { 10 11 12 public: Time( int = 0, int = 0 ); Outline Set functions return reference //to default constructor Time object to enable 13 14 15 16 17 18 // set functions Time &set. Time( int, Time &set. Hour( int ); Time &set. Minute( int ); Time &set. Second( int ); 19 20 21 22 23 // get functions (normally declared const) int get. Hour() const; // return hour int get. Minute() const; // return minute int get. Second() const; // return second int ); // set cascaded member function calls. set hour, minute, second time 6. h (1 of 2) // hour minute second 24 2003 Prentice Hall, Inc. All rights reserved. 45

25 26 27 // print functions (normally declared const) void print. Universal() const; //

25 26 27 // print functions (normally declared const) void print. Universal() const; // print universal time void print. Standard() const; // print standard time 28 29 30 31 32 private: int hour; int minute; int second; 33 34 }; // end class Time 35 36 #endif Outline // 0 - 23 (24 -hour clock format) // 0 - 59 time 6. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 46

1 2 3 // Fig. 7. 15: time 6. cpp // Member-function definitions for

1 2 3 // Fig. 7. 15: time 6. cpp // Member-function definitions for Time class. #include <iostream> 4 5 using std: : cout; 6 7 #include <iomanip> 8 9 10 using std: : setfill; using std: : setw; 11 12 #include "time 6. h" 13 14 15 16 17 18 19 // constructor function to initialize private data; // calls member function set. Time to set variables; // default values are 0 (see class definition) Time: : Time( int hr, int min, int sec ) { set. Time( hr, min, sec ); 20 21 } // end Time constructor Outline // Time class definition time 6. cpp (1 of 5) 22 2003 Prentice Hall, Inc. All rights reserved. 47

23 24 25 26 27 28 29 30 31 32 33 34 35 36

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 // set values of hour, minute, and second Time &Time: : set. Time( int h, int m, int s ) { set. Hour( h ); set. Minute( m ); Return *this as reference set. Second( s ); enable cascaded member return *this; Outline to function calls. // enables cascading } // end function set. Time // set hour value Time &Time: : set. Hour( int h ) { Return *this as reference hour = ( h >= 0 && h < 24 ) ? h enable : 0; cascaded member return *this; to time 6. cpp (2 of 5) function calls. // enables cascading } // end function set. Hour 42 2003 Prentice Hall, Inc. All rights reserved. 48

43 44 45 46 47 48 // set minute value Time &Time: : set.

43 44 45 46 47 48 // set minute value Time &Time: : set. Minute( int m ) { Return *this as reference minute = ( m >= 0 && m < 60 ) ? enable m : 0; cascaded member return *this; function calls. } // end function set. Minute 51 52 53 54 55 // set second value Time &Time: : set. Second( int s ) { Return *this as reference second = ( s >= 0 && s < 60 ) ? enable s : 0; cascaded member return *this; to // enables cascading 49 50 56 57 Outline function calls. to time 6. cpp (3 of 5) // enables cascading 58 59 } // end function set. Second 60 61 62 63 64 // get hour value int Time: : get. Hour() const { return hour; 65 66 } // end function get. Hour 67 2003 Prentice Hall, Inc. All rights reserved. 49

68 69 70 71 // get minute value int Time: : get. Minute() const

68 69 70 71 // get minute value int Time: : get. Minute() const { return minute; 72 73 } // end function get. Minute 74 75 76 77 78 // get second value int Time: : get. Second() const { return second; 79 80 } // end function get. Second 81 82 83 84 85 86 87 // print Time in universal format void Time: : print. Universal() const { cout << setfill( '0' ) << setw( 2 ) << hour << ": " << setw( 2 ) << minute << ": " << setw( 2 ) << second; 88 89 } // end function print. Universal Outline time 6. cpp (4 of 5) 90 2003 Prentice Hall, Inc. All rights reserved. 50

91 92 93 94 95 96 97 // print Time in standard format void

91 92 93 94 95 96 97 // print Time in standard format void Time: : print. Standard() const { cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ": " << setfill( '0' ) << setw( 2 ) << minute << ": " << setw( 2 ) << second << ( hour < 12 ? " AM" : " PM" ); 98 99 } // end function print. Standard Outline time 6. cpp (5 of 5) 2003 Prentice Hall, Inc. All rights reserved. 51

1 2 3 // Fig. 7. 16: fig 07_16. cpp // Cascading member function

1 2 3 // Fig. 7. 16: fig 07_16. cpp // Cascading member function calls with the this pointer. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include "time 6. h" 9 10 11 12 int main() { Time t; Outline // Time class definition Cascade member function calls; recall dot operator associates from left to right. 13 14 15 // cascaded function calls t. set. Hour( 18 ). set. Minute( 30 ). set. Second( 22 ); 16 17 18 19 // output time in universal and standard formats cout << "Universal time: "; t. print. Universal(); 20 21 22 cout << "n. Standard time: "; t. print. Standard(); 23 24 cout << "nn. New standard time: "; fig 07_16. cpp (1 of 2) 25 2003 Prentice Hall, Inc. All rights reserved. 52

26 27 // cascaded function calls t. set. Time( 20, 20 ). print. Standard();

26 27 // cascaded function calls t. set. Time( 20, 20 ). print. Standard(); 28 29 cout << endl; 30 31 return 0; 32 33 } // end main Universal time: 18: 30: 22 Standard time: 6: 30: 22 PM New standard time: 8: 20 PM Outline Function call to print. Standard must appear last; print. Standard does not return reference to t. fig 07_16. cpp (2 of 2) fig 07_16. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 53

7. 6 Dynamic Memory Management with Operators new and delete • Dynamic memory management

7. 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 <new> – Access to standard version of new 2003 Prentice Hall, Inc. All rights reserved. 54

7. 6 Dynamic Memory Management with Operators new and delete • new – Consider

7. 6 Dynamic Memory Management with Operators new and delete • new – Consider Time *time. Ptr; time. Ptr = 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 *time. Ptr = new Time( 12, 0, 0 ); – Allocating arrays int *grades. Array = new int[ 10 ]; 2003 Prentice Hall, Inc. All rights reserved. 55

7. 6 Dynamic Memory Management with Operators new and delete • delete – Destroy

7. 6 Dynamic Memory Management with Operators new and delete • delete – Destroy dynamically allocated object and free space – Consider delete time. Ptr; – Operator delete • Calls destructor for object • Deallocates memory associated with object – Memory can be reused to allocate other objects – Deallocating arrays delete [] grades. Array; – Deallocates array to which grades. Array points • If pointer to array of objects • First calls destructor for each object in array • Then deallocates memory 2003 Prentice Hall, Inc. All rights reserved. 56

57 7. 7 static Class Members • static class variable – “Class-wide” data •

57 7. 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 2003 Prentice Hall, Inc. All rights reserved.

58 7. 7 static Class Members • Accessing static class variables – Accessible through

58 7. 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: : get. Count() 2003 Prentice Hall, Inc. All rights reserved.

59 7. 7 static Class Members • static member functions – Cannot access non-static

59 7. 7 static Class Members • static member functions – Cannot access non-static data or functions – No this pointer for static functions • static data members and static member functions exist independent of objects 2003 Prentice Hall, Inc. All rights reserved.

1 2 3 4 // Fig. 7. 17: employee 2. h // Employee class

1 2 3 4 // Fig. 7. 17: employee 2. h // Employee class definition. #ifndef EMPLOYEE 2_H #define EMPLOYEE 2_H 5 6 class Employee { 7 8 9 10 11 12 public: Employee( const char *, const char ~Employee(); const char *get. First. Name() const; const char *get. Last. Name() const; 13 14 15 16 17 18 19 20 21 22 23 24 // static member function static int get. Count(); // return # private: char *first. Name; char *last. Name; Outline 60 * ); // constructor // destructor // return first name static // return lastmember name function can only access static data members and member objects instantiated functions. employee 2. h (1 of 2) static data member is class -wide data. // static data member static int count; // number of objects instantiated }; // end class Employee 25 2003 Prentice Hall, Inc. All rights reserved.

61 26 #endif 1 2 3 // Fig. 7. 18: employee 2. cpp //

61 26 #endif 1 2 3 // Fig. 7. 18: employee 2. cpp // Member-function definitions for class Employee. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 9 #include <new> #include <cstring> // C++ standard new operator // strcpy and strlen prototypes employee 2. h (2 of 2) 10 11 #include "employee 2. h" Initialize static // Employee class definition 12 13 14 employee 2. cpp (1 of 3) // define and initialize static data int Employee: : count = 0; 15 16 17 18 19 20 21 22 Outline data member exactly once at file memberscope. static member function data // define static member function that returns number of accesses static // Employee objects instantiated member count. int Employee: : get. Count() { return count; } // end static function get. Count 2003 Prentice Hall, Inc. All rights reserved.

23 24 25 26 27 28 29 30 31 32 33 // constructor dynamically

23 24 25 26 27 28 29 30 31 32 33 // constructor dynamically allocates space for // first and last name and uses strcpy to copy // first and last names into the object Employee: : Employee( const char *first, const char *last new) operator dynamically { allocates space. first. Name = new char[ strlen( first ) + 1 ]; strcpy( first. Name, first ); Uselast static data last. Name = new char[ strlen( ) + 1 ]; member strcpy( last. Name, last ); store total count of to employees. 34 35 ++count; 36 37 38 cout << "Employee constructor for " << first. Name << ' ' << last. Name << " called. " << endl; Outline // increment static count of employees 39 40 } // end Employee constructor 41 42 43 44 45 46 // destructor deallocates dynamically allocated memory Employee: : ~Employee() { cout << "~Employee() called for " << first. Name << ' ' << last. Name << endl; employee 2. cpp (2 of 3) 47 2003 Prentice Hall, Inc. All rights reserved. 62

48 49 delete [] first. Name; delete [] last. Name; 50 51 --count; //

48 49 delete [] first. Name; delete [] last. Name; 50 51 --count; // recapture memory Outline // decrement static count of employees 52 53 Use static } // end destructor ~Employee 54 55 56 57 58 59 60 61 // return first name of employees. const char *Employee: : get. First. Name() const { // const before return type prevents client from modifying // private data; client should copy returned string before // destructor deletes storage to prevent undefined pointer return first. Name; 62 63 } // end function get. First. Name 64 65 66 67 68 69 70 71 // return last name of employee const char *Employee: : get. Last. Name() const { // const before return type prevents client from modifying // private data; client should copy returned string before // destructor deletes storage to prevent undefined pointer return last. Name; 72 73 } // end function get. Last. Name Operator deletetodeallocates data member store totalmemory. count of employee 2. cpp (3 of 3) 2003 Prentice Hall, Inc. All rights reserved. 63

1 2 3 // Fig. 7. 19: fig 07_19. cpp // Driver to test

1 2 3 // Fig. 7. 19: fig 07_19. cpp // Driver to test class Employee. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <new> // C++ standard new operator 9 10 #include "employee 2. h" // Employee class definition 11 12 13 14 15 int main() { cout << "Number of employees before instantiation is " << Employee: : get. Count() << endl; // use class name 16 17 18 Employee *e 1 Ptr = new Employee( "Susan", "Baker" ); static member function Employee *e 2 Ptr = new Employee( "Robert", "Jones" ); 19 20 21 cout << "Number of employees << e 1 Ptr->get. Count(); Outline fig 07_19. cpp (1 of 2) new operator dynamically allocates space. can be invoked on any object class. afterofinstantiation is " 22 2003 Prentice Hall, Inc. All rights reserved. 64

23 24 25 26 27 28 cout << << << 29 30 31 32

23 24 25 26 27 28 cout << << << 29 30 31 32 33 delete e 1 Ptr; e 1 Ptr = 0; delete e 2 Ptr; e 2 Ptr = 0; 34 35 36 cout << "Number of employees << Employee: : get. Count() 37 38 return 0; 39 40 "nn. Employee 1: " e 1 Ptr->get. First. Name() " " << e 1 Ptr->get. Last. Name() "n. Employee 2: " e 2 Ptr->get. First. Name() " " << e 2 Ptr->get. Last. Name() << "nn"; // // Outline recapture memory disconnect pointer from free-store space recapture memory static member function disconnect pointer from free-store space invoked using binary scope Operatorresolution delete deallocates after deletion is " operator (no memory. << endl; existing class objects). fig 07_19. cpp (2 of 2) } // end main 2003 Prentice Hall, Inc. All rights reserved. 65

Number of employees before instantiation is 0 Employee constructor for Susan Baker called. Employee

Number of employees before instantiation is 0 Employee constructor for Susan Baker called. Employee constructor for Robert Jones called. Number of employees after instantiation is 2 Outline Employee 1: Susan Baker Employee 2: Robert Jones ~Employee() called for Susan Baker ~Employee() called for Robert Jones Number of employees after deletion is 0 fig 07_19. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 66

7. 8 Data Abstraction and Information Hiding • Information hiding – Classes hide implementation

7. 8 Data 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 2003 Prentice Hall, Inc. All rights reserved. 67

7. 8 Data Abstraction and Information Hiding • Abstract data types (ADTs) – Approximations/models

7. 8 Data 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 2003 Prentice Hall, Inc. All rights reserved. 68

69 7. 8. 1 Example: Array Abstract Data Type • ADT array – Could

69 7. 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 2003 Prentice Hall, Inc. All rights reserved.

70 7. 8. 2 Example: String Abstract Data Type • Strings in C++ –

70 7. 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) 2003 Prentice Hall, Inc. All rights reserved.

71 7. 8. 3 Example: Queue Abstract Data Type • Queue – FIFO •

71 7. 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) 2003 Prentice Hall, Inc. All rights reserved.

72 7. 9 Container Classes and Iterators • Container classes (collection classes) – Designed

72 7. 9 Container 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 2003 Prentice Hall, Inc. All rights reserved.

73 7. 10 Proxy Classes • Proxy class – Hide implementation details of another

73 7. 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 Class. To. Load; 2003 Prentice Hall, Inc. All rights reserved.

1 2 // Fig. 7. 20: implementation. h // Header file for class Implementation

1 2 // Fig. 7. 20: implementation. h // Header file for class Implementation 3 4 class Implementation { 5 6 public: 7 8 9 10 11 12 Outline // constructor Implementation( int v ) : value( v ) // initialize value with v { // empty body 13 14 } // end Implementation constructor 15 16 17 18 19 // set value to v void set. Value( int v ) { value = v; // should validate v 20 21 } // end function set. Value public member function. implementation. h (1 of 2) 22 2003 Prentice Hall, Inc. All rights reserved. 74

23 24 25 26 // return value int get. Value() const { return value;

23 24 25 26 // return value int get. Value() const { return value; 27 28 } // end function get. Value 29 30 31 private: int value; 32 33 }; // end class Implementation Outline public member function. implementation. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 75

1 2 // Fig. 7. 21: interface. h // Header file for interface. cpp

1 2 // Fig. 7. 21: interface. h // Header file for interface. cpp 3 4 class Implementation; 5 6 class Interface { 7 8 9 10 11 12 public: Interface( int ); void set. Value( int ); int get. Value() const; ~Interface(); 13 14 private: 15 16 17 18 19 Outline // forward class declaration // // Provide same public interface as class Implementation; recall set. Value same public interface as and get. Value only public member class Implementation functions. // requires previous forward Implementation *ptr; Pointer to Implementation object declaration (line 4) requires forward class declaration. interface. h (1 of 1) }; // end class Interface 2003 Prentice Hall, Inc. All rights reserved. 76

1 2 3 4 5 6 7 8 9 10 11 12 // Fig.

1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 7. 22: interface. cpp // Definition of class Interface #include "interface. h" // Interface class definition #include "implementation. h" // Implementation class definition Outline Maintain pointer to underlying // constructor Proxy class Interface object. Interface: : Interface( int v )Implementation : ptr ( new Implementation( v ) ) // includes initialize ptrfile for class header { Implementation. // empty body } // end Interface constructor 13 14 15 16 17 // call Implementation's set. Value function Invoke corresponding void Interface: : set. Value( int v ) function on underlying { Implementation object. ptr->set. Value( v ); 18 19 } // end function set. Value interface. cpp (1 of 2) 20 2003 Prentice Hall, Inc. All rights reserved. 77

21 22 23 24 // call Implementation's get. Value function int Interface: : get.

21 22 23 24 // call Implementation's get. Value function int Interface: : get. Value() const { return ptr->get. Value(); 25 26 } // end function get. Value 27 28 29 30 31 // destructor Interface: : ~Interface() { delete ptr; 32 33 } // end destructor ~Interface Invoke corresponding function on underlying Implementation object. Outline Deallocate underlying Implementation object. interface. cpp (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 78

1 2 3 // Fig. 7. 23: fig 07_23. cpp // Hiding a class’s

1 2 3 // Fig. 7. 23: fig 07_23. cpp // Hiding a class’s private data with a proxy class. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include "interface. h" 9 10 11 12 13 14 15 Only include proxy class header file. // Interface class definition int main() { Interface i( 5 ); Create object of proxy class Interface; note no mention of Implementation class. cout << "Interface contains: " << i. get. Value() << " before set. Value" << endl; 16 17 i. set. Value( 10 ); 18 19 20 cout << "Interface contains: " << i. get. Value() << " after set. Value" << endl; 21 22 return 0; 23 24 Outline fig 07_23. cpp (1 of 1) fig 07_23. cpp Invoke member functions via(1 of 1) output proxy class object. } // end main Interface contains: 5 before set. Value Interface contains: 10 after set. Value 2003 Prentice Hall, Inc. All rights reserved. 79