Computer Science II for Majors Lecture 10 and 11 Inheritance Dr. - - PowerPoint PPT Presentation

computer science ii for majors
SMART_READER_LITE
LIVE PREVIEW

Computer Science II for Majors Lecture 10 and 11 Inheritance Dr. - - PowerPoint PPT Presentation

CMSC202 Computer Science II for Majors Lecture 10 and 11 Inheritance Dr. Katherine Gibson www.umbc.edu Last Class We Covered Professor Chang substitute taught Allocation methods Static, automatic, dynamic new and delete


slide-1
SLIDE 1

www.umbc.edu

CMSC202 Computer Science II for Majors

Lecture 10 and 11 –

Inheritance

  • Dr. Katherine Gibson
slide-2
SLIDE 2

www.umbc.edu

Last Class We Covered

  • Professor Chang substitute taught
  • Allocation methods

– Static, automatic, dynamic – new and delete

  • Dynamically allocating arrays

– Constructors and destructors

2

slide-3
SLIDE 3

www.umbc.edu

Any Questions from Last Time?

slide-4
SLIDE 4

www.umbc.edu

Today’s Objectives

  • To review the exam results
  • To understand the relationships between objects
  • To begin learning about inheritance

–To cover what is being inherited –To understand how inheritance and access to member variables interact

4

slide-5
SLIDE 5

www.umbc.edu

Exam 1 Results

slide-6
SLIDE 6

www.umbc.edu

Code Reuse

slide-7
SLIDE 7

www.umbc.edu

Code Reuse

  • Important to successful coding
  • Efficient

– No need to reinvent the wheel

  • Error free

– Code has been previously used/tested – (Not guaranteed, but more likely)

7

slide-8
SLIDE 8

www.umbc.edu

Code Reuse Examples

  • What are some ways we reuse code?

– functions – classes

  • Any specific examples?

– calling Insert() and a modified Delete() for Move() – calling accessor functions inside a constructor

8

slide-9
SLIDE 9

www.umbc.edu

Code Reuse Examples

  • What are some ways we reuse code?

–Functions –Classes –Inheritance – what we’ll be covering today

  • Any specific examples?

9

slide-10
SLIDE 10

www.umbc.edu

Object Relationships

slide-11
SLIDE 11

www.umbc.edu

Refresher on Objects

  • Objects are what we call an instance of a class
  • For example:

– Rectangle is a class – r1 is a variable of type Rectangle – r1 is a Rectangle object

11

slide-12
SLIDE 12

www.umbc.edu

Object Relationships

  • There are two types of object relationships
  • is-a

– inheritance

  • has-a

– composition – aggregation both are forms

  • f association

12

slide-13
SLIDE 13

www.umbc.edu

Inheritance Relationship

A Car is-a Vehicle

  • This is called inheritance
  • The Car class inherits from the Vehicle class
  • Vehicle is the general class, or the parent class
  • Car is the specialized class, or child class, that

inherits from Vehicle

13

slide-14
SLIDE 14

www.umbc.edu

Inheritance Relationship Code

class Vehicle { public: // functions private: int m_numAxles; int m_numWheels; int m_maxSpeed; double m_weight; // etc } ;

14

all Vehicles have axles, wheels, a max speed, and a weight

slide-15
SLIDE 15

www.umbc.edu

Inheritance Relationship Code

class Car { } ;

15

slide-16
SLIDE 16

www.umbc.edu

Inheritance Relationship Code

class Car: public Vehicle { } ;

16

don’t forget the colon here! Car inherits from the Vehicle class

slide-17
SLIDE 17

www.umbc.edu

Inheritance Relationship Code

class Car: public Vehicle { public: // functions private: int m_numSeats; double m_MPG; string m_color; string m_fuelType; // etc } ;

17

all Cars have a number of seats, a MPG value, a color, and a fuel type

slide-18
SLIDE 18

www.umbc.edu

Inheritance Relationship Code

class Car: public Vehicle { /*etc*/ }; class Plane: public Vehicle { /*etc*/ }; class SpaceShuttle: public Vehicle { /*etc*/ }; class BigRig: public Vehicle { /*etc*/ };

18

slide-19
SLIDE 19

www.umbc.edu

Composition Relationship

A Car has-a Chassis

  • This is called composition
  • The Car class contains an object of type Chassis
  • A Chassis object is part of the Car class
  • A Chassis cannot “live” out of context of a Car

– If the Car is destroyed, the Chassis is also destroyed

19

slide-20
SLIDE 20

www.umbc.edu

Composition Relationship Code

class Chassis { public: // functions private: string m_material; double m_weight; double m_maxLoad; // etc } ;

20

all Chassis have a material, a weight, and a maxLoad they can hold

slide-21
SLIDE 21

www.umbc.edu

Composition Relationship Code

class Chassis { public: // functions private: string m_material; double m_weight; double m_maxLoad; // etc } ;

21

also, notice that there is no inheritance for the Chassis class

slide-22
SLIDE 22

www.umbc.edu

Composition Relationship Code

class Car: public Vehicle { public: // functions private: // member variables, etc. // has-a (composition) Chassis m_chassis; } ;

22

slide-23
SLIDE 23

www.umbc.edu

Aggregation Relationship

a Car has-a Driver

  • this is called aggregation

23

slide-24
SLIDE 24

www.umbc.edu

Aggregation Relationship

A Car has-a Driver

  • This is called aggregation
  • The Car class is linked to an object of type Driver
  • Driver class is not directly related to the Car class
  • A Driver can live out of context of a Car
  • A Driver must be “contained” in the Car
  • bject via a pointer to a Driver object

24

slide-25
SLIDE 25

www.umbc.edu

Aggregation Relationship Code

class Driver: public Person { public: // functions private: Date m_licenseExpire; string m_licenseType; // etc } ;

25

Driver itself is a child class of Person

slide-26
SLIDE 26

www.umbc.edu

Aggregation Relationship Code

class Driver: public Person { public: // functions private: Date m_licenseExpire; string m_licenseType; // etc } ;

26

Driver inherits all of Person’s member variables (Date m_age, string m_name, etc.) so they aren’t included in the Driver child class

Driver itself is a child class of Person

slide-27
SLIDE 27

www.umbc.edu

Aggregation Relationship Code

class Car: public Vehicle { public: // functions private: // member variables, etc. // has-a (aggregation) Person *m_driver; } ;

27

slide-28
SLIDE 28

www.umbc.edu

Visualizing Object Relationships

  • On paper, draw a representation of how the

following objects relate to each other

  • Make sure the type of relationship is clear

28

  • Engine
  • Driver
  • Person
  • Owner
  • Chassis
  • Car
  • Vehicle
  • BigRig
  • Rectangle
  • SpaceShuttle
slide-29
SLIDE 29

www.umbc.edu

Inheritance

slide-30
SLIDE 30

www.umbc.edu

Inheritance Access Specifiers

  • inheritance can be done via public, private, or

protected

  • we’re going to focus exclusively on public
  • you can also have multiple inheritance

– where a child class has more than one parent

  • we won’t be covering this

30

slide-31
SLIDE 31

www.umbc.edu

Hierarchy Example

Vehicle

31

slide-32
SLIDE 32

www.umbc.edu

Hierarchy Example

Vehicle etc. Car Plane BigRig

32

slide-33
SLIDE 33

www.umbc.edu

Hierarchy Example

Vehicle SUV etc. Sedan Car Plane BigRig Jeep Van

33

slide-34
SLIDE 34

www.umbc.edu

Hierarchy Example

Vehicle SUV etc. Sedan Car Plane BigRig Jeep Van

Specialization

34

slide-35
SLIDE 35

www.umbc.edu

Hierarchy Vocabulary

  • more general class (e.g., Vehicle) can be called:

– parent class – base class – superclass

  • more specialized class (e.g., Car) can be called:

– child class – derived class – subclass

35

slide-36
SLIDE 36

www.umbc.edu

Hierarchy Details

  • parent class contains all that is common among

its child classes (less specialized)

– Vehicle has a maximum speed, a weight, etc. because all vehicles have these

  • member variables and functions of the parent

class are inherited by all of its child classes

36

slide-37
SLIDE 37

www.umbc.edu

Hierarchy Details

  • child classes can use, extend, or replace the

parent class behaviors

37

slide-38
SLIDE 38

www.umbc.edu

Hierarchy Details

  • child classes can use, extend, or replace the

parent class behaviors

  • use

– the child class takes advantage of the parent class behaviors exactly as they are

  • like the mutators and accessors from the parent class

38

slide-39
SLIDE 39

www.umbc.edu

Hierarchy Details

  • child classes can use, extend, or replace the

parent class behaviors

  • extend

– the child class creates entirely new behaviors

  • a RepaintCar() function for the Car child class
  • mutators/accessors for new member variables

39

slide-40
SLIDE 40

www.umbc.edu

Hierarchy Details

  • child classes can use, extend, or replace the

parent class behaviors

  • replace

– child class overrides parent class’s behaviors

  • (we’ll cover this later today)

40

slide-41
SLIDE 41

www.umbc.edu

Outline

  • Code Reuse
  • Object Relationships
  • Inheritance

– What is Inherited – Handling Access

  • Overriding
  • Homework and Project

41

slide-42
SLIDE 42

www.umbc.edu

What is Inherited

Vehicle Class

42

slide-43
SLIDE 43

www.umbc.edu

What is Inherited

Vehicle Class

  • public fxns&vars

43

slide-44
SLIDE 44

www.umbc.edu

What is Inherited

Vehicle Class

  • public fxns&vars
  • protected fxns&vars

44

slide-45
SLIDE 45

www.umbc.edu

What is Inherited

Vehicle Class

  • public fxns&vars
  • protected fxns&vars
  • private variables
  • private functions

45

slide-46
SLIDE 46

www.umbc.edu

What is Inherited

Vehicle Class

  • public fxns&vars
  • protected fxns&vars
  • private variables
  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

46

slide-47
SLIDE 47

www.umbc.edu

What is Inherited

Car Class

  • public fxns&vars
  • protected fxns&vars
  • private variables
  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

Vehicle Class

47

slide-48
SLIDE 48

www.umbc.edu

What is Inherited

Car Class

  • public fxns&vars
  • protected fxns&vars
  • private variables
  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

Vehicle Class

48

  • child class

members (functions & variables)

slide-49
SLIDE 49

www.umbc.edu

What is Inherited

Car Class

  • public fxns&vars
  • protected fxns&vars
  • private variables
  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

Vehicle Class

49

  • child class

members (functions & variables) ?

slide-50
SLIDE 50

www.umbc.edu

What is Inherited

Car Class

  • public

fxns&vars

Vehicle Class

  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

50

  • protected fxns&vars
  • private variables
  • child class

members (functions & variables)

slide-51
SLIDE 51

www.umbc.edu

What is Inherited

Car Class

  • child class

members (functions & variables)

  • public

fxns&vars

  • protected

fxns&vars

Vehicle Class

  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

51

  • private variables
slide-52
SLIDE 52

www.umbc.edu

What is Inherited

Car Class

  • public

fxns&vars

  • protected

fxns&vars

  • private

variables

Vehicle Class

  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

52

  • child class

members (functions & variables)

slide-53
SLIDE 53

www.umbc.edu

What is Inherited

Car Class

  • public

fxns&vars

  • protected

fxns&vars

  • private

variables

Vehicle Class

  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

53

  • child class

members (functions & variables)

slide-54
SLIDE 54

www.umbc.edu

What is Inherited

Car Class

  • public

fxns&vars

  • protected

fxns&vars

  • private

variables

Vehicle Class

not (directly) accessible by Car objects

  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

54

  • child class

members (functions & variables)

slide-55
SLIDE 55

www.umbc.edu

What is Inherited

Car Class

  • public

fxns&vars

  • protected

fxns&vars

  • private

variables

Vehicle Class

not (directly) accessible by Car objects

  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

55

  • child class

members (functions & variables)

slide-56
SLIDE 56

www.umbc.edu

What is Inherited

Car Class

  • public

fxns&vars

  • protected

fxns&vars

  • private

variables

Vehicle Class

not (directly) accessible by Car objects

  • private functions
  • copy constructor
  • assignment operator
  • constructor
  • destructor

can access and invoke, but are not directly inherited

56

  • child class

members (functions & variables)

slide-57
SLIDE 57

www.umbc.edu

Outline

  • Code Reuse
  • Object Relationships
  • Inheritance

– What is Inherited – Handling Access

  • Overriding
  • Homework and Project

57

slide-58
SLIDE 58

www.umbc.edu

Handling Access

  • Child class has access to parent class’s:

– public member variables/functions – protected member variables/functions – but not private member variables/functions

  • How should we set the access modifier for

parent member variables we want the child class to be able to access?

58

slide-59
SLIDE 59

www.umbc.edu

Handling Access

  • Do not make these variables protected!

– Leave them private!

  • Instead, child class uses public or protected

functions when interacting with parent variables

– Reason we implement accessors and mutators

59

slide-60
SLIDE 60

www.umbc.edu

Announcements

  • Project 2 is out – you should have started!

–It is due Thursday, March 10th

  • Nothing over Spring Break

–Enjoy your temporary freedom!

60