Object Oriented Systems - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Object Oriented Systems

Description:

Prelude: A sly shortcut. 1st Movement: What is Inheritance? 2nd Movement: Protected Members ... PRELUDE - cheeky initialisation ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 42
Provided by: scie205
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Systems


1
Object Oriented Systems
  • Lecture 10

2
Special Classes Next Week
  • Extra Classes
  • Wednesdays 12.00-1.00 pm B53.
  • Stefan Senk (sms_at_cs.nott.ac.uk)
  • Coursework Deadline
  • Friday March 18th (14 days away)
  • There will be exercises over the easter holiday
    but no coursework. Use that time to improve!
  • Today
  • Inheritance

3
Previously
  • Copy Constructors
  • Destructors
  • Assignment Operator Overloading
  • These were the big 3, necessary if you have DMA
    in your class. If you need one, you need all of
    them.

4
Todays Symphony
  • Prelude A sly shortcut.
  • 1st Movement What is Inheritance?
  • 2nd Movement Protected Members
  • 3rd Movement Multiple Inheritance
  • 4th Movement Constructors in Inheritance
  • Coda Points to remember

5
Curtain Call Todays Bjarney
6
PRELUDE - cheeky initialisation
  • Weve seen how constructors are often used for
    initialising variables
  • class president
  • public
  • president()
  • string name
  • int age
  • int IQ
  • presidentpresident()
  • name George Bush
  • age 59
  • IQ NULL

7
A quicker method to use
  • Another technique is as follows
  • presidentpresident() name(George Bush),
  • age(59),
  • IQ(NULL)
  • Or generically it might be
  • presidentpresident(string n, int a, int i)
  • name(n), age(a), IQ(i)

Bit unsightly, but more efficient style than the
previous slide
8
1st Movement - Inheritance
  • A key feature of Object Orientation.
  • Inheritance is the ability to define new classes
    using existing classes as a basis.
  • New class inherits all the attributes and
    behaviours of its parent.
  • It can have extra attributes of its own
  • If your going to be a top software designer or
    manager youve got to get your head round it.

9
The Inheritance concept
  • Modelled on biological Inheritance
  • We all have common genes with our ancestors -
    Class Inheritance is analogous
  • We can use inheritance to design complex systems
    It provides a way of organising components into
    a hierarchical structure.
  • These can produce complex systems, but it is also
    there to help us.

10
Example Music Format
Music Format
Analog Format
Digital Format
Magnetic Tape
DAT
MP3
Vinyl
Disk
12 Record
7 Record
MiniDISC
CD
11
Levels of Abstraction
Shape
HIGH LEVEL
MEDIUM LEVEL
Parallelogram
LOW LEVEL
Diamond
  • Inheritance is based on the relationship
  • is-a
  • Whereas member variable represent the
    relationships
  • has-a
  • uses-a

12
The key to Object Orientation
  • is determining the level of abstraction your
    base classes should be.
  • It is easy to make these either too specific or
    not specific enough.
  • For example if the base abstraction for passenger
    vehicles contains an attribute for no. of wheels,
    we could get in trouble - This example is
    potentially too specific.
  • But if we do not add a variable for no. of
    passengers we will have to add it to every child
    class manually now the base class was too
    general.

13
Good Old Shape Example
Shape
Regular Shape
Irregular Shape
Parallelogram
Triangular
Circle
Diamond
Rectangle
14
Setting up a Child Class
  • class inheritance uses the following general
    form
  • class derivedClassaccess baseClass
  • // body of the class

a SINGLE colon
Normal Class Keyword
The type of access the new class has
The original class that we are using as our base
The name of our new class
15
The access keyword
  • Now, this can get confusing because, just like
    member variables the access keyword can also be
  • Private
  • Public
  • Protected
  • If you use public accessor all variables apart
    from the private ones are sent across to our new
    class, as they are. Public inheritance is the
    most widely used inheritance.
  • This is important as it means that only
    non-private members of your base class can ever
    be inherited.

16
Public Inheritance
  • Below is a graphical representation of public
    inheritance.
  • Public variables of the parent class become
    public members of the child class, but the
    original private members are not inherited.

base-class
derived class
public inheritance
PUBLIC members
PUBLIC members
PRIVATE members
17
Polygon Example
  • class polygon
  • public
  • int width, height
  • void set_values (int a, int b) widtha
    heightb
  • class rectangle public polygon
  • public
  • int area () return (width height)
  • class triangle public polygon
  • public
  • int area () return (width height / 2)

18
using our inherited items
  • int main ()
  • rectangle rect
  • triangle tri
  • rect.set_values (4,5)
  • tri.set_values (4,5)
  • cout ltlt rect.area() ltlt endl
  • cout ltlt tri.area() ltlt endl
  • return EXIT SUCCESS

Have same member variable types but different
area methods
19
Children have the same Imprint
  • Items in an inheritance always have the same
    imprint as their base class. What does this mean?
  • This is especially useful in containers such as
    vectors. In C vectors only take items of a
    single type. But they can also take items of a
    sub-class in that same vector.

your program object code
Other object
Rectangle object
101011111001010101011 10 1110
1100 10111100010010 110100101
Triangle object
20
Vector Example
  • If we declare a vector as holding a base class we
    can add sub-classed objects to it as well
  • int main ()
  • vectorltpolygongt v
  • polygon hexagon
  • rectangle rect
  • triangle tri
  • v.push_back(hexagon)
  • v.push_back(rect)
  • v.push_back(tri)

Fine as all these items are in the same
inheritance tree and so have the same imprint.
21
2nd Movement - Protected Variables
  • A section of a class may be set as Protected -
    but this keyword causes a lot of confusion.
  • Protected variables are like private variables in
    many ways other parts of your program are not
    allowed access to them.
  • But they can of course be accessed by members of
    the class that they are declared in.
  • So what on earth is the difference between the
    private and protected sections of a class?

22
The difference is
  • They exist as a tool for inheritance. They are
    just like private members except
  • If the base class is inherited using the public
    keyword then, the protected members become
    protected members of the derived class too

base-class
derived class
public inheritance
PUBLIC members
PUBLIC members
public inheritance
PROTECTED members
PROTECTED members
PRIVATE members
23
Play it safe use Protection
  • Hence you can create member variables which are
    essentially private to the class but can still be
    inherited
  • class coordinates
  • private
  • int x,y
  • public
  • coordinates(int _x, int _y) x_x y_y
  • display() cout ltlt ( ltlt x ltlt , ltlt y ltlt )
    ltlt endl
  • class derived public coordinates
  • int steps
  • public
  • distance() return xysteps
  • showSteps() cout ltlt steps ltlt endl

protected
24
99 Protection
  • You can of course chain your derived classes
    keeping these protected variables

Shape
Regular Shape
PUBLIC
PUBLIC members
Parallelogram
PROTECTED
PUBLIC members
PROTECTED members
PRIVATE
PROTECTED members
25
Back to Access
  • Keep very clear in you head that member variables
    can be
  • public
  • private
  • protected
  • The Inheritance mechanism also uses an access
    keyword which can be
  • public
  • private
  • protected
  • Up to this point we have looked at all the member
    variables but only considered access using public
    inheritance.

26
Private Inheritance
  • We have already considered what happens when we
    use the private and public inheritance access.
  • Private Access means that all of the public and
    protected variables of the parent class become
    private in the child.

base-class
derived class
private inheritance
PRIVATE members
PUBLIC members
private inheritance
PROTECTED members
PRIVATE members
PRIVATE members
27
3rd Movement - Multiple Inheritance
  • Your sub-classes can potentially have one parent,
    a mom and a dad, or in fact lots of parents. The
    syntax for this is simple
  • class derivedClassaccess base1, access base2,
    .
  • // body of the class
  • For example, maybe youve some how managed to
    record digitally onto vinyl, then from our music
    example we might have a derived class
  • class digiVinylpublic Vinyl, public Digital

just list all parents here
28
Coordinates example
  • class Xcoordinate
  • protected
  • int x
  • public
  • displayX() cout ltlt x is ltlt x ltlt endl
  • class Ycoordinate
  • protected
  • int y
  • public
  • displayY() cout ltlt y is ltlt y ltlt endl
  • class XYcoordinate public Xcoordinate, public
    Ycoordinate
  • public

29
Testing it out
  • include coords.h
  • int main()
  • XYcoordinate treasure
  • treasure.setXY(25,17)
  • treasure.displayX()
  • treasure.displayY()
  • return EXIT_SUCCESS

instance of the child class
from the derived class
from Xcoordinate class
From Ycoordinate class
OUTPUT x is 25 y is 17
30
4th Movement - Constructors (again)
  • Last few lectures weve done a huge amount on
    constructors and destructors.
  • When we create an instance of a derived class
    does the parent constructor or the child
    constructor execute?
  • BOTH
  • But in What order? And how do we know which one
    we are passing parameters to?

31
Proof of the Pudding
  • class base
  • public
  • base() cout ltlt Creating base ltlt endl
  • base() cout ltlt Killing base ltlt endl
  • class derivedpublic base
  • public
  • derived() cout ltlt Creating derived ltlt
    endl
  • derived() cout ltlt Killing derived ltlt
    endl
  • int main()
  • derived tester
  • return EXIT_SUCCESS

OUTPUT Creating base Creating derived Killing
derived Killing base
1
32
Same for even more children
  • class derivedpublic base
  • public
  • derived() cout ltlt Creating derived\n
  • derived() cout ltlt Killing derived\n
  • class lastOnepublic derived
  • public
  • derived() cout ltlt Creating lastOne\n
  • derived() cout ltlt Killing lastOne\n
  • int main()
  • lastOne tester
  • return EXIT_SUCCESS

OUTPUT Creating base Creating derived Creating
lastOne Killing lastOne Killing derived Killing
base
1
33
And Multiples. SAME AGAIN!
  • class Xcoordinate
  • public
  • Xcoordinate () cout ltlt Creating X ltlt endl
  • Xcoordinate() cout ltlt Killing X ltlt endl
  • int x
  • class Ycoordinate
  • public
  • Ycoordinate () cout ltlt Creating Y ltlt endl
  • Ycoordinate() cout ltlt Killing Y ltlt endl
  • int y

34
(continued)
  • class XYcoordinate public Xcoordinate,
  • public Ycoordinate
  • public
  • XYcoordinate (int a, int b)
  • cout ltlt Creating coordinate ltlt endl
  • XYcoordinate ()
  • cout ltlt Killing coordinate ltlt endl
  • int main()
  • XYcoordinate test_coordinate
  • return EXIT_SUCCESS

public Ycoordinate, public Xcoordinate
OUTPUT Creating X Creating Y Creating
coordinate Killing coordinate Killing Y Killing X
1
35
Constructor Confusion
  • So with all these constructors firing off left,
    right and centre which one takes our parameters?
  • Well, as we saw each constructor is invoked
    systematically
  • If only the sub-class requires parameters there
    is no problem as it is obvious where they are
    going.
  • But if we want to send parameters to both the
    child and the parent class constructors then we
    have to use a special syntax

36
Passing on Constructor Parameters
  • Example constructor
  • child_classchild_class(arguments)
    parent_class(arguments)
  • If the child is the bottom of a multiple
    inheritance then we can expand this to use
  • child_classchild_class(arguments)
  • parent1_class (arguments),
  • parent2_class(arguments)

37
Seeing this in action
  • class coordinates
  • int x,y
  • public
  • coordinates (int _x, int _y) x_x y_y
  • display() cout ltlt ( ltlt x ltlt , ltlt y ltlt )
    ltlt endl
  • class distance public direction
  • int steps
  • public
  • distance(int _x, int _y, int s) coordinates
    (_x, _y)stepss
  • showSteps() cout ltlt steps ltlt endl
  • int main()
  • coordinates digHere(100, 3, 3)

Here we pass on parameters to the parents
constructor
38
And if its even more complex
  • The same rules apply. If we had a chain of
    classes being inherited the constructors for each
    one must take into account their parents
  • distancedistance(int _x, int _y, int _a, int
    _b, int s)
  • startPoint(_x, _y, _a, _b)
  • steps s
  • startPointstartPoint (int _x, int _y, int _a,
    int _b)
  • coordinates (_x, _y)
  • a _a
  • b _b

Constructor for the child
Constructor for the new parent sending info to
the grand parent class.
39
Coda Points to Remember
  • The is-a relationship relationship indicates
    inheritance
  • a dwarf is-a type of person (yet sadly
    disadvantaged)
  • drum bass is-a type of music
  • garage is-not
  • This relationship is transitive Scooby Doo is-a
    type of dog, a dog is-a mammal and therefore
    Scooby Doo is a mammal.
  • The has-a relationship indicates containment.
    Aggregate objects are constructed using
    containment

40
Even more Points to Remember
  • A class created from an existing class using
    inheritance is called a derived class or a
    subclass.
  • The parent is called the base class or
    superclass.
  • I recommend for now you use public inheritance as
    then we always know that
  • Public variables of the base class are public
    members of the derived class.
  • Private variables of the base class are not
    accessible by the member functions of the derived
    class.
  • Protected members of the base class become
    private members of the derived class.

41
Next Lecture
  • If you have learnt everything up to you will be
    have good knowledge in intermediate C. Monday
    you graduate part 2 of the course with
  • Virtual Functions
  • Streams
  • Debugging
  • Putting it everything together in the
  • grand data structure experiment.
Write a Comment
User Comments (0)
About PowerShow.com