180 likes | 322 Views
ECE 264 Object-Oriented Software Development. Instructor: Dr. Honggang Wang Spring 2013 Lecture 10: Class diagrams; class relationships. Lecture outline. Announcements / reminders Lab 5 to be posted Tuesday Today UML class diagrams Class relationships
E N D
ECE 264Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 10: Class diagrams; class relationships
Lecture outline • Announcements / reminders • Lab 5 to be posted Tuesday • Today • UML class diagrams • Class relationships • Association, aggregation, and composition • Composition and initialization lists • Modeling in UML ECE 264: Lecture 10
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 10
Example: Class diagram ECE 264: Lecture 10
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 10
Example 2: Time class ECE 264: Lecture 10
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 10
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 10
Association example • Manager uses a swipe card • the manager object and the swipe card object use each other but they have their own object life time http://www.codeproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit ECE 264: Lecture 10
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 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 10
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 10
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 10
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 10
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 10
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 10
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 10
Final notes • Next time • Classes relationships and example • Exam review next Thuesday • 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 10