Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. Chapter 3: Data Abstraction: The Walls Data Abstraction & Problem.

Slides:



Advertisements
Similar presentations
UML an overview.
Advertisements

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall A.1.
Chapter 22 Object-Oriented Systems Analysis and Design and UML Systems Analysis and Design Kendall and Kendall Fifth Edition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Object-Oriented Analysis and Design
Introduction To System Analysis and Design
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Data Abstraction & Problem Solving with C++ Fifth Edition by Frank.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 1 Principles of Programming and Software Engineering.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Software Engineering Principles and C++ Classes
Data Abstraction: The Walls
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Unified Modeling Language
The Unified Modeling Language (UML) Class Diagrams.
Object-Oriented Analysis and Design
Introduction To System Analysis and design
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Systems Analysis and Design in a Changing World, Fifth Edition
Writing Classes (Chapter 4)
Copyright 2002 Prentice-Hall, Inc. Modern Systems Analysis and Design Third Edition Jeffrey A. Hoffer Joey F. George Joseph S. Valacich Chapter 20 Object-Oriented.
Introduction To System Analysis and Design
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
CS3773 Software Engineering Lecture 04 UML Class Diagram.
Modularity Keeps the complexity of a large program manageable by systematically controlling the interaction of its components Isolates errors Eliminates.
Introduction to Abstract Data Type & C++ Revision 1 Data Structure & Algorithm Part II 10/26/2015.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 3: Data Abstraction: The Walls Data Abstraction & Problem.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 15 System Modeling with the UML.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
1 Object-Oriented Programming Using C++ CLASS 5. 2 Object Composition Object composition occurs when a class contains an instance of another class. Creates.
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 14 Slide 1 Object-oriented Design.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
5 Systems Analysis and Design in a Changing World, Fifth Edition.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Object Oriented Analysis and Design Class and Object Diagrams.
Introduction to UML CS A470. What is UML? Unified Modeling Language –OMG Standard, Object Management Group –Based on work from Booch, Rumbaugh, Jacobson.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Object-Oriented Programming Chapter Chapter
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 2 Principles of Programming and Software Engineering.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Data Abstraction: The Walls
Principles of Programming and Software Engineering
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Data Abstraction: The Walls
Software Engineering System Modeling Chapter 5 (Part 1) Dr.Doaa Sami
Software Engineering System Modeling Chapter 5 (Part 1) Dr.Doaa Sami
Chapter 8: Class Relationships
Chapter 22 Object-Oriented Systems Analysis and Design and UML
Data Abstraction: The Walls
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 3: Data Abstraction: The Walls Data Abstraction & Problem Solving with C++ Fifth Edition by Frank M. Carrano

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver C++ Classes: The header file Sphere.h */ const double PI = ; class Sphere { public: Sphere(); // Default constructor Sphere(double initialRadius); // Constructor void setRadius(double newRadius); double getRadius() const; // can’t change data members double getDiameter() const; double getCircumference() const; double getArea() const; double getVolume() const; void displayStatistics() const; private: double theRadius; // data members should be private }; // end Sphere

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver C++ Classes: Constructors A class can have several constructors –A default constructor has no arguments –The compiler will generate a default constructor if you do not define any constructors

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver C++ Classes: Constructors The implementation of a method qualifies its name with the scope resolution operator :: The implementation of a constructor –Sets data members to initial values Can use an initializer Sphere::Sphere() : theRadius(1.0) { } // end default constructor –Cannot use return to return a value

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver C++ Classes: Destructors Destructor –Destroys an instance of an object when the object’s lifetime ends Each class has one destructor –For many classes, you can omit the destructor –The compiler will generate a destructor if you do not define one For now, we will use the compiler’s destructor

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver C++ Classes: The implementation file Sphere.cpp */ #include #include "Sphere.h" // header file using namespace std; Sphere::Sphere() : theRadius(1.0) { } // end default constructor Sphere::Sphere(double initialRadius) { if (initialRadius > 0) theRadius = initialRadius; else theRadius = 1.0; } // end constructor

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver C++ Classes: The implementation file void Sphere::setRadius(double newRadius) { if (newRadius > 0) theRadius = newRadius; else theRadius = 1.0; } // end setRadius The constructor could call setRadius

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver C++ Classes: The implementation file double Sphere::getRadius() const { return theRadius; } // end getRadius... double Sphere::getArea() const { return 4.0 * PI * theRadius * theRadius; } // end getArea...

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Abstract Data Types Modularity –Keeps the complexity of a large program manageable by systematically controlling the interaction of its components –Isolates errors –Eliminates redundancies

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Abstract Data Types Modularity (Continued) –A modular program is Easier to write Easier to read Easier to modify

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Abstract Data Types Functional abstraction –Separates the purpose and use of a module from its implementation –A module’s specifications should Detail how the module behaves Be independent of the module’s implementation

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Abstract Data Types Information hiding –Hides certain implementation details within a module –Makes these details inaccessible from outside the module

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Abstract Data Types Typical operations on data –Add data to a data collection –Remove data from a data collection –Ask questions about the data in a data collection

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Abstract Data Types Data abstraction –Asks you to think what you can do to a collection of data independently of how you do it –Allows you to develop each data structure in relative isolation from the rest of the solution –A natural extension of functional abstraction

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Abstract Data Types Abstract data type (ADT) –An ADT is composed of A collection of data A set of operations on that data –Specifications of an ADT indicate What the ADT operations do, not how to implement them –Implementation of an ADT Includes choosing a particular data structure

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Abstract Data Types Figure 3-4 A wall of ADT operations isolates a data structure from the program that uses it

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Object-Oriented Analysis and Design Object-oriented design (OOD) –Expresses an understanding of a solution that fulfills the requirements discovered during OOA –Describes a solution in terms of Software objects The collaborations of these objects with one another –Objects collaborate when they send messages (call each other’s operations) –Collaborations should be meaningful and minimal –Creates one or more models of a solution Some emphasize interactions among objects Others emphasize relationships among objects

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D Unified Modeling Language (UML) –A tool for exploration and communication during the design of a solution –Models a problem domain in terms of objects independently of a programming language –Visually represents object-oriented solutions as diagrams –Its visual nature is an advantage, since we are visual creatures –Enables members of a programming team to communicate visually with one another and gain a common understanding of the system being built

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D UML use case for OOA –A set of textual scenarios (stories) of the solution Each scenario describes the system’s behavior under certain circumstances from the perspective of the user –Focus on the responsibilities of the system to meeting a user’s goals Main success scenario (happy path): interaction between user and system when all goes well Alternate scenarios: interaction between user and system under exceptional circumstances –Find noteworthy objects, attributes, and associations within the scenarios

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D –An example of a main success scenario Customer asks to withdraw money from a bank account Bank identifies and authenticates customer Bank gets account type, account number, and withdrawal amount from customer Bank verifies that account balance is greater than withdrawal amount Bank generates receipt for the transaction Bank counts out the correct amount of money for customer Customer leaves bank

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D –An example of an alternate scenario Customer asks to withdraw money from a bank account Bank identifies, but fails to authenticate customer Bank refuses to process the customer’s request Customer leaves bank

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D UML sequence (interaction) diagram for OOD –Models the scenarios in a use case –Shows the interactions among objects over time –Lets you visualize the messages sent among objects in a scenario and their order of occurrence –Helps to define the responsibilities of the objects What must an object remember? What must an object do for other objects?

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D Figure 1-2 Sequence diagram for the main success scenario

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D Figure 1-3 Sequence diagram showing the creation of a new object

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D UML class (static) diagram –Represents a conceptual model of a class of objects in a language-independent way –Shows the name, attributes, and operations of a class –Shows how multiple classes are related to one another

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D Figure 1-4 Three possible class diagrams for a class of banks

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D Figure 1-5 A UML class diagram of a banking system

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D Class relationships –Association The classes know about each other Example: The Bank and Customer classes –Aggregation (Containment) One class contains an instance of another class Example: The Bank and Account classes The lifetime of the containing object and the object contained are not necessarily the same –Banks “live” longer than the accounts they contain

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D Class relationships (Continued) –Composition A stronger form of aggregation The lifetime of the containing object and the object contained are the same Example: A ballpoint pen –When the pen “dies,” so does the ball

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D Class relationships (Continued) –Generalization Indicates a family of classes related by inheritance Example: Account is an ancestor class; the attributes and operations of Account are inherited by the descendant classes, Checking and Savings

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D Notation –Association A relationship between two classes is shown by a connecting solid line Relationships more specific than association are indicated with arrowheads, as you will see Multiplicities: Optional numbers at the end(s) of an association or other relationship –Each bank object is associated with zero or more customers (denoted 0..*), but each customer is associated with one bank –Each customer can have multiple accounts of any type, but an account can belong to only one customer

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Applying the UML to OOA/D Notation (Continued) –Aggregation (Containment) Denoted by an open diamond arrowhead pointing to the containing class –Composition Denoted by a filled-in diamond arrowhead pointing to the containing class –Generalization (Inheritance) Denoted by an open triangular arrowhead pointing to the ancestor (general or parent) class –UML also provides notation to specify visibility, type, parameter, and default value information

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver In class exercise: Parking Ticket Simulator For this assignment, you will design a set of classes that work together to simulate a police officer issuing a parking ticket. For each car we have the license number, model, make, color, and the number of minutes the car has been parked. Each car is parked at a parking meter who knows the number of minutes of parking time that has been purchased. A police officer inspects the cars parked at the meters, and determines whether the car's time has expired. He issues a parking ticket if the car's time has expired. The parking ticket reports the make, model, color, and license number of the illegally parked car, the officer who gave the ticket and the amount of fine ($25 for the first hour plus $10 every additional hour or part of an hour);

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Design the UML class diagrams of this project.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Summary Data abstraction controls the interaction between a program and its data structures Abstract data type (ADT): a set of data- management operations together with the data values upon which they operate Define an ADT fully before making any decisions about an implementation

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Summary Hide an ADT’s implementation by defining the ADT as a C++ class An object encapsulates both data and operations A class contains one destructor and at least one constructor The compiler generates –A default constructor if no constructor is provided –A destructor if none is provided

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Summary Members of a class are private by default –Data members are typically private –Public methods can be provided to access them Define and implement a class within header and implementation files Namespace: a mechanism to group classes, functions, variables, types, and constants You can throw an exception if you detect an error during program execution. You handle, or deal with, an exception by using try and catch blocks