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

Post on 20-Jan-2018

228 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

OOP, Inheritance and Polymorphism

Lecture 6

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

Composition Clock comprises of

Case Clock face Minute hand Hour hand Works

It is not a clock without these objects!

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

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

Sub classes Do not inherit

Constructors Destructors Copy constructors Friends

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

Base class Virtual functions

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

function table

Pure virtual function

Derived class Virtual functions

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

Advantages We can store an array of Parent class

objects We don’t have to rewrite code

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

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

Aircraft is an interface for types of Aircraft

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();

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!

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

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

Plane is a BaseObject

The issue with inheritance

Component architecture

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

Component abstract class

top related