Download presentation
Presentation is loading. Please wait.
Published byRosemary Bryant Modified over 9 years ago
1
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 11: Class diagrams; class relationships
2
Lecture outline Announcements / reminders Lab 5 has been be posted on Tuesday Today Review UML class diagrams Class relationships Association, aggregation, and composition Composition and initialization lists Modeling in UML Group discussion Example 12/17/2015 ECE 264: Lecture 11 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 12/17/2015 ECE 264: Lecture 11 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 12/17/2015 ECE 264: Lecture 11 4
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 12/17/2015 ECE 264: Lecture 11 5
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(); }; 12/17/2015 ECE 264: Lecture 11 6
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 12/17/2015 ECE 264: Lecture 11 7
8
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 12/17/2015 ECE 264: Lecture 11 8 Rectangle Point 1 -double width -double height -Point origin +Rectangle()+setOrigin() +getHeight()+setWidth() +getOrigin()+move() +getWidth()+area() +setHeight() 1 1
9
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 12/17/2015 ECE 264: Lecture 11 9
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) {} 12/17/2015 ECE 264: Lecture 11 10
11
Initialization list example Write a parameterized constructor for the Rectangle class that takes 4 arguments: Height Width X coordinate of the origin Y coordinate of the origin 12/17/2015 ECE 264: Lecture 11 11
12
Example solution Rectangle::Rectangle(double h, double w, double x, double y) : height(h), width(w), origin(x,y) {} 12/17/2015 ECE 264: Lecture 11 12
13
Group Discussion 4 students in a group (team members nearby you) Draw a UML Class Diagram (Three Boxes) “Person” class What data members? What member functions? “Events” class What data members? What member functions? Need a volunteer who presents his/her group discussion results on the blackboard 12/17/2015 ECE 264: Lecture 11 13
14
In-class example This C++ example shows how composition is used as three classes (time, date and event) are used to display the time and day of a particular event. (cited from Prof. G. Blake Stracener's Web) 12/17/2015 ECE 264: Lecture 11 14
15
Time Class /*Specification: This program displays how composition is used. Three classes display the hours, minutes, day, month, year, and name pertaining to an event*/ #include using namespace std; class Time { //Time class public: Time(); Time(int, int); void setTime(int, int); void getTime(int&, int&); void printTime(); void incrementHours(); void incrementMinutes(); private: int hr; int min; }; 12/17/2015 ECE 264: Lecture 11 15
16
Date Class class Date {//Date class public: Date(); Date(int, int, int); void setDate(int, int, int); void getDate(int&, int&, int&); void printDate(); private: int month; int day; int year; }; 12/17/2015 ECE 264: Lecture 11 16
17
Event Class class Event {//Event class public: Event(int hours = 0, int minutes = 0, int m = 1, int d = 1, int y = 1900, string name = "Christmas"); void setEventData(int hours, int minutes, int m, int d, int y, string name); void printEventData(); private: string eventName; Time eventTime; Date eventDay; }; 12/17/2015 ECE 264: Lecture 11 17
18
Main program int main() {//instantiate an object and set data for Christmas Event object; object.setEventData(6, 0, 12, 25, 2010, "Christmas"); //print out the data for object object.printEventData(); //instantiate the second object and set date for the fourth of July Event object2; object2.setEventData(1, 15, 7, 4, 2010, "Fourth of July"); //print out the data for the second object object2.printEventData(); return 0; } 12/17/2015 ECE 264: Lecture 11 18
19
Time.cpp (Time class implementation) Time::Time() { //default constructor hr = 0; min = 0; } Time::Time(int hours, int minutes) { //class time constructor that accepts parameters if(0 <= hours && hours < 24)//makes sure hours are valid hr = hours; else hr = 0; if(0 <= minutes && minutes < 60)//makes sure minutes are valid min = minutes; else min = 0; } 12/17/2015 ECE 264: Lecture 11 19
20
Time.cpp (Time class implementation) void Time::setTime(int hours, int minutes) { //sets a valid time if(0 <= hours && hours < 24) hr = hours; else hr = 0; if(0 <= minutes && minutes < 60) min = minutes; else min = 0; } void Time::getTime(int& hours, int& minutes) { //returns the hours and minutes hr = hours; min = minutes; } 12/17/2015 ECE 264: Lecture 11 20
21
Time.cpp (Time class implementation) void Time::printTime() { //displays the hours and minutes to the screen if(hr < 10) cout << "0"; cout << hr << ":"; if(min < 10) cout << "0"; cout << min << endl; } void Time::incrementHours() { //increments hours by one hr++; if(hr > 23) hr = 0; } 12/17/2015 ECE 264: Lecture 11 21
22
Time.cpp (Time class implementation) void Time::incrementMinutes() { //increments minutes by one min++; if(min > 59) { min = 0; incrementHours(); } 12/17/2015 ECE 264: Lecture 11 22
23
Date.cpp (Date class implementation) Date::Date() { //default constructor month = 1; day = 1; year = 1900; } Date::Date(int m, int d, int y) {//constructor that accepts parameters if(m >= 1 && m <= 12)//makes sure month is valid month = m; else month = 1; if(d >= 1 && d <= 31)//makes sure day is valid day = d; else day = 1; if(y >= 1900 && y <= 2010)//makes sure year is valid year = y; else year = 1900; } 12/17/2015 ECE 264: Lecture 11 23
24
Date.cpp (Date class implementation) void Date::setDate(int m, int d, int y) {//sets a valid date if(m >= 1 && m <= 12) month = m; else month = 1; if(d >= 1 && d <= 31) day = d; else day = 1; if(y >= 1900 && y <= 2010) year = y; else year = 1900; } 12/17/2015 ECE 264: Lecture 11 24
25
Date.cpp (Date class implementation) void Date::getDate(int& m, int& d, int& y) { //returns the month, day and year month = m; day = d; year = y; } void Date::printDate() { //displays the month, day and year to the screen if(month < 10) cout << "0"; cout << month << "/"; if(day < 10) cout << "0"; cout << day << "/"; cout << year; } 12/17/2015 ECE 264: Lecture 11 25
26
Event.cpp (Event class implementation) Event::Event(int hours, int minutes, int m, int d, int y, string name) : eventTime(hours, minutes), eventDay(m, d, y) { eventName = name; } void Event::setEventData(int hours, int minutes, int m, int d, int y, string name) { eventTime.setTime(hours, minutes); eventDay.setDate(m, d, y); eventName = name; } void Event::printEventData() { cout << eventName << " occurs "; eventDay.printDate(); cout << " at "; eventTime.printTime(); cout << endl; } 12/17/2015 ECE 264: Lecture 11 26
27
Final notes Next time Arrays, vectors, and other container classes Exam review 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. 12/17/2015 ECE 264: Lecture 11 27
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.