13-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
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

Behavioral Pattern: Visitor C h a p t e r 5 – P a g e 224 When an algorithm is separated from an object structure upon which it operates, new operations.
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.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Chapter 14 Operator Overloading: What does + mean? Consider the code below. Note the multiple uses of “+”. #include using namespace std; int main() { int.
9-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
1 Object-Oriented Programming Using C++ CLASS 27.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CS161 Topic #15 1 Today in CS161 Lecture #15 Practicing! Writing Programs to Practice Write a game program (1 player) of Mad Math Reuse the functions to.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
6-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 Presentation Copyright.
18-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
3-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
2-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Classes and Data Structures Jeffrey S. Childs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 Modeling CDs (Continued). 2 Getting Started Download: 2011_02_07_CD_Collection_Incomplete/
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Objects and Classes Engineering 1D04, Teaching Session 9.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Modeling CDs. 2 Objectives You will be able to: Sketch a diagram showing the relationship between a class and classes that it contains a members. Define.
11 Introduction to Object Oriented Programming (Continued) Cats.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
1 Introduction to Object Oriented Programming Chapter 10.
16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Classes and Objects.
Computing Fundamentals with C++
The Object-Oriented Thought Process Chapter 06
Chapter 6 Class Definitions and Member Functions
Chapter 11 Generic Collections
Computing Fundamentals with C++
Data Structures & Algorithms
Presentation transcript:

13-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.

13-2 Chapter 13: Object-Oriented Software Development Design and Implementation  Chapter Goals — See how the analysis deliverable (CRH cards) helps programmers design class definitions — Implement action responsibilities as public member functions — Implement knowledge responsibilities as private data members — Add constructors to initialize the data members. — Perform unit testing — Perform system testing

Designing Class Interfaces  The analysis phase helps understand what the system does  Design concentrates on the how  Next up: designing C++ class definitions — a class encapsulates action responsibilities (what an instance should be able to do) as member functions action responsibilities (what an instance should be able to do) as member functions knowledge responsibilities (what each instance should know) as data members knowledge responsibilities (what each instance should know) as data members

13-4 Analysis / Design Analysis Implementation Design  Some software is developed using the waterfall model strategy — Finish one phase before going onto the next  Problem: harder to go back UP to earlier phase

13-5 Iterative Approach  Object-Oriented design involves an iterative approach that keeps things flexible — more flexible strategy allows analysis questions during design what did you want? analysis questions during design what did you want? modification of design during implementation modification of design during implementation revisit problem specification while implementing revisit problem specification while implementing –we just found out that it would nice if.... — There are no clean rigid walls between analysis, design, and implementation

13-6 Can move to any phase  Jukebox design had 10 classes — The design could change to 8 or 12 classes during design or during implementation — The programmers can uncover issues that were not previously realized — The customer may change her or his mind — Implementation may be easier and the product better if we could just change this part of the specification -- the customer could agree!

13-7 From CRH Cards to Class Definitions  The CRH cards help the team design class definitions behavior and knowledge  At this point, the team will — Specify member functions including parameters and return types for the class member function headings — Determine the knowledge responsibilities (data memb- ers) required to implement the action responsibilities — Add constructors (or other initialization routines) — Implement constructors to initialize data members — Implement the member functions

13-8 Specify Member Functions  Write part of the class and add the major responsibilities get and put students class studentCollection { // first draft class studentCollection { // first draft public: public: ??? getStudent(???); ??? getStudent(???); private: private: // TBA: knows all students // TBA: knows all students }; };

13-9 Second Draft  Add appropriate return types and parameters to the class definition class studentCollection { // second draft class studentCollection { // second draft public: public: student getStudent(string ID); student getStudent(string ID); // return student associated with ID // return student associated with ID private: private: // TBA: knows all students // TBA: knows all students }; };

13-10 Add a private operation and some data member(s) no constructor yet class studentCollection { third draft public: student getStudent(student ID); student getStudent(student ID); // return student associated with ID // return student associated with ID void addStudent(student newStudent); void addStudent(student newStudent); // should this be private???? Maybe. // should this be private???? Maybe.private: // store all students // store all students vector my_data; vector my_data;};

13-11 Need student now  Implement student before declaring a vector of them class student { // first draft class student { // first draft public: public: bool canSelect(track currentSelection); bool canSelect(track currentSelection); // Returns true if student has credit // Returns true if student has credit // and has played less than 2 tracks // and has played less than 2 tracks private: private: // TBA // TBA }; };

13-12 Student  Determine return type (bool) and parameter of canSelect needs playtime so pass track  Add one data member class student { // first draft class student { // first draft public: public: bool canSelect(track currentSelection); bool canSelect(track currentSelection); // Returns true if student has credit // Returns true if student has credit // and has played less than 2 tracks // and has played less than 2 tracks private: private: // data members TBA // data members TBA }; };

13-13 Change Class Student  Student is used too much — The author should have used the more meaningful name of jukeboxAccount jukeboxAccount jukeBoxAccountCollection jukeBoxAccountCollection  Implementing Student and studentCollection is left as an exercise — but please use the names jukeBoxAccount and jukeBoxAccountCollection jukeBoxAccount and jukeBoxAccountCollection

13-14 Implementing the iterator pattern  During design and implementation, the programmer notices this action responsibility: — Allow someone to look at individual CDs or tracks  There is a design pattern for this — The iterator pattern — Design Patterns: Elements of Reusable Object- Oriented Software [Gamma, Helm, Johnson, Vlissides 95]

13-15 The iterator pattern  Intent — Provide a way to access the elements of an aggregate object sequentially without exposing the underlying representation  Motivation — A container object--such as CD which has a vector of track objects--should give access to its elements without exposing its internal structure

13-16 Need more member functions  To implement this pattern, the following operations must be available first()next()isDone() currentItem() currentItem()  The cdCollection definition with iterator pattern is shown in its final form next slide

13-17 class cdCollection { public: cdCollection(); cdCollection(); void addCD(const CD & nextCD); void addCD(const CD & nextCD); bool removeCD(const CD & aCD); bool removeCD(const CD & aCD); int size() const; int size() const; CD getCD(int cdNumber) const; CD getCD(int cdNumber) const; //--iterator functions: void first(); // my_index points to first CD void first(); // my_index points to first CD void next(); // index point to the next CD void next(); // index point to the next CD bool isDone() const; // is traversal done? bool isDone() const; // is traversal done? CD currentItem() const; CD currentItem() const;private: vector my_data; // store the CDs vector my_data; // store the CDs int my_size; int my_size; int my_index; int my_index;};

13-18 Implementation  The class definitions can be written and stored in.h files  They could be compiled separately  They should have all data members and the constructors to initialize  Although this is design, it is also implementation need the class definition  Let's implement a few classes in.cpp files

13-19 track, CD, cdCollection  During implementation — choose the data to store the knowledge responsibilities could be int, string, double, vector, cardReader,… could be int, string, double, vector, cardReader,… — select constructors or other proper initialization members — refine class definitions — add action responsibilities  Team decides to do track, CD, and cdCollection

13-20 track class definition class track { // no constructor yet public: seconds playTime(); seconds playTime(); // returns seconds required to play // returns seconds required to play int trackNumber(); int trackNumber(); // this track's location on the CD // this track's location on the CD int cdNumber(); int cdNumber(); // the tray location in the CD player // the tray location in the CD playerprivate: seconds my_playTime; seconds my_playTime; int my_trackNumber; int my_trackNumber; int my_cdNumber; int my_cdNumber;};

13-21 // file name: track.h // Final version of track's class definition class track { public: track(); // default constructor track(); // default constructor track(string initTitle, seconds initPlayTime, track(string initTitle, seconds initPlayTime, int initCdNumber, int initTrackNumber); int initCdNumber, int initTrackNumber); string title() const; string title() const; seconds playTime() const; seconds playTime() const; int cdNumber() const; int cdNumber() const; int trackNumber() const; int trackNumber() const;private: string my_title; string my_title; seconds my_playTime; seconds my_playTime; int my_cdNumber; int my_cdNumber; int my_trackNumber; int my_trackNumber;}; track class with constructors

13-22 Implement Member Functions  The next few slides show the member function implementations  This is followed by a test driver for track  Each class must be tested at least by — calling every member function once — construct a vector of the objects (if they are to go into a container that is)

13-23 // file name: track.cpp #include "track.h" // for track's class def. // default constructor: track t1; track::track() { // Initialize a default track my_title = "?title?"; my_title = "?title?"; my_playTime = 0; my_playTime = 0; my_cdNumber = 0; my_cdNumber = 0; my_trackNumber = 0; my_trackNumber = 0;} // constructor: track t2( "Dog", 255, 5, 2 ); track::track( string initTitle, seconds initPlayTime, seconds initPlayTime, int initCdNumber, int initCdNumber, int initTrackNumber ) int initTrackNumber ) { // Initialize a track object with arguments my_title = initTitle; my_title = initTitle; my_playTime = initPlayTime; my_playTime = initPlayTime; my_trackNumber = initTrackNumber; my_trackNumber = initTrackNumber; my_cdNumber = initCdNumber; my_cdNumber = initCdNumber;}

13-24 // file name: track.cpp continued // Accessing member functions seconds track::playTime() const { return my_playTime; return my_playTime;} int track::trackNumber() const { return my_trackNumber; return my_trackNumber;} int track::cdNumber() const { return my_cdNumber; return my_cdNumber;} string track::title() const { return my_title; return my_title;}

13-25 // This test driver call all member functions #include #include using namespace std; #include "track" int main() { // test drive track track t1; track t1; track t2("I'm the Cat", 3*60+55, 5, 1); track t2("I'm the Cat", 3*60+55, 5, 1); cout << t2.title() << endl; cout << t2.title() << endl; cout << t2.playTime() << " or " cout << t2.playTime() << " or " << t2.playTime() / 60 << " minutes and " << t2.playTime() / 60 << " minutes and " << t2.playTime() % 60 << " seconds"<<endl; << t2.playTime() % 60 << " seconds"<<endl; cout << "track# " << t2.trackNumber() << endl; cout << "track# " << t2.trackNumber() << endl; cout << " CD# " << t2.cdNumber() << endl; cout << " CD# " << t2.cdNumber() << endl;}Output: Test driver calls all member functions I'm the Cat 235 or 3 minutes and 55 seconds track #1 CD #5 CD #5 Demo t_track.cpp

13-26 A first cut at CD class CD { // first draft public: string artist(); string artist(); string title(); string title();private: string my_artist; string my_artist; string my_title; string my_title; vector my_data; // many tracks vector my_data; // many tracks int my_size; // # of tracks int my_size; // # of tracks};

13-27 class CD final design class CD { public://--constructors // default constructor allows vector of tracks // default constructor allows vector of tracks CD(); CD(); // initialize a CD with 0 tracks // initialize a CD with 0 tracks CD( string initArtist, CD( string initArtist, string initTitle, string initTitle, int initCdNumber); int initCdNumber);//--modifier void addTrack(const track & nextTrack); void addTrack(const track & nextTrack); // add a new track--missed this during design // add a new track--missed this during design

13-28 //--accessors string artist() const; string artist() const; // return this CD's artist // return this CD's artist string title() const; string title() const; // return the title of this CD // return the title of this CD int cdNumber() const; int cdNumber() const; // return the physical location in CD player // return the physical location in CD player track getTrack(int trackNum) const; track getTrack(int trackNum) const; // Given the trackNum, return the track object // Given the trackNum, return the track object int size() const; int size() const; // return the number of tracks on this CD // return the number of tracks on this CD //--iterator functions void first(); void first(); void next(); void next(); bool isDone() const; bool isDone() const; track currentItem() const; track currentItem() const;

13-29 // Data members: shouldn't matter to user // But they sure do matter to the implementers private: string my_artist; string my_artist; string my_title; string my_title; vector my_data; vector my_data; int my_size; int my_size; int my_index; int my_index; int my_cdNumber; int my_cdNumber;};

13-30 #include #include using namespace std; #include "cd" void show(const track & t2); // show the data stored on one track int main() { // test drive CD CD aCD("Browne, Jackson", "Facing East"); CD aCD("Browne, Jackson", "Facing East"); aCD.addTrack(track( "Looking East", 296, 5, 1)); aCD.addTrack(track( "Looking East", 296, 5, 1)); // add 9 more tracks to CD // add 9 more tracks to CD // … 9 lines of code deleted // … 9 lines of code deleted Test Drive CD

13-31 Show all tracks on the CD cout << aCD.artist() << " " << aCD.title() << endl; cout << aCD.artist() << " " << aCD.title() << endl; // Iterate over all tracks on the CD // Iterate over all tracks on the CD track aTrack; track aTrack; aCD.first(); while( ! aCD.isDone() ) { aTrack = aCD.currentItem(); show(aTrack); aCD.next(); } return 0; return 0;} Visit all objects with the iterator methods

13-32 A Free Function to help test void show(const track & aTrack) { cout.width(2); cout.width(2); cout << aTrack.trackNumber(); cout << aTrack.trackNumber(); cout.width(3); cout.width(3); cout << aTrack.cdNumber() << " "; cout << aTrack.cdNumber() << " "; cout << aTrack.title() cout << aTrack.title() << " " << (aTrack.playTime() / 60) << " " << (aTrack.playTime() / 60) << ":" << (aTrack.playTime() % 60) << endl; << ":" << (aTrack.playTime() % 60) << endl;}  Output from first track: 1 5 Looking East 4: Looking East 4:56 Demo t_cd.cpp

13-33 Consider cdCollection  The cdCollection should be able to remove and add CDs this uses a vector to store CD objects class cdCollection { // third draft class cdCollection { // third draft public: public: void addCD(const CD & aCD); void addCD(const CD & aCD); // add a CD to the collection if possible // add a CD to the collection if possible bool removeCD(const CD & aCD); bool removeCD(const CD & aCD); // remove CD if possible. // remove CD if possible. private: private: vector my_data; vector my_data; int my_size; int my_size; }; }; Demo t_cdcollection.cpp

13-34 Summary of implementing individual classes  Each class can be designed like this — first as a CRH card — then as a class definition with action responsibilities listed as member function — then refine return types and parameters — add data members — add constructors — implement member functions — test the class at a minimum, call every method at a minimum, call every method

System Testing  Need to put everything together and test the entire system  In this case, consider a jukebox class that contains the major classes  Then jukebox can coordinate the activities between those objects  First consider the main function — see next slide

13-36 What int main might look like  Jukebox will start and stop things #include "jukebox" // for class jukebox #include "jukebox" // for class jukebox int main() // first draft int main() // first draft { // One possibility--still undecided { // One possibility--still undecided jukeBox theJukeBox; jukeBox theJukeBox; while(theJukeBox.isRunning()) while(theJukeBox.isRunning()) { theJukeBox.processOneUser(); theJukeBox.processOneUser(); } return 0; return 0; } Demo runjuke.cpp

13-37 first cut at jukeBox  Jukebox contains the major objects class jukeBox { // first draft class jukeBox { // first draft public: public: void processOneUser(); void processOneUser(); // Do what's necessary to process one user // Do what's necessary to process one user bool isRunning(); bool isRunning(); // true until jukeBox detects it's time to quit // true until jukeBox detects it's time to quit private: private: cardReader my_cardReader; cardReader my_cardReader; cdCollection my_cdCollection; cdCollection my_cdCollection; studentCollection my_studentCollection; studentCollection my_studentCollection; userInterface my_userInterface; userInterface my_userInterface; }; };

13-38 Assessing the Design  Some examples of good design — A class that contains objects of another class should be sending messages to them Jukebox does this. If it didn't, you have extraneous objects in the class Jukebox does this. If it didn't, you have extraneous objects in the class — CDCollection does not know who contains it my_CdCollection does not send messages to Jukebox -- CDCollection could be reused my_CdCollection does not send messages to Jukebox -- CDCollection could be reused

13-39 Good Design?  Other examples of good design — The contained classes don't use each other — System intelligence is distributed well enough that Jukebox does very little consider the ease with which Jukebox gets the user desired selection based on the contents of the cdCollection consider the ease with which Jukebox gets the user desired selection based on the contents of the cdCollection my_currentSelection = my_currentSelection = my_trackSelector.getTrack(my_cdCollection) my_trackSelector.getTrack(my_cdCollection)