19
Abstract Classes & Object Slicing

Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Embed Size (px)

DESCRIPTION

Pure Virtual Functions Pure Virtual Function : Child class MUST override this… I'm not even going to describe how it works

Citation preview

Page 1: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Abstract Classes & Object Slicing

Page 2: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Pure Virtual Functions

• Virtual Function : Child class may override, if working with pointer/reference, check to see if we have a better version

Page 3: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Pure Virtual Functions

• Pure Virtual Function : Child class MUST override this… I'm not even going to describe how it works

Page 4: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

New Problem…

• Can't make a GeometricObject:

Page 5: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Abstract

• Abstract Class : Class that is not designed to be instantiated– Defines common capabilities for subclasses

GeometricObject

+getArea() : double+getPerimeter() : double

Italics = Abstract Class

Italics = Abstract Method (Pure Virtual)

Page 6: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

A Promise

• Abstract class is an organizer / promise

Page 7: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Using it

• We have no idea how each object will get its area, but we know it can:

Page 8: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Constructors

• Can't make a Geo object:

• Still need constructors for child classes

Page 9: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Protected Constructors

• Protected constructorsmake sure only usablefrom inside child classes

GeometricObject

+getArea() : double+getPerimeter() : double

# = Protected

Page 10: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Object Slicing

• Assigning subtype to parent type variable slices the object

GeometricObject

Rectangle

r1

Page 11: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Object Slicing

• Assigning subtype to parent type variable slices the object

GeometricObject

Rectangle

r1GeometricObject

r2

Page 12: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Pointers/References

• Pointers / References don't slice– Just store memory address,

don't have to

Page 13: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Casting

• No cast required to store child address in parent ptr:

• Virtual dispatch still findschild version of functions

Page 14: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Down Casting

• Can't auto convert parent pointer back to child one:

Page 15: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

BAD static_cast, BAD

• static_cast happily pretends to turn a pointer into another pointer type…

…weird stuff may happen

Page 16: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Static vs Dynamic

• Static : done at compile timeCan this possibly make sense?

• Dynamic : done at runtimeTry to see if this works…

Page 17: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

Dynamic Cast

• dynamic_cast<Type>( value )– Type must be a pointer or reference– At runtime:• Check to see if conversion makes sense• If so, do it• If not, return nullptr

Page 18: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

• dynamic_cast allows us to check if cast worked:

GOOD dynamic_cast

Page 19: Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see

• dynamic_cast allows us to check if cast worked– Up to you to check!

GOOD dynamic_cast