ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development
Advertisements

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 21: Strings (cont.)
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
What is UML? A modeling language standardized by the OMG (Object Management Group), and widely used in OO analysis and design A modeling language is a.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Object-Oriented Analysis and Design
 2008 Pearson Education, Inc. All rights reserved (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements.
OBJECT ORIENTED PROGRAMMING LECTURE 12 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Object-Oriented Design, Part 2 CompSci 230 S Software Construction.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
UML Diagrams: Class Diagrams The Static Analysis Model Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Copyright © 2012 Pearson Education, Inc. 9/4/1435 h Sunday Lecture 1 Array’s Terminologies.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 2: Software Design Cycle.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Exam 3 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 11: Class diagrams; class relationships.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CLASS DIAGRAMS A classy approach to objects. The Basic Class Diagram  Class Name  Attributes (- indicates private each would have properties or accessor/mutator.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 11: Examples—creating a basic class.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 16: Destructors, Copy Constructors and Exam 2 Review.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
11/07/11Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro.
6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
Software Modelling Class Diagram. Class Diagrams The main building block in object oriented modeling They are used both for general conceptual modeling.
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2017
Inheritance ITI1121 Nour El Kadri.
ECE 264 Object-Oriented Software Development
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
Software Engineering Lecture #11.
Object Oriented System Design Class Diagrams
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships

Lecture outline Announcements / reminders  Lab 5 to be posted Tuesday; due 10/22 Today  UML class diagrams  Class relationships Association, aggregation, and composition Composition and initialization lists Modeling in UML 7/6/2015 ECE 264: Lecture 15 2

UML class diagrams UML class diagram contains 3 boxes  First contains class name  Second contains data members  Third contains member functions For member data/functions  Can list names only, but types/arguments preferred Format: name : type Same format for data/functions—type is fn. return type With function arguments, only types needed  + indicates public  - indicates private 7/6/2015 ECE 264: Lecture 15 3

Example: Class diagram Point -xCoord: double -yCoord: double +Point() +Point(double, double) +setX(double) : void +setY(double) : void +getX() : double +getY() : double +printPoint(ostream &) : void +movePoint(double, double) : void 7/6/2015 ECE 264: Lecture 15 4

Example 2 Say we have a class to represent a time in 24-hour format; this class should contain:  The number of hours and minutes  Default and parameterized constructors  Set/get functions for all data members  A private “helper” function, extraF, that is only used in other member functions, takes no arguments, and returns an integer Draw a class diagram for this class Using your class diagram, write a header file for the class 7/6/2015 ECE 264: Lecture 15 5

Example 2: Time class Time -hours : int -minutes : int +Time() +Time(int, int) +setHours(int) : void +setMinutes(int) : void +getHours() : int +getMinutes() : int -extraF() : int 7/6/2015 ECE 264: Lecture 15 6

Example 2: Time.h class Time { public: Time(); Time(int h, int m); void setHours(int newH); void setMinutes(int newM); int getX(); int getY(); private: int hours, minutes; int extraF(); }; 7/6/2015 ECE 264: Lecture 15 7

Class relationships Typically have multiple objects in program Different types may interact with one another  Basic interactions: association One class “uses” another in some way Example (from text): ATM “executes” a Withdrawal  Classes as data members: “has a” Two such relationships: aggregation and composition  Difference: are object lifetimes linked?  In composition, if “parent” is destroyed, “child” is as well  Same is not true for aggregation  Can model relationships in UML 7/6/2015 ECE 264: Lecture 15 8

Composition example A rectangle is a shape that has a:  point of origin  width  height Can implement this concept by defining a class named Rectangle  Methods might include: Accessing width/height/origin Setting width/height/origin Moving rectangle (i.e., relocating origin) Calculating area 7/6/2015 ECE 264: Lecture 15 9

Basic UML composition diagram Shows that Rectangle “has a” Point The 1 indicates Rectangle contains 1 point The closed diamond indicates composition  Objects share “life cycle”—destroy rectangle, and you destroy Point 7/6/2015 ECE 264: Lecture Rectangle Point 1 -double width -double height -Point origin +Rectangle()+setOrigin() +getHeight()+setWidth() +getOrigin()+move() +getWidth()+area() +setHeight() 1 1

Example code: setOrigin() void Rectangle::setOrigin(double x, double y) { origin.xCoord = x;// Won’t work origin.setY(y); } Example shows two different ways of accessing elements of Point  Directly changing private data still won’t work  Must use set functions 7/6/2015 ECE 264: Lecture 15 11

Composition example Write code for:  Rectangle::getOrigin();  Rectangle::setOrigin(); Rewrite example on previous slide to take a Point object, p, as an argument to the function  Rectangle::move(); Takes two arguments—distances to move the x and y coordinates of the origin Note that (most) types are purposely not given 7/6/2015 ECE 264: Lecture 15 12

Example solutions Point Rectangle::getOrigin() { return origin; } void Rectangle::setOrigin(Point p) { origin.setX(p.getX()); origin.setY(p.getY()); } void Rectangle::move(double addX, double addY) { origin.move(addX, addY); } 7/6/2015 ECE 264: Lecture 15 13

Modeling association Extension of class diagram  When showing relationships, do not need to show all members of class Association contains unidirectional arrow Can specify how many objects are involved  “1 ATM object executes 0 or 1 Withdrawal objects”  Possible multiplicities Constant values: 0, 1, m (integer) Either-or: m,n  “m or n”  0..1 is special case Unknown value: * (non-negative integer) Ranges:  m..n  “at least m but not more than n”  0..*  “zero or more”  1..*  “one or more” 7/6/2015 ECE 264: Lecture 15 14

Modeling composition/aggregation Composition indicated by solid diamonds attached to association lines  Aggregation would use hollow diamonds Properties  Only one class represents whole  Parts may only belong to one whole at a time 7/6/2015 ECE 264: Lecture 15 15

Initialization lists How would we write Rectangle constructor(s)?  Ideally, we’d like to call Point constructor as well  Use an initialization list Explicitly calls constructors for member data Requires parameterized constructor to be defined Can be used for predefined types as well  Example: Rectangle::Rectangle() : height(1), width(1), origin(0,0) {} 7/6/2015 ECE 264: Lecture 15 16

Final notes Next time  Arrays, vectors, and other container classes Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 7/6/2015 ECE 264: Lecture 15 17