ECE 264Object-Oriented
Software Development
Instructor: Dr. Honggang WangFall 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
04/21/23 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
04/21/23 ECE 264: Lecture 15 3
Example: Class diagramPoint
-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
04/21/23 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
04/21/23 ECE 264: Lecture 15 5
Example 2: Time classTime
-hours : int-minutes : int+Time()+Time(int, int)+setHours(int) : void+setMinutes(int) : void+getHours() : int+getMinutes() : int-extraF() : int
04/21/23 ECE 264: Lecture 15 6
Example 2: Time.hclass 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();
};
04/21/23 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
04/21/23 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
04/21/23 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
04/21/23 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
Example code: setOrigin()void Rectangle::setOrigin(double x, double y) {
origin.xCoord = x; // Won’t workorigin.setY(y);
}
Example shows two different ways of accessing elements of Point Directly changing private data still won’t work Must use set functions
04/21/23 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
04/21/23 ECE 264: Lecture 15 12
Example solutionsPoint 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);
}
04/21/23 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”
04/21/23 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
04/21/23 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)
{}
04/21/23 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, 8th ed. Etter & Ingber, Engineering Problem Solving with
C++, 2nd ed.
04/21/23 ECE 264: Lecture 15 17