250 likes | 352 Views
ECE 264 Object-Oriented Software Development. Instructor: Dr. Honggang Wang Fall 2012 Lecture 16: Class diagrams; class relationships. Lecture outline. Announcements / reminders Project groups: e-mail Dr. Wang ( hwang1@umassd.edu ) by Fri., 10/19 Groups of 3 or 4 students
E N D
ECE 264Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 16: Class diagrams; class relationships
Lecture outline • Announcements / reminders • Project groups: e-mail Dr. Wang (hwang1@umassd.edu) by Fri., 10/19 • Groups of 3 or 4 students • Those who don’t choose a group will be randomly assigned • Can e-mail me with “sub-group”; I’ll fill rest of group • Today • Review • UML class diagrams • Association, aggregation, and composition • Initialization lists • Composition Example ECE 264: Lecture 16
Review • 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 16
Example: Class diagram ECE 264: Lecture 16
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 16
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 16
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 16
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 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 16
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 ECE 264: Lecture 16
Example solution Rectangle::Rectangle(double h, double w, double x, double y) : height(h), width(w), origin(x,y) {} ECE 264: Lecture 16
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) ECE 264: Lecture 17
Date 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<iostream> • #include<string> • 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; }; ECE 264: Lecture 17
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; • }; ECE 264: Lecture 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; • }; ECE 264: Lecture 17
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; • } ECE 264: Lecture 17
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; • } ECE 264: Lecture 17
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; • } ECE 264: Lecture 17
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; • } • } ECE 264: Lecture 17
Time.cpp (Time class implementation) • void Time::incrementMinutes() • { //increments minutes by one • min++; • if(min > 59) • { • min = 0; • incrementHours(); • } ECE 264: Lecture 17
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; • } ECE 264: Lecture 17
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; • } ECE 264: Lecture 17
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; • } ECE 264: Lecture 17
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; • } ECE 264: Lecture 17
Final notes • Next time • In-class code example: composition • 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 16