CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding...

92
CIS 190: C/C++ Programming Lecture 10 Inheritance 1

Transcript of CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding...

Page 1: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

CIS 190: C/C++ Programming

Lecture 10

Inheritance

1

Page 2: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Outline

• Code Reuse

• Object Relationships

• Inheritance

– What is Inherited

– Handling Access

• Overriding

• Homework and Project

2

Page 3: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Code Reuse

• important to successful coding

• efficient

– no need to reinvent the wheel

• error free (more likely to be)

– code has been previously used/test

3

Page 4: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Code Reuse Examples

• What are some ways we reuse code?

• Any specific examples?

4

Page 5: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

5

Page 6: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Code Reuse Examples

• What are some ways we reuse code?

– functions

– classes

– inheritance – what we’ll be covering today

• Any specific examples?

– calling Insert() and a modified Delete() for Move()

– calling accessor functions inside a constructor

6

Page 7: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Outline

• Code Reuse

• Object Relationships

• Inheritance

– What is Inherited

– Handling Access

• Overriding

• Homework and Project

7

Page 8: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

8

Page 9: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Object Relationships

• two types of object relationships

• is-a

– inheritance

• has-a

– composition

– aggregation

both are forms of association

9

Page 10: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Inheritance Relationship

a Car is-a Vehicle

• this is called inheritance

10

Page 11: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Inheritance Relationship

a Car is-a Vehicle

• 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

11

Page 12: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Inheritance Relationship Code

class Vehicle {

public:

// functions

private:

int m_numAxles;

int m_numWheels;

int m_maxSpeed;

double m_weight;

// etc

} ;

12

Page 13: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Inheritance Relationship Code

class Vehicle {

public:

// functions

private:

int m_numAxles;

int m_numWheels;

int m_maxSpeed;

double m_weight;

// etc

} ;

13

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

Page 14: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Inheritance Relationship Code

class Car {

} ;

14

Page 15: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Inheritance Relationship Code

class Car: public Vehicle {

} ;

15

Car inherits from the Vehicle class

Page 16: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Inheritance Relationship Code

class Car: public Vehicle {

} ;

16

Car inherits from the Vehicle class

don’t forget the colon here!

Page 17: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

Page 18: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Inheritance Relationship Code

class Car:

public Vehicle { /*etc*/ };

18

Page 19: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Inheritance Relationship Code

class Car:

public Vehicle { /*etc*/ };

class Plane:

public Vehicle { /*etc*/ };

class SpaceShuttle:

public Vehicle { /*etc*/ };

class BigRig:

public Vehicle { /*etc*/ };

19

Page 20: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Composition Relationship

a Car has-a Chassis

• this is called composition

20

Page 21: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Composition Relationship

a Car has-a Chassis

• 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

21

Page 22: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Composition Relationship Code

class Chassis {

public:

//functions

private:

string m_material;

double m_weight;

double m_maxLoad;

// etc

} ;

22

Page 23: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Composition Relationship Code

class Chassis {

public:

//functions

private:

string m_material;

double m_weight;

double m_maxLoad;

// etc

} ;

23

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

Page 24: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Composition Relationship Code

class Chassis {

public:

//functions

private:

string m_material;

double m_weight;

double m_maxLoad;

// etc

} ;

24

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

Page 25: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Composition Relationship Code

class Car: public Vehicle {

public:

//functions

private:

// member variables, etc.

} ;

25

Page 26: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Composition Relationship Code

class Car: public Vehicle {

public:

//functions

private:

// member variables, etc.

// has-a (composition)

Chassis m_chassis;

} ;

26

Page 27: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Aggregation Relationship

a Car has-a Driver

• this is called aggregation

27

Page 28: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Aggregation Relationship

a Car has-a Driver

• 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 object via a pointer to a Driver object

28

Page 29: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Aggregation Relationship Code

class Driver: public Person {

public:

// functions

private:

Date m_licenseExpire;

string m_licenseType;

// etc

} ;

29

Page 30: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Aggregation Relationship Code

class Driver: public Person {

public:

// functions

private:

Date m_licenseExpire;

string m_licenseType;

// etc

} ;

30

Driver itself is a child class of Person

Page 31: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Aggregation Relationship Code

class Driver: public Person {

public:

// functions

private:

Date m_licenseExpire;

string m_licenseType;

// etc

} ;

31

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

Page 32: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Aggregation Relationship Code

class Car: public Vehicle {

public:

//functions

private:

// member variables, etc.

} ;

32

Page 33: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Aggregation Relationship Code

class Car: public Vehicle {

public:

//functions

private:

// member variables, etc.

// has-a (aggregation)

Person *m_driver;

} ;

33

Page 34: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

34

• Engine • Driver • Person • Owner • Chassis

• Car • Vehicle • BigRig • Rectangle • SpaceShuttle

Page 35: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Outline

• Code Reuse

• Object Relationships

• Inheritance

– What is Inherited

– Handling Access

• Overriding

• Homework and Project

35

Page 36: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

36

Page 37: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Hierarchy Example

Vehicle

37

Page 38: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Hierarchy Example

Vehicle

etc. Car Plane BigRig

38

Page 39: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Hierarchy Example

Vehicle

SUV

etc.

Sedan

Car Plane BigRig

Jeep Van

39

Page 40: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Hierarchy Example

Vehicle

SUV

etc.

Sedan

Car Plane BigRig

Jeep Van

Spe

cial

izat

ion

40

Page 41: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

41

Page 42: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

42

Page 43: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Hierarchy Details

• child classes can use, extend, or replace the parent class behaviors

43

Page 44: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

44

Page 45: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

45

Page 46: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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)

46

Page 47: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Outline

• Code Reuse

• Object Relationships

• Inheritance

– What is Inherited

– Handling Access

• Overriding

• Homework and Project

47

Page 48: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited

Vehicle Class

48

Page 49: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited

Vehicle Class

• public fxns&vars

49

Page 50: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited

Vehicle Class

• public fxns&vars • protected fxns&vars

50

Page 51: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited

Vehicle Class

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

• private functions

51

Page 52: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited

Vehicle Class

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

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

52

Page 53: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited Car Class

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

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

Vehicle Class

53

Page 54: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited Car Class

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

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

Vehicle Class

54

• child class members (functions

& variables)

Page 55: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited Car Class

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

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

Vehicle Class

55

• child class members (functions

& variables) ?

Page 56: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited Car Class

• public fxns&vars

Vehicle Class

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

56

• protected fxns&vars • private variables

• child class members (functions

& variables)

Page 57: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

57

• private variables

Page 58: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited Car Class

• public fxns&vars

• protected fxns&vars

• private

variables

Vehicle Class

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

58

• child class members (functions

& variables)

Page 59: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

What is Inherited Car Class

• public fxns&vars

• protected fxns&vars

• private

variables

Vehicle Class

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

59

• child class members (functions

& variables)

Page 60: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

60

• child class members (functions

& variables)

Page 61: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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

61

• child class members (functions

& variables)

Page 62: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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 62

• child class members (functions

& variables)

Page 63: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Outline

• Code Reuse

• Object Relationships

• Inheritance

– What is Inherited

– Handling Access

• Overriding

• Homework and Project

63

Page 64: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Handling Access

• child class has access to parent class’s:

– public member variables/functions

– protected member variables/functions

64

Page 65: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Handling Access

• child class has access to parent class’s:

– public member variables/functions

– protected member variables/functions

– but not private member variables/functions

65

Page 66: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

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?

66

Page 67: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Handling Access

• we should not make these variables protected!

• leave them private!

67

Page 68: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Handling Access

• we should not make these variables protected!

• leave them private!

• instead, child class uses protected functions when interacting with parent variables

– mutators

– accessors

68

Page 69: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Livecoding

• let’s look more closely at inheritance

• with these classes:

– Shape

• Rectangle

• Pentagon

• Circle

69 LIVECODING LIVECODING

Page 70: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Outline

• Code Reuse

• Object Relationships

• Inheritance

– What is Inherited

– Handling Access

• Overriding

• Homework and Project

70

Page 71: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Specialization

• child classes are meant to be more specialized than parent classes

– adding new member functions

– adding new member variables

• child classes can also specialize by overriding parent class member functions

– child class uses exact same function signature

71

Page 72: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Overloading vs Overriding

• overloading

– ???

72

Page 73: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Overloading vs Overriding

• overloading

– use the same function name, but with different parameters for each overloaded implementation

73

Page 74: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Overloading vs Overriding

• overloading

– use the same function name, but with different parameters for each overloaded implementation

• overriding

– ???

74

Page 75: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Overloading vs Overriding

• overloading

– use the same function name, but with different parameters for each overloaded implementation

• overriding

– use the same function name and parameters, but with a different implementation

– child class method “hides” parent class method

– only possible by using inheritance

75

Page 76: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Overriding Examples

• for these examples, the Vehicle class now contains these public functions: void Upgrade();

void PrintSpecs();

void Move(double distance);

76

Page 77: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Overriding Examples

• for these examples, the Vehicle class now contains these public functions: void Upgrade();

void PrintSpecs();

void Move(double distance);

• Car class inherits all of these public functions

– it can therefore override them

77

Page 78: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Basic Overriding Example

• Car class overrides Upgrade() void Car::Upgrade()

{

// entirely new Car-only code

}

• when Upgrade() is called on a object of type Car, what happens?

78

Page 79: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Basic Overriding Example

• Car class overrides Upgrade() void Car::Upgrade()

{

// entirely new Car-only code

}

• when Upgrade() is called on a object of type Car, the Car::Upgrade() function is invoked

79

Page 80: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Overriding (and Calling) Example

• Car class overrides and calls PrintSpecs() void Car::PrintSpecs()

{

Vehicle::PrintSpecs();

// additional Car-only code

}

• can explicitly call a parent’s original function by using the scope resolution operator

80

Page 81: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Attempted Overloading Example

• Car class attempts to overload the function Move(double distance) with new parameters void Car::Move(double distance,

double avgSpeed)

{

// new overloaded Car-only code

}

81

Page 82: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Attempted Overloading Example

• Car class attempts to overload the function Move(double distance) with new parameters void Car::Move(double distance,

double avgSpeed)

{

// new overloaded Car-only code

}

• but this does something we weren’t expecting! 82

Page 83: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Precedence

• overriding takes precedence over overloading

– instead of overloading the Move() function, the compiler assumes we are trying to override it

83

Page 84: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Precedence

• overriding takes precedence over overloading

– instead of overloading the Move() function, the compiler assumes we are trying to override it

• declaring Car::Move(2 parameters)

84

Page 85: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Precedence

• overriding takes precedence over overloading

– instead of overloading the Move() function, the compiler assumes we are trying to override it

• declaring Car::Move(2 parameters)

• overrides Vehicle::Move(1 parameter)

85

Page 86: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Precedence

• overriding takes precedence over overloading

– instead of overloading the Move() function, the compiler assumes we are trying to override it

• declaring Car::Move(2 parameters)

• overrides Vehicle::Move(1 parameter)

• we no longer have access to the original Move() function from the Vehicle class

86

Page 87: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Overloading in Child Class

• to overload, we must have both original and overloaded functions in child class void Car::Move(double distance);

void Car::Move(double distance,

double avgSpeed);

• the “original” one parameter function can then explicitly call parent function

87

Page 88: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Livecoding

• let’s change the PrintX() functions to be an overridden member function

• we’ll have one of each “type”:

– basic (complete override)

– override and call

– override and overload

88 LIVECODING LIVECODING

Page 89: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Outline

• Code Reuse

• Object Relationships

• Inheritance

– What is Inherited

– Handling Access

• Overriding

• Homework and Project

89

Page 90: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Homework 6

• check validity of input values

• acceptable does not mean guaranteed!

• be extra careful with following the coding standards, and making appropriate decisions

– explain in your README.txt

• any questions?

90

Page 91: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Project

• groups are due today on Piazza

– if you haven’t posted your group by 9 PM tonight, I will assign them for you!

• proposal due next week in class

• alphas are due November 23rd

• IMPORTANT!!!: we will be grading the most recent submission from your group

91

Page 92: CIS 190: C/C++ Programmingcis190/fall2014/lectures/10/lec10.pdf · Overloading vs Overriding •overloading –use the same function name, but with different parameters for each overloaded

Project

• proposal due next week in class

• alphas due 1 ½ weeks after proposal

• please don’t turn in anything late!

• will grade last submission from group members for alpha and project

92