oop, inheritance and polymorphism lecture 6. object relations inheritance is ‘a kind of’,...

29
OOP, Inheritance and Polymorphism Lecture 6

Upload: zoe-payne

Post on 20-Jan-2018

229 views

Category:

Documents


0 download

DESCRIPTION

Composition  Clock comprises of Case Clock face Minute hand Hour hand Works  It is not a clock without these objects!

TRANSCRIPT

Page 1: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

OOP, Inheritance and Polymorphism

Lecture 6

Page 2: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Object relations Inheritance

is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun

Aggregation ‘comprises’, ‘consists’, ‘ is made of’ etc. e.g. Our gun has a chamber, Our player has an

inventory, Our map consists of locations Association

is related to by some action e.g. musician plays the piano, cowboy fires a gun

Page 3: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Composition Clock comprises of

Case Clock face Minute hand Hour hand Works

It is not a clock without these objects!

Page 4: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…
Page 5: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…
Page 6: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Inheritance Inheritance allows code reuse by enabling the

creation of sub-classes based on base classes

Syntax of inheritance:class subclass: access_level

baseclass1,access_level baseclass2{}; Class Dog : public Mammal, public

Canine Avoid multiple inheritance

Page 7: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Inheritance

When a sub class is instantiated the base class constructor is called by default

The access level describes the maximum access level that members of the sub class will have

The default access level is private for classes and public for structs

Page 8: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Sub classes Do not inherit

Constructors Destructors Copy constructors Friends

Page 9: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Protected Private members are only accessible

in the base class Protected members are accessible in

the child class Non-static protected variables are

accessible to: Friends Methods of a child class

Page 10: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Base class Virtual functions

Anything that is allowed to be overridden Destructor Tells the compiler to check the virtual

function table

Pure virtual function

Page 11: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Derived class Virtual functions

As someone might want to use your class Initializer lists MyClass : ParentClass(), x(a), y(b)

Page 12: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Advantages We can store an array of Parent class

objects We don’t have to rewrite code

Page 13: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Access levels Public

Inherit all parent’s public attributes and methods that are public AS public

Private Inherit all parent’s public attributes and

methods that are public AS private Protected

Inherit all parent’s public attributes and methods that are public AS protected

Page 14: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Pure virtual methods virtual void show () = 0; No implementation of function in

the .cpp Allows us to create an interface for a

class The interface can not be instantiated

Page 15: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Aircraft is an interface for types of Aircraft

Page 16: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

We still use parent class pointers even if the class is abstract i.e. Aircraft* plane = NULL;

These statements are permitted plane = new Fighter(); plane = new Bomber();

Compiler error plane = new Aircraft();

Page 17: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Polymorphism A base class pointer can reference a derived class

However it can only access members the derived class inherits

Bar is a Foo Foo f has been instantiated

(Bar*) f;dynamic_cast<Bar*>(f);f->NextLine(); //Calls Bar’s function!

Page 18: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Let’s dive into some code Time to see some epic typos!

Page 19: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…
Page 20: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Inheritance architecture Clock is a base object Clock has a Hand class Is Hand also a base object?

Page 21: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…
Page 22: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Plane is a BaseObject

Page 23: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

The issue with inheritance

Page 24: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Component architecture

Page 25: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

BaseObject* clock = new BaseObject();clock->addComponent(BigHand);clock->addComponent(SmallHand);clock->addComponent(Face);clock->addComponent(Engine);

Page 26: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…
Page 27: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…

Component abstract class

Page 28: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…
Page 29: OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a…