52
2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction Structure Definitions Accessing Structure Members Implementing a User-Defined Type Time with a struct Implementing a Time Abstract Data Type with a class Class Scope and Accessing Class Members Separating Interface from Implementation linked list Homework 2

2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

Embed Size (px)

DESCRIPTION

 2003 Prentice Hall, Inc. All rights reserved Function Pointers Calling functions using pointers –Assume parameter: bool ( *compare ) ( int, int ) –Execute function with either ( *compare ) ( int1, int2 ) –Dereference pointer to function to execute OR compare( int1, int2 ) –Could be confusing User may think compare name of actual function in program

Citation preview

Page 1: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

1Lecture 6: Classes andData Abstraction

Posted Feb 3Chapter 6pointer for functionClass IntroductionStructure DefinitionsAccessing Structure MembersImplementing a User-Defined Type Time with a structImplementing a Time Abstract Data Type with a classClass Scope and Accessing Class MembersSeparating Interface from Implementation

linked listHomework 2

Page 2: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

2

5.11Function Pointers• Pointers to functions

– Contain address of function– Similar to how array name is address of first element– Function name is starting address of code that defines function

• Function pointers can be – Passed to functions– Returned from functions– Stored in arrays– Assigned to other function pointers

Page 3: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

3

5.11Function Pointers

• Calling functions using pointers– Assume parameter:

• bool ( *compare ) ( int, int )– Execute function with either

• ( *compare ) ( int1, int2 )– Dereference pointer to function to execute

OR• compare( int1, int2 )

– Could be confusing• User may think compare name of actual function in

program

Page 4: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

4

5.11Function Pointers

• Arrays of pointers to functions– Menu-driven systems– Pointers to each function stored in array of pointers to

functions• All functions must have same return type and same parameter

types– Menu choice subscript into array of function pointers

Page 5: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline5

fig05_26.cpp(1 of 3)

1 // Fig. 5.26: fig05_26.cpp2 // Demonstrating an array of pointers to functions.3 #include <iostream>4 5 using std::cout;6 using std::cin;7 using std::endl;8 9 // function prototypes10 void function1( int );11 void function2( int );12 void function3( int );13 14 int main()15 {16 // initialize array of 3 pointers to functions that each 17 // take an int argument and return void 18 void (*f[ 3 ])( int ) = { function1, function2, function3 };19 20 int choice;21 22 cout << "Enter a number between 0 and 2, 3 to end: ";23 cin >> choice;24

Array initialized with names of three functions; function names are pointers.

Page 6: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline6

fig05_26.cpp(2 of 3)

25 // process user's choice26 while ( choice >= 0 && choice < 3 ) {27 28 // invoke function at location choice in array f29 // and pass choice as an argument 30 (*f[ choice ])( choice ); 31 32 cout << "Enter a number between 0 and 2, 3 to end: ";33 cin >> choice;34 }35 36 cout << "Program execution completed." << endl;37 38 return 0; // indicates successful termination39 40 } // end main41 42 void function1( int a )43 {44 cout << "You entered " << a 45 << " so function1 was called\n\n";46 47 } // end function148

Call chosen function by dereferencing corresponding element in array.

Page 7: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline7

fig05_26.cpp(3 of 3)

fig05_26.cppoutput (1 of 1)

49 void function2( int b )50 {51 cout << "You entered " << b 52 << " so function2 was called\n\n";53 54 } // end function255 56 void function3( int c )57 {58 cout << "You entered " << c 59 << " so function3 was called\n\n";60 61 } // end function3

Enter a number between 0 and 2, 3 to end: 0You entered 0 so function1 was called Enter a number between 0 and 2, 3 to end: 1You entered 1 so function2 was called Enter a number between 0 and 2, 3 to end: 2You entered 2 so function3 was called Enter a number between 0 and 2, 3 to end: 3Program execution completed.

Page 8: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

8

6.1 Introduction

• Object-oriented programming (OOP) – Encapsulates data (attributes) and functions (behavior) into

packages called classes• Information hiding

– Class objects communicate across well-defined interfaces– Implementation details hidden within classes themselves

• User-defined (programmer-defined) types: classes– Data (data members) – Functions (member functions or methods)– Similar to blueprints – reusable

– Class instance: object variable

Page 9: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

9

6.2 Structure Definitions

• Structures – Aggregate data types built using elements of other types

struct Time {int hour; int minute; int second;

};

• Structure member naming– In same struct: must have unique names– In different structs: can share name

• struct definition must end with semicolon

Structure tag

Structure members

Page 10: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

10

6.2 Structure Definitions

• Self-referential structure – Structure member cannot be instance of enclosing struct– Structure member can be pointer to instance of enclosing struct (self-referential structure)

• Used for linked lists, queues, stacks and trees• struct definition

– Creates new data type used to declare variables– Structure variables declared like variables of other types– Examples:

• Time timeObject;• Time timeArray[ 10 ]; • Time *timePtr;• Time &timeRef = timeObject;

Page 11: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

11

6.3 Accessing Structure Members

• Member access operators– Dot operator (.) for structure and class members– Arrow operator (->) for structure and class members via

pointer to object– Print member hour of timeObject:

cout << timeObject.hour;

OR timePtr = &timeObject; cout << timePtr->hour;

– timePtr->hour same as ( *timePtr ).hour• Parentheses required

– * lower precedence than .

Page 12: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

12

6.4 Implementing a User-Defined Type Time with a struct

• Default: structures passed by value – Pass structure by reference

• Avoid overhead of copying structure

• C-style structures – No “interface”

• If implementation changes, all programs using that struct must change accordingly

– Cannot print as unit• Must print/format member by member

– Cannot compare in entirety• Must compare member by member

Page 13: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline13

fig06_01.cpp(1 of 3)

1 // Fig. 6.1: fig06_01.cpp2 // Create a structure, set its members, and print it.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 #include <iomanip>9 10 using std::setfill;11 using std::setw;12 13 // structure definition 14 struct Time { 15 int hour; // 0-23 (24-hour clock format)16 int minute; // 0-59 17 int second; // 0-59 18 19 }; // end struct Time 20 21 void printUniversal( const Time & ); // prototype22 void printStandard( const Time & ); // prototype23

Define structure type Time with three integer members.

Pass references to constant Time objects to eliminate copying overhead.

Page 14: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline14

fig06_01.cpp(2 of 3)

24 int main()25 {26 Time dinnerTime; // variable of new type Time 27 28 dinnerTime.hour = 18; // set hour member of dinnerTime 29 dinnerTime.minute = 30; // set minute member of dinnerTime30 dinnerTime.second = 0; // set second member of dinnerTime31 32 cout << "Dinner will be held at ";33 printUniversal( dinnerTime );34 cout << " universal time,\nwhich is ";35 printStandard( dinnerTime ); 36 cout << " standard time.\n";37 38 dinnerTime.hour = 29; // set hour to invalid value 39 dinnerTime.minute = 73; // set minute to invalid value40 41 cout << "\nTime with invalid values: ";42 printUniversal( dinnerTime );43 cout << endl;44 45 return 0; 46 47 } // end main48

Use dot operator to initialize structure members.

Direct access to data allows assignment of bad values.

Page 15: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline15

fig06_01.cpp(3 of 3)

fig06_01.cppoutput (1 of 1)

49 // print time in universal-time format50 void printUniversal( const Time &t )51 {52 cout << setfill( '0' ) << setw( 2 ) << t.hour << ":"53 << setw( 2 ) << t.minute << ":" 54 << setw( 2 ) << t.second;55 56 } // end function printUniversal57 58 // print time in standard-time format59 void printStandard( const Time &t )60 {61 cout << ( ( t.hour == 0 || t.hour == 12 ) ? 62 12 : t.hour % 12 ) << ":" << setfill( '0' )63 << setw( 2 ) << t.minute << ":" 64 << setw( 2 ) << t.second 65 << ( t.hour < 12 ? " AM" : " PM" );66 67 } // end function printStandard

Dinner will be held at 18:30:00 universal time,which is 6:30:00 PM standard time. Time with invalid values: 29:73:00

Use parameterized stream manipulator setfill.

Use dot operator to access data members.

Page 16: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

16

6.5 Implementing a Time Abstract Data Type with a class

• Classes– Model objects

• Attributes (data members) • Behaviors (member functions)

– Defined using keyword class– Member functions

• Methods

• Member access specifiers– public:

• Accessible wherever object of class in scope– private:

• Accessible only to member functions of class– protected:

Page 17: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

17

6.5 Implementing a Time Abstract Data Type with a class

• Constructor function– Special member function

• Initializes data members• Same name as class

– Called when object instantiated– Several constructors

• Function overloading– No return type

Page 18: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline18

Class Time definition(1 of 1)

1 class Time {2 3 public:4 Time(); // constructor5 void setTime( int, int, int ); // set hour, minute, second6 void printUniversal(); // print universal-time format7 void printStandard(); // print standard-time format8 9 private:10 int hour; // 0 - 23 (24-hour clock format)11 int minute; // 0 - 5912 int second; // 0 - 5913 14 }; // end class Time

Member access specifiers.

Definition of class begins with keyword class.

Class body starts with left brace.

Class body ends with right brace.

Definition terminates with semicolon.

Function prototypes for public member functions.

private data members accessible only to member functions.

Constructor has same name as class, Time, and no return type.

Page 19: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

19

6.5 Implementing a Time Abstract Data Type with a class

• Objects of class– After class definition

• Class name new type specifier– C++ extensible language

• Object, array, pointer and reference declarations– Example:

Time sunset; // object of type TimeTime arrayOfTimes[ 5 ]; // array of Time objectsTime *pointerToTime; // pointer to a Time objectTime &dinnerTime = sunset; // reference to a Time object

Class name becomes new type specifier.

Page 20: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

20

6.5 Implementing a Time Abstract Data Type with a class

• Member functions defined outside class– Binary scope resolution operator (::)

• “Ties” member name to class name• Uniquely identify functions of particular class• Different classes can have member functions with same name

– Format for defining member functionsReturnType ClassName::MemberFunctionName( ){…

}– Does not change whether function public or private

• Member functions defined inside class– Do not need scope resolution operator, class name– Compiler attempts inline

• Outside class, inline explicitly with keyword inline

Page 21: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline21

fig06_03.cpp(1 of 5)

1 // Fig. 6.3: fig06_03.cpp2 // Time class.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 #include <iomanip>9 10 using std::setfill;11 using std::setw;12 13 // Time abstract data type (ADT) definition 14 class Time { 15 16 public: 17 Time(); // constructor 18 void setTime( int, int, int ); // set hour, minute, second 19 void printUniversal(); // print universal-time format20 void printStandard(); // print standard-time format 21

Define class Time.

Page 22: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline22

fig06_03.cpp(2 of 5)

22 private: 23 int hour; // 0 - 23 (24-hour clock format) 24 int minute; // 0 - 59 25 int second; // 0 - 59 26 27 }; // end class Time 28 29 // Time constructor initializes each data member to zero and30 // ensures all Time objects start in a consistent state 31 Time::Time() 32 { 33 hour = minute = second = 0; 34 35 } // end Time constructor 36 37 // set new Time value using universal time, perform validity38 // checks on the data values and set invalid values to zero 39 void Time::setTime( int h, int m, int s ) 40 { 41 hour = ( h >= 0 && h < 24 ) ? h : 0; 42 minute = ( m >= 0 && m < 60 ) ? m : 0; 43 second = ( s >= 0 && s < 60 ) ? s : 0; 44 45 } // end function setTime 46

Constructor initializes private data members to 0.

public member function checks parameter values for validity before setting private data members.

Page 23: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline23

fig06_03.cpp(3 of 5)

47 // print Time in universal format48 void Time::printUniversal()49 {50 cout << setfill( '0' ) << setw( 2 ) << hour << ":"51 << setw( 2 ) << minute << ":"52 << setw( 2 ) << second;53 54 } // end function printUniversal55 56 // print Time in standard format57 void Time::printStandard()58 {59 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )60 << ":" << setfill( '0' ) << setw( 2 ) << minute61 << ":" << setw( 2 ) << second 62 << ( hour < 12 ? " AM" : " PM" );63 64 } // end function printStandard65 66 int main()67 {68 Time t; // instantiate object t of class Time69

Declare variable t to be object of class Time.

No arguments (implicitly “know” purpose is to print data members); member function calls more concise.

Page 24: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline24

fig06_03.cpp(4 of 5)

70 // output Time object t's initial values71 cout << "The initial universal time is ";72 t.printUniversal(); // 00:00:0073 74 cout << "\nThe initial standard time is ";75 t.printStandard(); // 12:00:00 AM76 77 t.setTime( 13, 27, 6 ); // change time78 79 // output Time object t's new values80 cout << "\n\nUniversal time after setTime is ";81 t.printUniversal(); // 13:27:0682 83 cout << "\nStandard time after setTime is ";84 t.printStandard(); // 1:27:06 PM85 86 t.setTime( 99, 99, 99 ); // attempt invalid settings87 88 // output t's values after specifying invalid values89 cout << "\n\nAfter attempting invalid settings:"90 << "\nUniversal time: ";91 t.printUniversal(); // 00:00:0092

Invoke public member functions to print time.

Set data members using public member function.

Attempt to set data members to invalid values using public member function.

Page 25: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline25

fig06_03.cpp(5 of 5)

fig06_03.cppoutput (1 of 1)

93 cout << "\nStandard time: ";94 t.printStandard(); // 12:00:00 AM95 cout << endl;96 97 return 0; 98 99 } // end main

The initial universal time is 00:00:00The initial standard time is 12:00:00 AM Universal time after setTime is 13:27:06Standard time after setTime is 1:27:06 PM After attempting invalid settings:Universal time: 00:00:00Standard time: 12:00:00 AM

Data members set to 0 after attempting invalid settings.

Page 26: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

26

6.5 Implementing a Time Abstract Data Type with a class

• Destructors– Same name as class

• Preceded with tilde (~)– No arguments – Cannot be overloaded– Performs “termination housekeeping”

Page 27: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

27

6.6 Class Scope and Accessing Class Members

• Class scope – Data members, member functions– Within class scope

• Class members – Immediately accessible by all member functions– Referenced by name

– Outside class scope• Referenced through handles

– Object name, reference to object, pointer to object

• File scope – Nonmember functions

Page 28: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

28

6.6 Class Scope and Accessing Class Members

• Function scope– Variables declared in member function– Only known to function– Variables with same name as class-scope variables

• Class-scope variable “hidden”– Access with scope resolution operator (::)

ClassName::classVariableName– Variables only known to function they are defined in– Variables are destroyed after function completion

Page 29: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

29

6.7 Separating Interface from Implementation

• Separating interface from implementation– Advantage

• Easier to modify programs– Disadvantage

• Header files – Portions of implementation

• Inline member functions– Hints about other implementation

• private members• Can hide more with proxy class

Page 30: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

30

6.7 Separating Interface from Implementation

• Header files– Class definitions and function prototypes– Included in each file using class

• #include– File extension .h

• Source-code files– Member function definitions– Same base name

• Convention– Compiled and linked

Page 31: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline31

time1.h (1 of 1)

1 // Fig. 6.5: time1.h 2 // Declaration of class Time. 3 // Member functions are defined in time1.cpp4 5 // prevent multiple inclusions of header file6 #ifndef TIME1_H7 #define TIME1_H8 9 // Time abstract data type definition10 class Time {11 12 public:13 Time(); // constructor14 void setTime( int, int, int ); // set hour, minute, second15 void printUniversal(); // print universal-time format16 void printStandard(); // print standard-time format17 18 private:19 int hour; // 0 - 23 (24-hour clock format)20 int minute; // 0 - 5921 int second; // 0 - 5922 23 }; // end class Time24 25 #endif

Preprocessor code to prevent multiple inclusions.

Code between these directives not included if name TIME1_H already defined.

“If not defined”Preprocessor directive defines name TIME1_H.Naming convention:

header file name with underscore replacing period.

Page 32: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline32

time1.cpp (1 of 3)

1 // Fig. 6.6: time1.cpp2 // Member-function definitions for class Time.3 #include <iostream>4 5 using std::cout;6 7 #include <iomanip>8 9 using std::setfill;10 using std::setw;11 12 // include definition of class Time from time1.h13 #include "time1.h" 14 15 // Time constructor initializes each data member to zero.16 // Ensures all Time objects start in a consistent state.17 Time::Time() 18 { 19 hour = minute = second = 0; 20 21 } // end Time constructor22

Include header file time1.h.

Name of header file enclosed in quotes; angle brackets cause preprocessor to assume header part of C++ Standard Library.

Page 33: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline33

time1.cpp (2 of 3)

23 // Set new Time value using universal time. Perform validity24 // checks on the data values. Set invalid values to zero.25 void Time::setTime( int h, int m, int s )26 {27 hour = ( h >= 0 && h < 24 ) ? h : 0;28 minute = ( m >= 0 && m < 60 ) ? m : 0;29 second = ( s >= 0 && s < 60 ) ? s : 0;30 31 } // end function setTime32 33 // print Time in universal format34 void Time::printUniversal()35 {36 cout << setfill( '0' ) << setw( 2 ) << hour << ":"37 << setw( 2 ) << minute << ":"38 << setw( 2 ) << second;39 40 } // end function printUniversal41

Page 34: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline34

time1.cpp (3 of 3)

42 // print Time in standard format43 void Time::printStandard()44 {45 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )46 << ":" << setfill( '0' ) << setw( 2 ) << minute47 << ":" << setw( 2 ) << second 48 << ( hour < 12 ? " AM" : " PM" );49 50 } // end function printStandard

Page 35: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline35

fig06_07.cpp(1 of 2)

1 // Fig. 6.7: fig06_07.cpp2 // Program to test class Time. 3 // NOTE: This file must be compiled with time1.cpp.4 #include <iostream>5 6 using std::cout;7 using std::endl;8 9 // include definition of class Time from time1.h10 #include "time1.h" 11 12 int main()13 {14 Time t; // instantiate object t of class Time15 16 // output Time object t's initial values17 cout << "The initial universal time is ";18 t.printUniversal(); // 00:00:0019 cout << "\nThe initial standard time is ";20 t.printStandard(); // 12:00:00 AM21 22 t.setTime( 13, 27, 6 ); // change time23

Include header file time1.h to ensure correct creation/manipulation and determine size of Time class object.

Page 36: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline36

fig06_07.cpp(2 of 2)

fig06_07.cppoutput (1 of 1)

24 // output Time object t's new values25 cout << "\n\nUniversal time after setTime is ";26 t.printUniversal(); // 13:27:0627 cout << "\nStandard time after setTime is ";28 t.printStandard(); // 1:27:06 PM29 30 t.setTime( 99, 99, 99 ); // attempt invalid settings31 32 // output t's values after specifying invalid values33 cout << "\n\nAfter attempting invalid settings:"34 << "\nUniversal time: ";35 t.printUniversal(); // 00:00:0036 cout << "\nStandard time: ";37 t.printStandard(); // 12:00:00 AM38 cout << endl;39 40 return 0; 41 42 } // end main

The initial universal time is 00:00:00The initial standard time is 12:00:00 AM Universal time after setTime is 13:27:06Standard time after setTime is 1:27:06 PM

Page 37: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

37

17.1 Introduction

• Fixed-size data structures– Arrays, structs

• Dynamic data structures– Grow and shrink as program runs– Linked lists

• Insert/remove items anywhere– Stacks

• Insert/remove from top of stack– Queues

• Like a line, insert at back, remove from front– Binary trees

• High-speed searching/sorting of data

Page 38: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

38

17.2 Self-Referential Classes

• Self-referential class– Has pointer to object of same class– Link together to form useful data structures

• Lists, stacks, queues, trees– Terminated with NULL pointer

15 10

NULL pointer (points to nothing)Data member and pointer

Page 39: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

39

17.2 Self-Referential Classes

• Sample codeclass Node { public: Node( int ); void setData( int ); int getData() const; void setNextPtr( Node * ); const Node *getNextPtr() const;private: int data; Node *nextPtr;};

• Pointer to object called a link– nextPtr points to a Node

Page 40: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

40

17.3 Dynamic Memory Allocation and Data Structures

• Dynamic memory allocation– Obtain and release memory during program execution– Create and remove nodes

• Operator new– Takes type of object to create– Returns pointer to newly created object

• Node *newPtr = new Node( 10 );• Returns bad_alloc if not enough memory• 10 is the node's object data

Page 41: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

41

17.3 Dynamic Memory Allocation and Data Structures

• Operator delete– delete newPtr;– Deallocates memory allocated by new, calls destructor– Memory returned to system, can be used in future

• newPtr not deleted, only the space it points to

Page 42: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

42

17.4 Linked Lists

• Linked list – Collection of self-referential class objects (nodes) connected

by pointers (links)– Accessed using pointer to first node of list

• Subsequent nodes accessed using the links in each node– Link in last node is null (zero)

• Indicates end of list– Data stored dynamically

• Nodes created as necessary• Node can have data of any type

Page 43: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

43

17.4 Linked Lists

H D Q

firstPtr lastPtr

...

Page 44: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

44

17.4 Linked Lists

• Linked lists vs. arrays– Arrays can become full

• Allocating "extra" space in array wasteful, may never be used• Linked lists can grow/shrink as needed• Linked lists only become full when system runs out of

memory– Linked lists can be maintained in sorted order

• Insert element at proper position• Existing elements do not need to be moved

Page 45: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

45

17.4 Linked Lists

• Selected linked list operations– Insert node at front– Insert node at back– Remove node from front– Remove node from back

• In following illustrations– List has firstPtr and lastPtr– (a) is before, (b) is after

Page 46: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

46

Insert at front

7 11

firstPtr

12

newPtr

a)

7 11

firstPtr

12

newPtr

b)

newPtr->nextPtr = firstPtr

firstPtr = newPtrIf list empty, then firstPtr = lastPtr = newPtr

Page 47: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

47

Insert at backfirstPtr lastPtra) newPtr

firstPtr lastPtrb) newPtr

12 7 11 5

12 11 57

lastPtr->nextPtr = newPtr

lastPtr = newPtrIf list empty, then firstPtr = lastPtr = newPtr

Page 48: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

48

Remove from frontfirstPtr lastPtra)

firstPtr lastPtrb)

tempPtr

12 712

12 7

511

511

tempPtr = firstPtr

firstPtr = firstPtr->nextIf there are no more nodes,firstPtr = lastPtr = 0

delete tempPtr

Page 49: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

49

Remove from back

5

5

117

7

12

12

firstPtr lastPtra)

firstPtr lastPtrb)

tempPtr

currentPtr

11

tempPtr = lastPtr

"Walk" list until get next-to-last node, untilcurrentPtr->nextPtr = lastPtr

lastPtr = currentPtr

delete tempPtr

Page 50: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc. All rights reserved.

50

17.4 Linked Lists

• Upcoming program has two class templates– Create two class templates– ListNode

• data (type depends on class template)• nextPtr

– List• Linked list of ListNode objects• List manipulation functions

– insertAtFront– insertAtBack– removeFromFront– removeFromBack

Page 51: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline51

listnode.h (1 of 2)

1 // Fig. 17.3: listnode.h2 // Template ListNode class definition.3 #ifndef LISTNODE_H4 #define LISTNODE_H5 6 // forward declaration of class List 7 template< class NODETYPE > class List; 8 9 template< class NODETYPE> 10 class ListNode {11 friend class List< NODETYPE >; // make List a friend12 13 public:14 ListNode( const NODETYPE & ); // constructor15 NODETYPE getData() const; // return data in node16 17 private:18 NODETYPE data; // data19 ListNode< NODETYPE > *nextPtr; // next node in list20 21 }; // end class ListNode22

Template class ListNode. The type of member data depends on how the class template is used.

Page 52: 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction

2003 Prentice Hall, Inc.All rights reserved.

Outline52

listnode.h (2 of 2)

23 // constructor24 template< class NODETYPE> 25 ListNode< NODETYPE >::ListNode( const NODETYPE &info )26 : data( info ), 27 nextPtr( 0 ) 28 { 29 // empty body30 31 } // end ListNode constructor32 33 // return copy of data in node34 template< class NODETYPE >35 NODETYPE ListNode< NODETYPE >::getData() const 36 { 37 return data; 38 39 } // end function getData40 41 #endif