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

Slides:



Advertisements
Similar presentations
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Advertisements

9-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Asserting Java ©Rick Mercer Chapter 3 Objects and JUnit.
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Computer Science 1620 Programming & Problem Solving.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
C++ fundamentals.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
13-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
6-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 Presentation Copyright.
Programming Principles Chapter 1. Objectives Discuss the program design process. Introduce the Game of Life. Discuss object oriented design. – Information.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
File I/O ifstreams and ofstreams Sections 11.1 &
7-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 Object-Oriented Programming Using C++ CLASS 5. 2 Object Composition Object composition occurs when a class contains an instance of another class. Creates.
18-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Programming Life Cycle Problem analysisunderstand the problem Requirements definition specify what program will do High- and low-level designhow it meets.
3-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
1 Warm-Up Problem Just like with primitive data types (int, double, etc.), we can create arrays of objects. ex: bankAccount employees[100]; Problem: It’s.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Looping and Counting Lecture 3 Hartmut Kaiser
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
2-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Chapter 4 Introduction to Classes, Objects, Methods and strings
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
10-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Unit 1 - Introducing Abstract Data Type (ADT) Part 1.
Library Functions. CSCE 1062 Outline  cmath class library functions {section 3.2}  iomanip class library functions {section 8.5}  string class library.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
About the Presentations
– Introduction to Object Technology
Using Free Functions Chapter 3 Computing Fundamentals with C++
Sending Messages Chapter 5 Computing Fundamentals with C++ 3rd Edition
Chapter 6 Class Definitions and Member Functions
Chapter 7 Selection Computing Fundamentals with C++ 3rd Edition
Chapter 12 Pointers and Memory Management
Chapter 4 Implementing Free Functions
Chapter 11 Generic Collections
Computing Fundamentals with C++
Chapter 1 Problem Solving with C++
Today’s Objectives 28-Jun-2006 Announcements
Chapter 9 Introduction To Classes
Presentation transcript:

4-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 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.

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

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 Nouns are possible classes  Potential Classes to Model a Solution:  teller  customer  bank account  identification number  transaction  list of transactions  date  monthly statement

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", );

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 = ; // double x(0.0); double x = ; // 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", ); bankAccount anAccount("Gosch", );

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);

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);

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", ); bankAccount one("Stoers", ); bankAccount two("Daly", ); bankAccount two("Daly", ); // 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

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

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

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]

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

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 = ; double x = ; cout << " " << endl; cout << " " << 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:

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

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()

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

4-18 Differences between member and non-member functions

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

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

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);

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

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

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

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

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

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

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

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; }

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

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