Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.

Similar presentations


Presentation on theme: "1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University."— Presentation transcript:

1 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

2 2 Binary Operators A binary operator acts upon two operands x + ax + a binary operator operands

3 3 Overloaded Operators The operands can be constants, variables, or objects If one or more of the operands are objects, the operator must be an overloaded operator An overloaded operator calls a function, which carries out the operation

4 4 1 struct CarType { 2string maker; 3int year; 4float price; 5bool operator >( int num ) 6{ if ( price > num ) return true; return false; } 6 }; 7 8 int main( ) 9 { 10CarType myCar;. 16if ( myCar > 2000 ) 17cout << “My car is more than 2000!” << endl; 18return 0; 19 } Example

5 5 The Function Call “myCar > 2000” is the function call The operator > function is called for the myCar object The right operand is always passed as a parameter into the function

6 6 1 struct CarType { 2string maker; 3int year; 4float price; 5bool operator >( int num ) 6{ if ( price > num ) return true; return false; } 6 }; 7 8 int main( ) 9 { 10CarType myCar;. 16if ( myCar > 2000 ) 17cout << “My car is more than 2000!” << endl; 18return 0; 19 } Right Operand

7 7 1 struct CarType { 2string maker; 3int year; 4float price; 5bool operator >( int num ) 6 { if ( price > num ) return true; else return false; } 7bool operator >( CarType car ) 8 { if ( price > car.price ) return true; else return false; } 9 }; Adding Another Overloaded Operator > for “myCar > yourCar”

8 8 struct CarType { string maker; int year; float price; bool operator >( int num ) { if ( price > num ) return true; else return false; } bool operator >( CarType car ) { if ( price > car.price ) return true; else return false; } float operator +( CarType car ) { return price + car.price; } }; Adding an Overloaded Operator + for “myCar + yourCar”

9 9 Where to Place Overloaded Operator Functions If the left operand is an object of a struct, place the overloaded operator within the struct definition (as shown previously) If the left operand is not an object, place the overloaded operator directly underneath the struct definition

10 10 struct CarType { string maker; int year; float price;. float operator +( CarType car ) { return price + car.price; } }; bool operator <( int num, CarType car ) { if ( num < car.price ) return true; else return false; } Adding an Overloaded Operator for “2000 < myCar”

11 11 struct Check { float checkAmount; int checkNum; string date; string receiver; }; A Check Struct for the Checkbook Class (to Replace float)

12 12 class Checkbook { public: void setBalance( float amount ); bool writeCheck( float amount ); void deposit( float amount ); float getBalance( ); float getLastCheck( ); float getLastDeposit( ); private: float balance; float lastCheck; float lastDeposit; }; The First Class

13 13 class Checkbook { public: void setBalance( float amount ); bool writeCheck( Check amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; The Second Class Check struct placed here Necessary, so the compiler knows what “Check” is

14 14 Class Templates Instead of making different classes for different data types, we can make one class template. A class template is a blueprint for making a class. The client uses one line of code to have the compiler make a class from the class template. The client can make his/her own Check struct for the Checkbook, and place it in the main program.

15 15 class Checkbook { public: void setBalance( float amount ); bool writeCheck( Check amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template Check struct placed here First, we don’t need the Check struct here – the client can make it the way they want in the main program (the compiler will see it before it makes the class)

16 16 class Checkbook { public: void setBalance( float amount ); bool writeCheck( Check amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template (cont.)

17 17 class Checkbook { public: void setBalance( float amount ); bool writeCheck( Check amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template (cont.) Now, we need a special line of code to tell the compiler this is a class template

18 18 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( Check amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template (cont.) Now, we need a special line of code to tell the compiler this is a class template

19 19 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( Check amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template (cont.) you can think of DataType as a “variable name” for a data type – its value can be “float” or “Check” or anything else the client would like it to be

20 20 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( Check amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template (cont.) Now, since we are using a more general DataType, we use it for the type wherever appropriate

21 21 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( Check amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template (cont.)

22 22 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template (cont.)

23 23 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template (cont.)

24 24 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); DataType getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template (cont.)

25 25 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); DataType getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; Making a Class Template (cont.)

26 26 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); DataType getLastCheck( ); float getLastDeposit( ); private: float balance; DataType lastCheck; float lastDeposit; }; Making a Class Template (cont.)

27 27 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); DataType getLastCheck( ); float getLastDeposit( ); private: float balance; DataType lastCheck; float lastDeposit; }; Making a Class Template (cont.) Finally, we add a line to include the implementation file (not done in an ordinary class)

28 28 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); DataType getLastCheck( ); float getLastDeposit( ); private: float balance; DataType lastCheck; float lastDeposit; }; #include “checkbook.cpp” Making a Class Template (cont.) Finally, we add a line to include the implementation file (not done in an ordinary class)

29 29 template class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); DataType getLastCheck( ); float getLastDeposit( ); private: float balance; DataType lastCheck; float lastDeposit; }; #include “checkbook.cpp” Making a Class Template (cont.) The client can also have the compiler make more than one class from the class template, each having a different type for DataType

30 30 18 template 19 void Checkbook ::setBalance( float amount ) 20 { 21balance = amount; 22 } The Implementation File for a Class Template In a class template, we don’t include the class specification file at the top of the implementation file.

31 31 23 template 24 bool Checkbook ::writeCheck( DataType amount ) 25 { 26if ( amount > balance ) 27return false; 28balance -= amount; 29lastCheck = amount; 30return true; 31 } The Implementation File for a Class Template (cont.)

32 32 23 template 24 bool Checkbook ::writeCheck( DataType amount ) 25 { 26if ( amount > balance ) 27return false; 28balance -= amount; 29lastCheck = amount; 30return true; 31 } The Implementation File for a Class Template (cont.) If the client wants to use a Check struct for DataType, we have some overloaded operators here…

33 33 23 template 24 bool Checkbook ::writeCheck( DataType amount ) 25 { 26if ( amount > balance ) 27return false; 28balance -= amount; 29lastCheck = amount; 30return true; 31 } The Implementation File for a Class Template (cont.) struct assignment is allowed

34 34 32 template 33 void Checkbook ::deposit( float amount ) 34 { 35balance += amount; 36lastDeposit = amount; 37 } 38 39 template 40 float Checkbook ::getBalance( ) 41 { 42return balance; 43 } The Implementation File for a Class Template (cont.)

35 35 61 template 62 DataType Checkbook ::getLastCheck( ) 63 { 64return lastCheck; 65 } 66 67 template 68 float Checkbook ::getLastDeposit( ) 69 { 70return lastDeposit; 71 } The Implementation File for a Class Template (cont.)

36 36 // checkbook.h – a class template for a Checkbook, where the // check is any data type // to use an object for the DataType, overload the following // operators: //>left operand: objectright operand: float //used to compare the amount of the check in the // struct object with the balance //-=left operand: floatright operand: object //used to subtract the amount of the check in the //struct object from the balance Example of Comments for a Class Template Place these comments above the class template

37 37 1 #include 2 #include 3 #include 4 #include "checkbook.h" 5 6 using namespace std; Program Using the Class Template Needed in the main program for both classes and class templates

38 38 7 struct MyCheck { 8float amt; 9int checkNum; 10string checkComment; 11bool operator >( float bal ) 12{ if ( amt > bal ) return true; return false; } 13 }; 14 15 void operator -=( float & bal, MyCheck ch ) 16{ bal -= ch.amt; } Program Using the Class Template (cont.) from writeCheck: 26 if ( amount > balance ) from writeCheck: 28 balance -= amount;

39 39 17 int main( ) 18{ 19Checkbook johnsCheckbook; 20Checkbook susansCheckbook; 21 22MyCheck susansCheck; 23float amount; 24bool johnsCheckAccepted = false, 25susansCheckAccepted = false; Program Using the Class Template (cont.)

40 40 26johnsCheckbook.setBalance( 1000 ); 27susansCheckbook.setBalance( 2000 ); 28 29cout << "John, your balance is $1000.00." << endl; 30cout << "Susan, your balance is $2000.00." << endl; Program Using the Class Template (cont.)

41 41 31cout << "John, enter your check amount: $"; 32cin >> amount; 33if ( johnsCheckbook.writeCheck( amount ) ) { 34cout << "Your check was accepted." << endl; 35johnsCheckAccepted = true; 36} 37else 38 cout << 39 “Check amount is higher than balance” << endl; Program Using the Class Template (cont.)

42 42 40cout << "Susan, enter the check number for your check: "; 41cin >> susansCheck.checkNum; 42cin.ignore( ); 43cout << "Please also enter any comment you wish” << 44“ to make about the check: “ << endl; 45getline( cin, susansCheck.checkComment ); 46cout << "Susan, enter your check amount: $"; 47cin >> susansCheck.amt; Program Using the Class Template (cont.)

43 43 48if ( susansCheckbook.writeCheck( susansCheck ) ) { 49cout << "Your check was accepted." << endl; 50susansCheckAccepted = true; 51} 52else 53 cout << 54 “Check amount is higher than balance” << endl; Program Using the Class Template (cont.)

44 44 55cout << fixed << showpoint << setprecision( 2 ); 56cout << "John, your balance is: $" << 57johnsCheckbook.getBalance( ) << endl; 58if ( johnsCheckAccepted ) 59cout << "Your last check amount is: $" << 60johnsCheckbook.getLastCheck( ) << endl; 61cout << "Susan, your balance is: $" << 62susansCheckbook.getBalance( ) << endl; Program Using the Class Template (cont.)

45 45 63if ( susansCheckAccepted ) { 64MyCheck testSusansCheck; 65testSusansCheck = susansCheckbook.getLastCheck( ); 66cout << "Your last check amount was: $" << 67 testSusansCheck.amt << endl; 68cout << "for check number: " << 69testSusansCheck.checkNum << endl; 70cout << "with check comment: " << 71testSusansCheck.checkComment << endl; 72} 73 74return 0; 75 } Program Using the Class Template (cont.)

46 46 Stack A stack is a data structure It is like a stack of plates, except it is a stack of data It can be a stack of int’s, a stack of float’s, a stack of strings, a stack of struct objects, etc. –why class templates are used for data structures

47 47 Push Push means place a new data element at the top of the stack 17 5 11 3

48 48 Push (cont.) 17 5 11 3 Push means place a new data element at the top of the stack

49 49 Push (cont.) 17 5 11 3 Push means place a new data element at the top of the stack

50 50 Push (cont.) 17 5 11 3 Push means place a new data element at the top of the stack

51 51 Pop Pop means take a data element off the top of the stack 17 5 11 3

52 52 Pop (cont.) Pop means take a data element off the top of the stack 17 5 11 3

53 53 Pop (cont.) Pop means take a data element off the top of the stack 17 5 11 3

54 54 Pop (cont.) Pop means take a data element off the top of the stack 17 5 11 3

55 55 Peek Peek means retrieve the top of the stack without removing it 17 5 11 3

56 56 Peek (cont.) Peek means retrieve the top of the stack without removing it 17 5 11 3 3

57 57 Peek (cont.) Peek means retrieve the top of the stack without removing it 17 5 11 3 3

58 58 Abstraction Abstraction occurs when we use something without thinking about the details of how it works –Driving a car –The string class An abstract data type (ADT) is a data type that contains both data and functions (operations on data); we use the functions without thinking about how they work The stack is an abstract data type Abstraction makes large program development much easier

59 59 Chapter 2 Exercises A stack is often used as an aid in more complex algorithms For example, stacks can be used in expression evaluation Consider the problem of having the user enter an expression to be evaluated –must account for operator precedence: 5 + 2 * 3 –may be an indefinite number of nested parentheses: ( ( 5 + 2 ) * 3 + 4 ) / 5 Ordinarily, this would be a very difficult problem, but an algorithm using stacks makes it easy

60 60 Chapter 2 Exercises (cont.) First, a stack is used to convert an infix expression to a postfix expression Then, a stack is used to evaluate the postfix expression The following slides show how…

61 61 Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) Postfix Expression: Stack Infix to Postfix Conversion

62 62 Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) Postfix Expression: First, we push an ‘(‘ onto an empty stack. Infix to Postfix Conversion (cont.)

63 63 Stack ( Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) Postfix Expression: First, we push an ‘(‘ onto an empty stack. Infix to Postfix Conversion (cont.)

64 64 ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) Postfix Expression: Then, we append a ‘)’ to the infix expression Infix to Postfix Conversion (cont.)

65 65 ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: Then, we append a ‘)’ to the infix expression Infix to Postfix Conversion (cont.)

66 66 ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: Then, we read “tokens” from the infix expression, performing actions based on what we read. Infix to Postfix Conversion (cont.)

67 67 Infix to Postfix Conversion (cont.) If the token is a ‘(‘, we push it on the stack If the token is a number, we append it to the postfix expression If the token is a ‘)’, we pop the stack and append each popped token to the postfix expression, until we pop a ‘(‘ If the token is an operator, then while the top of the stack is an operator with precedence greater than or equal to the token, we pop the stack and append the popped operator to the postfix expression; when we are done popping, we push the token onto the stack

68 68 ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

69 69 ( ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

70 70 ( ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

71 71 ( ( 5 Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

72 72 ( ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 Infix to Postfix Conversion (cont.)

73 73 ( ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 Not even an operator here, so we don’t pop anything. Infix to Postfix Conversion (cont.)

74 74 ( ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 Infix to Postfix Conversion (cont.)

75 75 ( ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 Infix to Postfix Conversion (cont.)

76 76 ( ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 Infix to Postfix Conversion (cont.)

77 77 ( ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 Infix to Postfix Conversion (cont.)

78 78 ( ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 Infix to Postfix Conversion (cont.)

79 79 ( ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 This is an operator, but its precedence is less than *, so we don’t pop. Infix to Postfix Conversion (cont.)

80 80 ( ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 Infix to Postfix Conversion (cont.)

81 81 ( ( + * Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 Infix to Postfix Conversion (cont.)

82 82 ( ( + * Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 Infix to Postfix Conversion (cont.)

83 83 ( ( + * Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 Infix to Postfix Conversion (cont.)

84 84 ( ( + * Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 Infix to Postfix Conversion (cont.)

85 85 ( ( + * Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 Infix to Postfix Conversion (cont.)

86 86 ( ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * Infix to Postfix Conversion (cont.)

87 87 ( ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * Infix to Postfix Conversion (cont.)

88 88 ( ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + Infix to Postfix Conversion (cont.)

89 89 ( ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + Infix to Postfix Conversion (cont.)

90 90 ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + Infix to Postfix Conversion (cont.)

91 91 ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + Infix to Postfix Conversion (cont.)

92 92 ( - Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + Infix to Postfix Conversion (cont.)

93 93 ( - Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + Infix to Postfix Conversion (cont.)

94 94 ( - ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + Infix to Postfix Conversion (cont.)

95 95 ( - ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + Infix to Postfix Conversion (cont.)

96 96 ( - ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 Infix to Postfix Conversion (cont.)

97 97 ( - ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 Infix to Postfix Conversion (cont.)

98 98 ( - ( / Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 Infix to Postfix Conversion (cont.)

99 99 ( - ( / Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 Infix to Postfix Conversion (cont.)

100 100 ( - ( / Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 Infix to Postfix Conversion (cont.)

101 101 ( - ( / Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 Infix to Postfix Conversion (cont.)

102 102 ( - ( / Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 Infix to Postfix Conversion (cont.)

103 103 ( - ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / Infix to Postfix Conversion (cont.)

104 104 ( - ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / Infix to Postfix Conversion (cont.)

105 105 ( - ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / Infix to Postfix Conversion (cont.)

106 106 ( - ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 Infix to Postfix Conversion (cont.)

107 107 ( - ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 Infix to Postfix Conversion (cont.)

108 108 ( - ( + Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 Infix to Postfix Conversion (cont.)

109 109 ( - ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 + Infix to Postfix Conversion (cont.)

110 110 ( - ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 + Infix to Postfix Conversion (cont.)

111 111 ( - Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 + Infix to Postfix Conversion (cont.)

112 112 ( - Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 + Infix to Postfix Conversion (cont.)

113 113 ( - Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 + Infix to Postfix Conversion (cont.)

114 114 ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 + - Infix to Postfix Conversion (cont.)

115 115 ( Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 + - Infix to Postfix Conversion (cont.)

116 116 Stack Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) ) Postfix Expression: 5 3 2 * + 6 2 / 7 + - Infix to Postfix Conversion (cont.)

117 117 Postfix Expression: 5 3 2 * + 6 2 / 7 + - Stack Postfix Expression Evaluation

118 118 Postfix Expression: 5 3 2 * + 6 2 / 7 + - Stack When evaluating a postfix expression, a number encountered is simply pushed onto the stack. Postfix Expression Evaluation (cont.)

119 119 5Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

120 120 5Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

121 121 5 3 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

122 122 5 3 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

123 123 5 3 2 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

124 124 5 3 2 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

125 125 5 3 2 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - When an operator is encountered, it is used to perform an operation on the top 2 elements of the stack – in the following slides, take careful note of how it is done… Postfix Expression Evaluation (cont.)

126 126 5 3 2 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

127 127 5 32 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

128 128 5 32* Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

129 129 5 32* Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Was the topmost element – order can be important here! Postfix Expression Evaluation (cont.)

130 130 5 6 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

131 131 5 6 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

132 132 5 6 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

133 133 5 6 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

134 134 56 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

135 135 56+ Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

136 136 11 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

137 137 11 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

138 138 11 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

139 139 11 6 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

140 140 11 6 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

141 141 11 6 2 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

142 142 11 6 2 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

143 143 11 6 2 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

144 144 11 62 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

145 145 11 62/ Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

146 146 11 3 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

147 147 11 3 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

148 148 11 3 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

149 149 11 3 7 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

150 150 11 3 7 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

151 151 11 3 7 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

152 152 11 37 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

153 153 11 37+ Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

154 154 11 10 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

155 155 11 10 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

156 156 11 10 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

157 157 11 10 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

158 158 1110 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

159 159 1110- Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

160 160 1 Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

161 161 1Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

162 162 1Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)

163 163 1 Answer: Stack Postfix Expression: 5 3 2 * + 6 2 / 7 + - Postfix Expression Evaluation (cont.)


Download ppt "1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University."

Similar presentations


Ads by Google