1 Object-Oriented Programming Using C++ CLASS 5. 2 Object Composition Object composition occurs when a class contains an instance of another class. Creates.

Slides:



Advertisements
Similar presentations
UML an overview.
Advertisements

Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
C++ Classes & Data Abstraction
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Use Case & Use Case Diagram
1 Object-oriented design Part 2: OO tools & UML. 2 CRC cards Design tool & method for discovering classes, responsibilities, & relationships Record on.
Object-Oriented Application Development Using VB.NET 1 Chapter 5 Object-Oriented Analysis and Design.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 17 - C++ Classes: Part II Outline 17.1Introduction 17.2 const (Constant) Objects and const Member.
Sequence Diagrams. Introduction A Sequence diagram depicts the sequence of actions that occur in a system. The invocation of methods in each object, and.
Object-Oriented Analysis and Design
Introduction To System Analysis and Design
UML – Class Diagrams.
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.
1 Lecture Note 7- Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
Classes and Objects. const (Constant) Objects and const Member Functions Principle of least privilege –Only give objects permissions they need, no more.
Chapter 1 Principles of Programming and Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Software Engineering Principles and C++ Classes
1 Lab Beginning Analysis and Design 4 Completion of first version of use case diagram initiates the processes of analysis and design. 4 UML provides.
Use Case Modeling. Use case diagram For each use case we develop  Object class diagram (with attributes only)  System sequence diagram (analysis) 
Unified Modeling Language
The Unified Modeling Language (UML) Class Diagrams.
Object-Oriented Analysis and Design
Introduction To System Analysis and design
Systems Analysis and Design in a Changing World, Fifth Edition
Introduction To System Analysis and Design
Object-Oriented Analysis and Design An Introduction.
Programming in Java Unit 3. Learning outcome:  LO2:Be able to design Java solutions  LO3:Be able to implement Java solutions Assessment criteria: 
1 UML Basic Training. UML Basic training2 Agenda  Definitions: requirements, design  Basics of Unified Modeling Language 1.4  SysML.
Lab 04.
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.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 14 Slide 1 Object-oriented Design.
Fall 2010 CS4310 Requirements Engineering A Brief Review of UML & OO Dr. Guoqiang Hu Department of Computer Science UTEP 1.
Introduction to Unified Modeling Language (UML) By Rick Mercer with help from The Unified Modeling Language User Guide, Grady Booch, James Rumbaugh, Ivar.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6 1.
5 Systems Analysis and Design in a Changing World, Fifth Edition.
1 System Analysis and Design Using UML INSTRUCTOR: Jesmin Akhter Lecturer, IIT, JU.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
Design Jon Walker. More UML ● What is UML again?
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
LECTURE 4. Composition Definition: A data member of a class is an object of some other class Example: an AlarmClock object needs to know when it is supposed.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
CS212: Object Oriented Analysis and Design Lecture 32: Use case and Class diagrams.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
EEL 3801 Part VI Fundamentals of C and C++ Programming Classes and Objects.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
1 Object-Oriented Programming Using C++ CLASS 4 Honors.
Introduction to Unified Modeling Language (UML) By Rick Mercer with help from The Unified Modeling Language User Guide, Grady Booch, James Rumbaugh, Ivar.
Data Abstraction: The Walls
Introduction to Unified Modeling Language (UML)
Principles of Programming and Software Engineering
About the Presentations
Chapter 17 - C++ Classes: Part II
Software Engineering System Modeling Chapter 5 (Part 1) Dr.Doaa Sami
Using Use Case Diagrams
Copyright 2007 Oxford Consulting, Ltd
Software Engineering System Modeling Chapter 5 (Part 1) Dr.Doaa Sami
Data Abstraction: The Walls
Presentation transcript:

1 Object-Oriented Programming Using C++ CLASS 5

2 Object Composition Object composition occurs when a class contains an instance of another class. Creates a “has a” relationship between classes.

3 A class can contain an object of another class class Dateclass Employee {public:{public: // methods//methodsprivate:// data }; char lastName[25]; char firstName[25]; Date birthdate; Date hiredate; }; Composition (example not in textbook)

4 Date class

5 // DATE1.H #ifndefDATE1_H #defineDATE1_H class Date{ public: Date(int = 1, int = 1, int = 1900); // default constructor void print ( ) const; ~Date( ); private: int month;// 1-12 int day;// 1-31 based on month int year;// any year int checkDay(int); }; #endif

6 Date::Date(int mn, int dy, int yr) { if (mn > 0 && mn <= 12) month = mn; else{ month = 1; cout << “Month 1” << mn << “ invalid.Set to month 1.” << endl; } year = yr;// could also check day = checkDay(dy);// validate the day cout << “Date object constructor for date ”; print( ); cout << endl; }

7 int Date::checkDay(int testDay) { static int daysPerMonth[13] = {0, 31, 28, 31, 30 31, 30, 31, 31, 30, 31, 30, 31}; if (testDay > 0 && testDay <= daysPerMonth[month]) return testDay; if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 !=0))) return testDay; cout<<“Day”<< testDay << “ invalid. Set to day 1.”<<endl; return 1; }

8 //Print Date object in form month/day/year void Date::print( ) const { cout << month << ‘/’ << day << ‘/’ << year; } Date::~Date( ) { cout << “Date object destructor for date ”; print( ); cout << endl; }

9 Employee class

10 // emply1.h #ifndef EMPLY1_H #define EMPLY1_H #include "date1.h“ class Employee { public: Employee( char *, char *, int, int, int, int, int, int ); void print() const; ~Employee(); private: char firstName[ 25 ]; char lastName[ 25 ]; const Date birthDate; const Date hireDate; }; #endif

11 // Member function definitions for Employee class. #include using namespace std; #include #include "emply1.h" #include "date1.h"

12 Employee::Employee( char *fname, char *lname, int bmonth, int bday, int byear, int hmonth, int hday, int hyear ) : birthDate( bmonth, bday, byear ), hireDate( hmonth, hday, hyear ) { // body of constructor

13 Employee::Employee( char *fname, char *lname, int bmonth, int bday, int byear, int hmonth, int hday, int hyear ) : birthDate( bmonth, bday, byear ), hireDate( hmonth, hday, hyear ) // constructor of date class called twice: // once for the birthDate object // once for the hireDate object { // body of constructor

14 int length = strlen( fname ); length = ( length < 25 ? length : 24 ); strncpy( firstName, fname, length ); firstName[ length ] = '\0'; length = strlen( lname ); length = ( length < 25 ? length : 24 ); strncpy( lastName, lname, length ); lastName[ length ] = '\0'; cout << "Employee object constructor: “ << firstName << ' ' << lastName << endl; }

15 void Employee::print() const { cout << lastName << ", " << firstName << "\nHired: "; hireDate.print(); cout << " Birth date: "; birthDate.print(); cout << endl; } Employee::~Employee() { cout << "Employee object destructor: " << lastName << ", " << firstName << endl; } P. 466

16 #include using namespace std; #include "emply1.h“ int main() {Employee e( "Bob", "Jones", 7, 24, 1949, 3, 12, 1988 ); e.print(); Date d( 14, 35, 1994 ); cout << endl; return 0; } For the Birthdate object For the Hiredate object Parameters to the Employee constructor

17 Abstract Data Types Modularity –Keeps the complexity of a large program manageable by systematically controlling the interaction of its components –Isolates errors –Eliminates redundancies

18 Abstract Data Types Modularity (Continued) –A modular program is Easier to write Easier to read Easier to modify

19 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

20 Abstract Data Types Information hiding –Hides certain implementation details within a module –Makes these details inaccessible from outside the module

21 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

22 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

23 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

24 Abstract Data Types Figure 3-4 A wall of ADT operations isolates a data structure from the program that uses it

1-25 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

1-26 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

1-27 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

1-28 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

1-29 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

1-30 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?

1-31 Applying the UML to OOA/D Figure 1-2 Sequence diagram for the main success scenario

1-32 Applying the UML to OOA/D Figure 1-3 Sequence diagram showing the creation of a new object

1-33 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

1-34 Applying the UML to OOA/D Figure 1-4 Three possible class diagrams for a class of banks

1-35 Applying the UML to OOA/D Figure 1-5 A UML class diagram of a banking system

1-36 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

1-37 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

1-38 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

1-39 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

1-40 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