Object Oriented Programming Development - Week8 - PowerPoint PPT Presentation

About This Presentation
Title:

Object Oriented Programming Development - Week8

Description:

Now a quick comparison between public, private and protected attributes.. 19 ... Eg a car and its engine have a containment relationship. A Container ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 79
Provided by: std73
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Programming Development - Week8


1
Object Oriented ProgrammingDevelopment - Week8
  • Rob Manton
  • Email
  • Rob.Manton_at_luton.ac.uk
  • Room D104

2
Module Outline
  • Introduction
  • Non object oriented basics
  • Classes
  • Inheritance
  • Aggregation
  • Polymorphism
  • Multifile Development

3
Today
  • Some advice for next weeks test
  • Inheritance recap
  • Aggregation

4
Practical Test in Week 9
  • Tuesday 26th November in two sittings
  • 5-6pm and 6.15-7.15pm
  • Will involve writing a simple class
  • instantiating objects
  • other C constructs as practised in the lab
    sheets to date
  • inheritance and or aggregation

5
Last week Inheritance
  • Inheritance allows classes to inherit attributes
    and methods from other classes in a
    classification hierarchy

6
Last week Inheritance
  • Inheritance allows
  • specialisation
  • (extending the functionality of an existing
    class)
  • generalisation
  • (sharing commonality between two or more classes)

7
Last week Inheritance
  • Inheritance is appropriate where a class can be
    said to be a kind of other class

A car is a kind of vehicle car class can
inherit from vehicle class
Vehicle
Car
8
Last week Inheritance
  • Inheritance is appropriate where a class can be
    said to be a kind of other class

A wheel isnt a kind of car. A wheel is a part
of a car - this is dealt with by
aggregation which is this weeks topic
?
Car
Wheel
9
Last week Inheritance
  • Inheriting from a class doesnt affect the
    integrity of that class - objects of the original
    base class can still be created
  • Generalisation allows removal of redundancy and
    duplication among classes
  • Some base classes are abstract - they are not
    specific enough to be instantiated but act as
    holders for common attributes and methods of
    derived classes

10
Last week C Syntax
  • The colon () operator to denote inheritance
  • Public and private derivation of object methods
    from a base class
  • The protected keyword to allow derived classes
    to access inherited attributes

11
C Syntax public derivation
  • Class DerivedClass public BaseClass
  • private
  • int y
  • public
  • void setY(int y_in)
  • int getY()

A derived class. The colon operator means the
derived class inherits from the base class
12
C Syntax public derivation
  • Class DerivedClass public BaseClass
  • private
  • int y
  • public
  • void setY(int y_in)
  • int getY()

the public derivation means that objects of the
derived class can access the public methods and
attributes of the base class This is the most
usual type of derivation
13
C Syntax public derivation
  • class BaseClass
  • private
  • int x
  • public
  • void setX(int x_in)
  • int getX()
  • class DerivedClass public BaseClass
  • private
  • int y
  • public
  • void setY(int y_in)
  • int getY()

Main() BaseClass base_object DerivedClass
derived_object base_object.setX(7) derived_obj
ect.setX(12) derived_object.setY(1) return 0
Object of the derived class can access methods of
the derived class and also methods of the base
class
14
C Syntax public derivation
Publicly derived class objects of this class
are able to call public methods of the base
class Public derivation is the most common type
15
C Syntax private derivation
Changed to a Privately derived class objects of
this class are NOT able to call public methods of
the base class - this wont compile!
16
Last week public/private inheritance
  • Public inheritance allows all public methods of
    ancestor classes to be available to objects of a
    derived class

Line
Class ColouredLine public Line . main()
ColouredLine myLine myLine.draw()
Attributes start position end position
Methods draw
Coloured Line
Attributes colour
Object of derived class can access method of base
class
Methods setColour
17
Last week public/private inheritance
  • Private inheritance prevents objects of the
    derived class from calling the public methods of
    the ancestor class

Line
Class ColouredLine private Line . main()
ColouredLine myLine // myLine.draw()
Attributes start position end position
Methods draw
?
Coloured Line
Attributes colour
Methods setColour
Object of privately derived class cant access
method of base class
18
private/public/protected attributes
  • Now a quick comparison between public, private
    and protected attributes..

19
C Syntax private attributes
We have a privately defined attribute x in the
base class
20
C Syntax private attributes
Here we have a method in the derived class
(getIfYEqX) that needs to access an attribute (x)
declared in the base class
21
C Syntax private attributes
Because x is declared as private in the base
class, the derived class is not allowed to access
it directly so we get an error and the code wont
compile
22
C Syntax public attributes
If we change the attribute to be public then the
method in the derived class can access it but...
23
C Syntax public attributes
Doing this goes against the idea of encapsulation
and data hiding users of the class can access
the attributes directly which is a bad idea
usually!
24
C Syntax protected attributes
If we change the attribute to be protected then
the method in the derived class can access the
attribute in the base class..
25
C Syntax protected attributes
And the notion of encapsulation and data hiding
is preserved because users of the class arent
allowed to access the protected attribute directly
26
C Syntax the protected keyword
  • So in summary...Protected attributes share some
    features of public and some of private
  • Derived classes can access the protected
    attributes of their ancestor classes
  • (as if they were publicly defined)
  • but external users of the class cant access the
    protected attributes
  • (as if they were privately defined)

27
C Syntax inheriting constructors
  • A derived class always inherits the constructor
    of the base class. The base class constructor is
    called first.
  • If the base class constructor takes no parameters
    then the inheritance is implicit - you dont need
    to do anything!
  • If the base class constructor takes parameters
    then each derived class needs to declare a
    constructor with the same parameters. You can
    pass the arguments given to the derived class
    constructor to the constructor for the base class

28
C Syntax inheriting constructors
Base class declares a constructor that takes a
char pointer parameter
  • Class Customer
  • Customer (char name_in)
  • Class AccountCustomerpublic Customer
  • AccountCustomer(char name_in)
  • ..

Derived class declares a constructor that takes
the same char pointer parameter
29
C Syntax inheriting constructors
  • AccountCustomer AccountCustomer(char name_in)
  • Customer (name_in)
  • //main body of constructor..

In the implementation of the constructor for the
derived class, the parameter passed to
the derived class constructor is passed down to
the base class constructor. Note use of the
colon () syntax once again
30
C Syntax inheriting constructors
  • class creature
  • private
  • int yearOfBirth
  • public
  • creature(int YOB)
  • int getYearOfBirth()
  • int main()
  • creature myCreature(1985)
  • cout ltlt "my creature was born in " ltlt
    myCreature.getYearOfBirth() ltltendl
  • return 0

This class has a constructor that takes an
integer argument.
When instantiating an object of this class you
pass a parameter to the constructor.
31
C Syntax inheriting constructors
Dog class derived from creature class
  • class dogpublic creature
  • public
  • void bark()
  • int main()
  • creature myCreature(1985)
  • dog myDog(1985)
  • cout ltlt "my creature was born in " ltlt
    myCreature.getYearOfBirth() ltltendl
  • return 0

At the moment we cant do this there is no
constructor for the dog class that takes an
integer argument
32
C Syntax inheriting constructors
  • class dogpublic creature
  • public
  • dog(int YOB)
  • void bark()
  • //implementation for dog constructor
  • dogdog(int YOB)
  • creature(YOB)
  • //other constructor stuff goes here

Now we have defined a constructor that does
take an integer argument
The argument sent to the dog constructor gets
sent to the creature constructor so the
YearOfBirth attribute of the base class gets
set properly
33
C Syntax inheriting constructors
Now we do have an appropriate constructor for the
dog class which correctly initialises
the attributes defined in the base class
  • class dogpublic creature
  • public
  • dog(int YOB)
  • void bark()
  • int main()
  • creature myCreature(1985)
  • dog myDog(1985)
  • cout ltlt "my creature was born in " ltlt
    myCreature.getYearOfBirth() ltltendl
  • cout ltlt "my dog was born in " ltlt
    myDog.getYearOfBirth() ltltendl
  • return 0

34
C Syntax inheriting destructors
  • A derived class always inherits the destructor of
    the base class. The derived class destructor is
    called first. This is the reverse of the sequence
    for constructors
  • Because destructors never take an argument there
    is no issue with inheritance of destructor
    parameters.

35
This weeks new topic
  • Associations
  • links between objects
  • Aggregations
  • objects composed wholly or partly of others

36
Associations
  • Up to now we have made single objects and called
    their methods from the main() function.
  • In practice it is likely that we will have many
    objects that will need to communicate with each
    other..

37
Types of associations
  • Associations can take many forms
  • one to one
  • one to many
  • many to many
  • bidirectional
  • unidirectional

38
Types of associations
  • Associations can take many forms
  • one to one
  • one object of a class has a link to one other
    object of a class
  • one to many
  • many to many
  • bidirectional
  • unidirectional

39
Types of associations
  • Associations can take many forms
  • one to one
  • one to many
  • one object of a class has many links with objects
    of a particular class
  • many to many
  • bidirectional
  • unidirectional

40
Types of associations
  • Associations can take many forms
  • one to one
  • one to many
  • many to many
  • many objects of one class have links with many
    objects of a particular class
  • bidirectional
  • unidirectional

41
Types of associations
  • Associations can take many forms
  • one to one
  • one to many
  • many to many
  • bidirectional
  • messages can be sent in both directions
  • unidirectional

42
Types of associations
  • Associations can take many forms
  • one to one
  • one to many
  • many to many
  • bidirectional
  • unidirectional
  • messages only need to be sent in one direction.
  • One object is the actor - acts upon another
  • the other is a server - it is only acted upon.
    Booch,1994

43
Aggregation
  • Aggregations
  • Up to now we have made classes that use simple
    data types for their attributes - (int, float,
    char etc)
  • Aggregations use objects of other classes to
    define their state

44
Terms used to describe aggregation
  • Aggregation
  • Composition
  • Part-Whole
  • A part of (APO)
  • Has-a
  • Containment

45
Aggregation
  • Aggregation

46
Composition vs Aggregation
  • Composition Implies that the internal objects are
    not seen from the outside.
  • Aggregation implies that some objects have some
    visibility or existence outside of the hierarchy

47
Containment
  • Containment
  • a composition hierarchy defines how an object is
    composed of other objects in a fixed
    relationship. Aggregate object cannot exist
    without its components.
  • Eg a car and its engine have a containment
    relationship
  • A Container
  • an object of a container class which is able to
    contain other objects. Its existence is
    independent of whether it actually contains
    anything. Contained objects will probably be
    dynamic and vary over time.
  • Eg a car boot is a container which is able to
    contain some or no objects

48
Aggregations
  • Aggregations can be
  • fixed
  • - like in composition - the number and identity
    of contained objects is always the same
  • variable
  • - as with a container object - the number of
    contained objects may vary at runtime
  • recursive
  • may contain components of its own type

49
Fixed Aggregation in C
  • Fixed aggregation - declare attributes as objects
    rather than standard data types
  • class car
  • private
  • engine myEngine
  • light left_headlight
  • light right_headlight

Class attributes now include objects of other
classes
50
Fixed Aggregation in C
  • aggregated class can then call methods of the
    component objects
  • void
  • carLightsOn()
  • left_headlight.switchOn()
  • right_headlight.switchOn()

Car class methods can now call methods of the
contained objects
51
Fixed Aggregation with parameters
  • What if the contained objects require parameters
    for their construction?
  • class wheel
  • private
  • int diameter
  • public
  • wheel(diameter_in)
  • int getDiameter()

Constructor for wheel class takes an
integer argument to define its diameter
52
Fixed Aggregation with parameters
  • What if the contained objects require parameters
    for their construction?
  • class car
  • private
  • wheel l_front, r_front, l_back, r_back
  • engine theEngine
  • public
  • car (int diameter_in,int enginecc_in)

Constructor for car class takes enough parameters
to construct all the constituent objects
53
Fixed Aggregation with parameters
  • What if the contained objects require parameters
    for their construction?
  • carcar (int diameter_in,int enginecc_in)
  • l_front(diameter_in),
  • r_front(diameter_in),
  • l_back(diameter_in),
  • r_back(diameter_in),
  • theEngine(enginecc_in)
  • //rest of constructor code goes here

Note use of colon() operator to pass parameters
sent to constructor of aggregated object to the
constructors of the constituent objects
54
Variable Aggregation in C
  • Variable aggregation - declare attributes as
    object pointers rather than standard data types.
    Pointers can be set to NULL to represent no
    object
  • class car
  • private
  • person theDriver NULL
  • ..

55
Variable Aggregation in C
  • Variable aggregation - declare attributes as
    object pointers rather than standard data types.
    Pointers can be set to NULL to represent no
    object
  • class car
  • private
  • person theDriver NULL
  • ..

Class attributes now include pointers to objects
of other classes
56
Variable Aggregation in C
  • caraddDriver()
  • theDrivernew person()

We could have a method which instantiates an
object of the contained class and allocates it to
the pointer like this..
57
Variable Aggregation in C
  • caraddDriver()
  • theDrivernew person()
  • carremoveDriver()
  • delete the Driver
  • theDriverNULL

..and deletes it like this
58
Variable Aggregation in C
  • However, some objects in an aggregation may have
    a lifetime outside of the contained object. In
    this case a pointer to the previously existing
    object needs to be passed to the containing
    object
  • caraddDriver(person driver_in)
  • theDriverdriver_in

59
Variable Aggregation in C
  • However, some objects in an aggregation may have
    a lifetime outside of the contained object. In
    this case a pointer to the previously existing
    object needs to be passed to the containing
    object
  • caraddDriver(person driver_in)
  • theDriverdriver_in

A pointer to a previously existing object is now
available to the aggregated object
60
Variable Aggregation in C
  • Removing the object from the aggregation simply
    means setting the pointer to null. (the object
    continues to exist in its own right but not as
    part of the aggregation)
  • carremoveDriver()
  • theDriverNULL

61
Variable Aggregation in C
  • Removing the object from the aggregation simply
    means setting the pointer to null. (the object
    continues to exist in its own right but not as
    part of the aggregation)
  • carremoveDriver()
  • theDriverNULL

The car object would now have no driver
pointer but the driver person object would
continue to exist outside of the car
62
Implementing associations in C
  • forms of Associations
  • one to one
  • one to many
  • many to many
  • bidirectional
  • unidirectional

63
Implementing associations in C
  • forms of Associations
  • one to one unidirectional
  • eg a light switch and a light
  • the switch is an actor - it acts upon another
    object Brooch
  • the light is a server -it is told what to do
  • switch needs a pointer to the light object to be
    able to control it

64
Implementing associations in C
  • forms of Associations
  • one to one unidirectional
  • main()
  • light bigLight new light()
  • button lightButtonnew button(bigLight)
  • ..

Create a dynamic light object pass its pointer to
the constructor for the new button object button
object can now call methods of the light object
65
Implementing associations in C
  • forms of Associations
  • one to one bidirectional
  • eg between two people objects who become
    married
  • two person objects of the same class
  • each object needs a pointer to the other object
    (its partner)

66
Implementing associations in C
  • forms of Associations
  • one to one bidirectional
  • class person
  • private
  • person partner
  • ..

The person class now contains a pointer
to another object of the person class. Initially
this will be set to NULL, but when the
person gets married the partner pointer
will point to another object of the person class
67
Implementing associations in C
  • forms of Associations
  • one to one bidirectional
  • main()
  • person Frednew person()
  • person Wilmanew person()
  • Fred.marry(Wilma)
  • Wilma.marry(Fred)
  • ..

68
Implementing associations in C
  • forms of Associations
  • one to one bidirectional
  • main()
  • person Frednew person()
  • person Wilmanew person()
  • Fred.marry(Wilma)
  • Wilma.marry(Fred)
  • ..

We can create two dynamic person objects and then
call the marry method from each. This
repetition is necessary to ensure both partners
know who they are married to!
69
Implementing associations in C
  • forms of Associations
  • one to one bidirectional
  • a method of establishing both ends of the
    bidirectional link simultaneously is to use the
    this keyword
  • All objects in C have a this pointer which
    points to themselves

70
Implementing associations in C
  • forms of Associations
  • one to one bidirectional
  • personmarry(person partner_in)
  • //establish one way link
  • partnerpartner_in
  • //now do the other way
  • partner-gtmarry(this)
  • ..

When the marry method of one object is called
it automatically calls the marry method of its
new spouse!
71
Implementing associations in C
Need to avoid an infinite recursive call here!
72
Implementing associations in C
  • forms of Associations
  • multiple associations
  • Maintain an array of pointers and an integer
    attribute to record the current number of objects
    referenced.
  • CaraddPassenger(person passenger_in)
  • if (numPassengerslt5)
  • passengernumPassengerspassenger_in

73
Implementing associations in C
  • forms of Associations
  • multiple associations
  • Maintain an array of pointers and an integer
    attribute to record the current number of objects
    referenced.
  • caraddPassenger(person passenger_in)
  • if (numPassengerslt5)
  • passengernumPassengerspassenger_in
  • ..

numPassengers is an integer attribute of the car
class If the car is not full then add the new
passenger
74
Implementing associations in C
  • forms of Associations
  • multiple associations
  • Maintain an array of pointers and an integer
    attribute to record the current number of objects
    referenced.
  • caraddPassenger(person passenger_in)
  • if (numPassengerslt5)
  • passengernumPassengerspassenger_in
  • ..

Here we use the postfix increment operator to
update the numPassengers variable
75
Summary
  • Associations
  • links between objects
  • Aggregations
  • objects composed wholly or partly of others

76
Summary associations
  • forms of Associations
  • one to one
  • one to many
  • many to many
  • bidirectional
  • unidirectional

77
Summary Aggregation
  • Composition Implies that the internal objects are
    not seen from the outside.
  • Aggregation implies that some objects have some
    visibility or existence outside of the hierarchy

78
Summary Aggregation
  • Aggregations can be
  • fixed
  • - like in composition - the number and identity
    of contained objects is always the same - use
    named automatic objects
  • variable
  • - as with a container object - the number of
    contained objects may vary at runtime - use
    pointers which can be NULL
  • recursive
  • may contain components of its own type
Write a Comment
User Comments (0)
About PowerShow.com