Download presentation
Presentation is loading. Please wait.
Published byZoe Payne Modified over 8 years ago
1
OOP, Inheritance and Polymorphism Lecture 6
2
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
3
Composition Clock comprises of Case Clock face Minute hand Hour hand Works It is not a clock without these objects!
6
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
7
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
8
Sub classes Do not inherit Constructors Destructors Copy constructors Friends
9
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
10
Base class Virtual functions Anything that is allowed to be overridden Destructor Tells the compiler to check the virtual function table Pure virtual function
11
Derived class Virtual functions As someone might want to use your class Initializer lists MyClass : ParentClass(), x(a), y(b)
12
Advantages We can store an array of Parent class objects We don’t have to rewrite code
13
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
14
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
15
Aircraft is an interface for types of Aircraft
16
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();
17
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 (f); f->NextLine(); //Calls Bar’s function!
18
Let’s dive into some code Time to see some epic typos!
20
Inheritance architecture Clock is a base object Clock has a Hand class Is Hand also a base object?
22
Plane is a BaseObject
23
The issue with inheritance
24
Component architecture
25
BaseObject* clock = new BaseObject(); clock->addComponent(BigHand); clock->addComponent(SmallHand); clock->addComponent(Face); clock->addComponent(Engine);
27
Component abstract class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.