ECE 538 Object Oriented Concepts Session 9 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

ECE 538 Object Oriented Concepts Session 9

Description:

Class association actually implies that the objects of the classes have some ... If class B is derived from Class A, then B is a kind of A ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 19
Provided by: johngwe
Category:

less

Transcript and Presenter's Notes

Title: ECE 538 Object Oriented Concepts Session 9


1
ECE 538 Object Oriented ConceptsSession 9
  • Dr. John G. Weber
  • John.Weber_at_notes.udayton.edu
  • http//academic.udayton.edu/JohnWeber

2
Class Relationships
  • Some Important Class Relationships
  • Inheritance
  • Association
  • Aggregation
  • Composition

3
Inheritance
  • Discusses previously

4
Association
  • Concept
  • Real-World entities represented by classes in
    program have some kind of obvious relationship
  • For Example, drivers are related to cars, books
    are related to libraries, race horses are related
    to race tracks
  • If such entities were classes in a program, the
    relationship would be called association
  • Class association actually implies that the
    objects of the classes have some kind of
    relationship rather than the classes themselves
  • Two classes are associated if an object of one
    class calls a member function of an object of
    another class.
  • Two classes are associated if an attribute of one
    class is an object of the other class

5
Types of Association
  • Unary (Unidirectional)
  • Object of one class calls function from object of
    another class
  • Binary (bidirectional)
  • Objects of each class call functions from objects
    of the other class

6
Example
/times2.cpp //converts from time24 to time12
using constructor in time12 include
ltiostreamgt include ltstringgt using namespace
std class time24 private int
hours //0 to 23 int
minutes //0 to 59 int
seconds //0 to 59 public
//no-arg constructor
time24() hours(0), minutes(0), seconds(0)
time24(int h, int m, int s) hours(h),
minutes(m), seconds(s) //3-arg constructor
void display() const //format
231501 if(hours lt 10)
cout ltlt '0' cout ltlt hours ltlt ''
if(minutes lt 10) cout ltlt '0' cout
ltlt minutes ltlt '' if(seconds lt 10)
cout ltlt '0' cout ltlt seconds
int getHrs() const return hours
int getMins() const return minutes
int getSecs() const return seconds
7
Example (Cont)
class time12 private bool pm
//true pm, false am
int hrs //1 to 12
int mins //0 to 59
public //no-arg
constructor time12() pm(true), hrs(0),
mins(0) time12(time24)
//1-arg constructor
//3-arg constructor
time12(bool ap, int h, int m) pm(ap), hrs(h),
mins(m) void display() const
cout ltlt hrs ltlt ''
if(mins lt 10) cout ltlt '0' //extra zero for
"01" cout ltlt mins ltlt ' '
string am_pm pm ? "p.m." "a.m."
cout ltlt am_pm
8
Example (Cont)
time12time12( time24 t24 ) //1-arg
constructor
//converts time24 to time12 int hrs24
t24.getHrs() //get hours
//find am/pm pm
t24.getHrs() lt 12 ? false true
mins
(t24.getSecs() lt 30) ? //round secs
t24.getMins() t24.getMins()1
if(mins 60) //carry mins?
mins0 hrs24
if(hrs24 12 hrs24 24) //carry hrs?
pm (pmtrue) ? false true //toggle
am/pm hrs (hrs24 lt 13) ? hrs24
hrs24-12 //convert hrs if(hrs0)
//00 is 12 a.m. hrs12
pmfalse
9
Example (Cont)
int main() int h, m, s while(true)
//get 24-hour
time from user cout ltlt "Enter 24-hour time
\n" cout ltlt " Hours (0 to 23) " cin gtgt
h if(h gt 23) //quit if
hours gt 23 return(1) cout ltlt "
Minutes " cin gtgt m cout ltlt " Seconds
" cin gtgt s time24 t24(h, m, s)
//make a time24 cout ltlt "You entered "
//display the time24 t24.display()
time12 t12 t24 //convert
time24 to time12 cout ltlt "\n12-hour time
" //display equivalent time12
t12.display() cout ltlt
"\n\n" return 0
10
Navigability
  • Direction of association
  • e.g. if you know object of class one, you can
    quickly find obj of class 2
  • time12 class calls time24 functions to support
    time conversion
  • i.e. since we know time24 obj, we can easily
    convert to time12 obj with the classes as defined

11
Aggregation Classes within Classes
  • If class B is derived from Class A, then B is a
    kind of A
  • B has all characteristics of A plus some of its
    own
  • Aggregation is called a has a relationship
  • A library has a book
  • A car has a door
  • Aggregation may occur when one object is an
    attribute of another

Class A Class B A objA // define obj
A as an object of class A
12
UML for Aggregation
13
Aggregation Example
  • Recall the Multiple Inheritance Example
  • Restructure to use aggregation instead

14
Mini-program Example
class student class employee class
manager student stu //stu is an object of
class student employee emp //emp is an object
of class employee class scientist student
stu //stu is an object of class
student employee emp //emp is an object of
class employee class laborer employee
emp //emp is an object of class employee
15
Assignment
  • Convert your multiple inheritance program to use
    aggregation instead

16
Composition
  • Stronger form of aggregation
  • All the characteristics of aggregation plus two
    more
  • Part may belong to only one whole
  • Lifetime of part is same as lifetime of whole
  • Composition is a consists of relationship

17
Multiplicity Symbols
18
Assignment
  • Due June 15
  • Rework the inheritance example to use aggregation
  • Due June 22
  • Restructured Cannon Program
Write a Comment
User Comments (0)
About PowerShow.com