Download presentation
Presentation is loading. Please wait.
Published byDoris Thomas Modified over 9 years ago
1
ECE 264 Object-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 7/6/2015 ECE 264: Lecture 15 2
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 7/6/2015 ECE 264: Lecture 15 3
4
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
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 7/6/2015 ECE 264: Lecture 15 5
6
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
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(); }; 7/6/2015 ECE 264: Lecture 15 7
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 7/6/2015 ECE 264: Lecture 15 8
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 7/6/2015 ECE 264: Lecture 15 9
10
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 15 10 Rectangle Point 1 -double width -double height -Point origin +Rectangle()+setOrigin() +getHeight()+setWidth() +getOrigin()+move() +getWidth()+area() +setHeight() 1 1
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 7/6/2015 ECE 264: Lecture 15 11
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 7/6/2015 ECE 264: Lecture 15 12
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); } 7/6/2015 ECE 264: Lecture 15 13
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” 7/6/2015 ECE 264: Lecture 15 14
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 7/6/2015 ECE 264: Lecture 15 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) {} 7/6/2015 ECE 264: Lecture 15 16
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, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 7/6/2015 ECE 264: Lecture 15 17
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.