1 / 17

ECE 264 Object-Oriented Software Development

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

felcia
Télécharger la présentation

ECE 264 Object-Oriented Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 ECE 264: Lecture 15

  3. 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 ECE 264: Lecture 15

  4. Example: Class diagram ECE 264: Lecture 15

  5. 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 ECE 264: Lecture 15

  6. Example 2: Time class ECE 264: Lecture 15

  7. 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(); }; ECE 264: Lecture 15

  8. 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 ECE 264: Lecture 15

  9. 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 ECE 264: Lecture 15

  10. Basic UML composition diagram Rectangle • 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 • double width • double height • Point origin +Rectangle() +setOrigin() +getHeight() +setWidth() +getOrigin() +move() +getWidth() +area() +setHeight() 1 1 1 Point ECE 264: Lecture 15

  11. 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 ECE 264: Lecture 15

  12. 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 ECE 264: Lecture 15

  13. 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); } ECE 264: Lecture 15

  14. 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” ECE 264: Lecture 15

  15. 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 ECE 264: Lecture 15

  16. 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) {} ECE 264: Lecture 15

  17. 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, 8th ed. • Etter & Ingber, Engineering Problem Solving with C++, 2nd ed. ECE 264: Lecture 15

More Related