Presentation is loading. Please wait.

Presentation is loading. Please wait.

4-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

Similar presentations


Presentation on theme: "4-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8."— Presentation transcript:

1 4-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8 Presentation Copyright 1999, Franklin, Beedle & Associates Students who purchase and instructors who adopt Computing Fundamentals with C++, Object-Oriented Programming and Design by Rick Mercer are welcome to use this presentation as long as this copyright notice remains intact.

2 4-2 Chapter 4 Messages and Member Functions  Chapter Objectives  Send messages to objects  Understand how to send a few string and ostream messages and the effect they have on the object  Problem solve with grid and bankAccount objects  Appreciate why programmers partition software into classes, which are a collection of member functions combined with related data –Exercises and Programming Projects to reinforce sending messages to standard classes and some author-supplied classes

3 4-3 Find the objects in here Problem: Implement a bank teller application to allow bank customers to access bank accounts through an identification number. The customer, with the help of the teller, may complete any of the following transactions: withdraw money, deposit money, query account balances, and view any and all transactions between any two given dates. The system must maintain the correct balances for all accounts and produce monthly statements.

4 4-4 Nouns are possible classes  Potential Classes to Model a Solution:  teller  customer  bank account  identification number  transaction  list of transactions  date  monthly statement

5 4-5 4.1.1 class bankAccount  bankAccount represents a simple checking or savings account.  Operations: withdrawdeposit balance // return account balance name // return account name  State is represented by string my_name; double my_balance;  Initialization requires two arguments bankAccount anAcct("Kelsey Jenkins", 250.00);

6 4-6 Some objects need 2 or more arguments in the constructor  ints, doubles, and strings could be initialized with only one argument: // Equivalent initialization with ( ) // Equivalent initialization with ( ) int n = 0; // int n(0); int n = 0; // int n(0); double x = 0.000001; // double x(0.0); double x = 0.000001; // double x(0.0); string s = "A string"; // string s("A string"); string s = "A string"; // string s("A string");  bankAccount objects require two  Initialization with = is not an option bankAccount anAccount("Gosch", 100.00); bankAccount anAccount("Gosch", 100.00);

7 4-7 Messages  Some messages return the object's state. Other messages tell an object to do something:  A message that asks the object to return its state: cout << anAccount.balance() << endl; cout << anAccount.balance() << endl;  A message that tells the object to do something: anAccount.withdraw(25.00); anAccount.withdraw(25.00);

8 4-8 Messages continued  General Form: sending a message to an object object-name. function-name ( argument-list ) object-name. function-name ( argument-list )  Examples anAccount.deposit(100.00); anAccount.deposit(100.00); cout.width(6); cout.width(6); cout << anAccount.balance() << endl; cout << anAccount.balance() << endl; cout << aString.length() << endl; cout << aString.length() << endl; aGrid.move(3); aGrid.move(3);

9 4-9 Example Program // Initialize 2 bankAccount objects, send some messages #include "baccount" // for class bankAccount #include #include using namespace std; int main() { bankAccount one("Stoers", 200.00); bankAccount one("Stoers", 200.00); bankAccount two("Daly", 125.00); bankAccount two("Daly", 125.00); // assert: anAcct and anotherAcct are initialized // assert: anAcct and anotherAcct are initialized one.deposit(133.33); one.deposit(133.33); two.withdraw(50.00); two.withdraw(50.00); cout << one.name() << ": " << one.balance() << endl; cout << one.name() << ": " << one.balance() << endl; cout << two.name() << ": " << two.balance() << endl; cout << two.name() << ": " << two.balance() << endl; return 0; return 0;} Program Output? Optional Demo accounts2.cpp

10 4-10 4.1.3 Class and Object Diagrams  Relationship between a class and it objects using the Unified Modeling Language diagram

11 4-11 String messages and two new operators  Some objects have so many operations, we resort to using descriptive operation names  The [] operator returns a single character in a string. The + operator concatenates two strings  However, which operator should  return the dynamic length of a string object?  return the beginning location of a string in another?  return a portion of a string object (a substring)  Answers: The operations named length find substr

12 4-12 Some string operations string aString("Any old string") ; // assert: aString is initialized cout << aString[0] << endl ; // assert: A has been output (C++ startd counting at 0) cout << aString[1] << endl ; // assert: n has been output (C++ starts counting at 0) cout << aString.length() << endl ; // assert: aString.length() returns 14 cout << aString.find("ring") << endl ; // assert: aString.find("ring") returns 10 cout << aString.substr(4, 7) << endl ; // assert: returns "old str", aString[4]..aString[11]

13 4-13 4.2.2 ostream and istream Member Functions  Other standard C++ classes and named operations include:  ostream:: width, precision, setf  istream:: good, bad, clear  string:: replace, c_str  vector:: capacity, resize Demonstrate strings with p115.cpp

14 4-14 ostream::width and ostream::precision #include // for cout #include // for cout using namespace std; using namespace std; int main() int main() { double x = 9.87654321; double x = 9.87654321; cout << "123456789" << endl; cout << "123456789" << endl; cout.precision(4); cout.precision(4); cout.width(9); cout.width(9); cout << x << endl; cout << x << endl; cout.precision(3); cout.precision(3); cout.width(8); cout.width(8); cout << x << endl; cout << x << endl; return 0; return 0; } Output:123456789 9.877 9.877 9.88 9.88

15 4-15 4.2.4 Class Member Function Headings  Member function names are distinguished from non-member functions by qualifying the operation with the class-name and :: (the scope resolution operator)  Example function names as you might see them referred to in the text ostream::widthistream::goodstring::lengthstring::substr

16 4-16 Class member function headings  Member function headings are also qualified  Here are the member functions used so far: int string::length() int string::find(string subString) int string::find(string subString) string string::substr(int pos, int n) string string::substr(int pos, int n) int ostream::width(int nCols) int ostream::width(int nCols) int ostream::precision(int nDigits) int ostream::precision(int nDigits) int istream::good() int istream::good()

17 4-17 Why qualify member function headings?  The previous class member function headings indicate the class to which the function belongs  You will be required to use the qualification to implement class member functions if you study Chapter 6

18 4-18 Differences between member and non-member functions

19 4-19 4.3 Another nonstandard class grid  A grid object  stores a little rectangular map made up of rows and columns  has an object to move around.  is initialized with five arguments: grid grid-name ( rows, cols, moverRow, moverCol, direction );  where the first four arguments are integers and direction is either north, south, east, or west

20 4-20 Example #include "grid" // for class grid int main() { grid aGrid(5, 10, 0, 0, east); aGrid.display(); } Program Output: The grid: >..............

21 4-21 One grid Constructor  Use this class member function heading to help understand the construction of a grid object: grid::grid(int Rows, int Cols, int startRow, int startCol, int direction) // post: construct a 10 by 10 grid object with 5 arguments 1 grid aGrid(10, 10, 0, 0, east);

22 4-22 Accessing the state of a grid object  We observe state of grid objects with grid::display const means display does not modify the object void grid::display() const // post: The current state of the grid is // post: The current state of the grid is displayed on the computer screen displayed on the computer screen  Also access the state of grid objects with grid::row // the row the mover is in grid::row // the row the mover is in grid::column // the column the mover is in grid::column // the column the mover is in grid::nRows // the maximum number of rows grid::nRows // the maximum number of rows grid::nColumns // the maximum number of rows grid::nColumns // the maximum number of rows

23 4-23 Member functions that modify the state of a grid object void grid::move(int nSpaces) // pre: The mover has no obstructions in the next nSpaces // post: the mover has moved nSpaces forward void grid::putDown(int putDownRow, int putDownCol) // pre: The intersection (putDownRow, putDownCol) has nothing // on it expect perhaps the mover // post: There is one thing at the intersection void grid::pickUp() // pre: There is something to pickup at the movers location // post: There is nothing to pick up void grid::turnLeft() // post: The mover is facing 90 degrees counter-clockwise void grid::block(int blockRow, int blockCol) // pre: There is nothing at all at the intersection // post: The intersection can no longer be used

24 4-24 4.3.2 Failing to Meet the Preconditions  There are many "illegal" messages you can send to a grid object. For example  send a message telling the mover to move through a block ('#')  send a message telling the mover to move off the edge of the world  List some other sensible preconditions Demonstrate grid with p125.cpp

25 4-25 So what are we to do?  A precondition is a statement the client must ensure is true before sending a message  If the client ignores it, the resulting behavior is undefined--tough luck  You can experiment and see what happens when you run programs with grid objects

26 4-26 4.3.3 Functions with no Arguments still need ( )  Note: Remember to include ( ) even when no arguments are required in a message: cout << aString.length << endl; // ERROR: Missing () after length // ERROR: Missing () after length cout << aGrid.row << endl; // ERROR: Missing () after row

27 4-27 4.5 Why Functions and Classes?  Abstraction  has many meanings  is the process of pulling out and highlighting the relevant features of a complex system  allows us to use existing functions and classes more easily  allows us to use existing software without knowing all the implementation details how it works

28 4-28 Functions hide a lot of detail  One function call can represent many statements (or lines of code)

29 4-29 Active Learning  How many lines of code are represented by the messages in this program? Show output? #include "grid" // for class grid #include "grid" // for class grid int main() int main() { grid tarpit (5, 5, 1, 1, south); grid tarpit (5, 5, 1, 1, south); tarpit.move(3); tarpit.move(3); tarpit.turnLeft(); tarpit.turnLeft(); tarpit.move(3); tarpit.move(3); tarpit.turnLeft(); tarpit.turnLeft(); tarpit.move(3); tarpit.move(3); tarpit.turnLeft(); tarpit.turnLeft(); tarpit.move(3); tarpit.move(3); return 0; return 0; }

30 4-30 Reasons for functions  To reuse existing code rather than write it from scratch  To concentrate on the bigger issues at hand  To reduce errors by writing the function only once and testing it thoroughly  Programs that once had 1,000 statements in main might now have 100 functions that are 10 lines long  With object-orientation, it could be 10 classes with 10 member functions, that have 10 statements each

31 4-31 Structured and Object- Oriented Programming  Structured Programming  Partition programs into functions  The data is passed around from one non-member function to another  Worse yet, the data is made available everywhere throughout a large program  Object-Oriented Programming  Safely encapsulates collections of functions with the data they manipulate


Download ppt "4-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8."

Similar presentations


Ads by Google