starting out with c++, 3 rd edition 1 chapter 15 what is inheritance? inheritance allows a new class...

23
1 Starting Out with C++, 3 rd Edition Chapter 15 What is Chapter 15 What is Inheritance? Inheritance? • Inheritance allows a new class to be based on an existing class. • The new class inherits all the member variables and functions of the class it is based on. • Note difference between is-a and has-a. • A station-wagon is-a car, but a car has-a wheel.

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

1

Starting Out with C++, 3rd Edition

Chapter 15 What is Chapter 15 What is Inheritance?Inheritance?

• Inheritance allows a new class to be based on an existing class.

• The new class inherits all the member variables and functions of the class it is based on.

• Note difference between is-a and has-a.

• A station-wagon is-a car, but a car has-a wheel.

Page 2: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

2

Starting Out with C++, 3rd Edition

Inherited ClassInherited Class

Page 3: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

3

Starting Out with C++, 3rd Edition

Inheriting from Base ClassInheriting from Base Class

Page 4: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

4

Starting Out with C++, 3rd Edition

test.htest.h - - Program Program #ifndef TEST_H#define TEST_H#include "grade.h"

class Test : public Grade{ private:

int numQuestions;float pointsEach;int numMissed;

public:Test ( int, int );

}; // Test#endif

Page 5: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

5

Starting Out with C++, 3rd Edition

Why would you want?Why would you want?• In our set example, we could derive sets with

various differences– traits, prints differently– new methods, isKind– keep track of number of traits

• Animals, add/replace features. Logical categorization

• Cars, add common characteristics, options• Allows customization, reuse of base class.• Keeps design consistent. All cars have

dealerRetail. All cars have taxAmount.

Page 6: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

6

Starting Out with C++, 3rd Edition

Protected Members and Protected Members and Class AccessClass Access

• Protected members of a base class are like private members, but they may be accessed by derived classes.

• The base class access specification determines how private, protected, and public base class members may be accessed by derived classes.

Page 7: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

7

Starting Out with C++, 3rd Edition

Base Class AccessBase Class Access

Page 8: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

8

Starting Out with C++, 3rd Edition

Constructors and Constructors and DestructorsDestructors

• The base class’s constructor is called before the derived class’s constructor.

• Since constructors have arguments, we are often not content with an argument-less call (the default).

• The destructors are called in reverse order, with the derived class’s destructor being called first.

Page 9: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

9

Starting Out with C++, 3rd Edition

Passing Arguments to Passing Arguments to Base Class ConstructorsBase Class Constructors

• Assume a class called Cube is derived from a class called Rect. Here is how the constructor looks in Rect:Rect::Rect ( float w, float l ){ width = w; length = l; area = length * width;} // Rect::Rect

Page 10: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

10

Starting Out with C++, 3rd Edition

Cube ConstructorCube Constructor

Cube::Cube( float wide, float long, float high ) : Rect(wide, long)

{ height = high;volume = area * high;

} // Cube::Cube

Page 11: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

11

Starting Out with C++, 3rd Edition

Overriding Base Class Overriding Base Class FunctionsFunctions

• A member function of a derived class may have the same name as a member function of a base class.

Page 12: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

12

Starting Out with C++, 3rd Edition

Polymorphism and Virtual Polymorphism and Virtual Member FunctionsMember Functions

• The term polymorphism means the ability to take many form.

• A virtual member function in a base class expects to be overridden in a derived class

Page 13: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

13

Starting Out with C++, 3rd Edition

Need for Virtual Member Need for Virtual Member FunctionsFunctions

Page 14: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

14

Starting Out with C++, 3rd Edition

Abstract Base Classes and Abstract Base Classes and Pure Virtual FunctionsPure Virtual Functions

• An abstract base class is not instantiated, but other classes are derived from it.

• A pure virtual function is a virtual member function of a base class that must be overridden.

Page 15: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

15

Starting Out with C++, 3rd Edition

Abstract Base Classes and Abstract Base Classes and Pure Virtual Functions Pure Virtual Functions (cont)(cont)

• A class becomes an abstract base class when it contains one or more pure virtual functions.

• A pure virtual function is a virtual member function declared in a manner similar to the following:virtual void showInfo(void) = 0;

Page 16: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

16

Starting Out with C++, 3rd Edition

Base Class PointersBase Class Pointers• Pointers to a base class may be

assigned the address of a derived class object.

• The pointer, however, ignores any overrides the derived class performs

Page 17: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

17

Starting Out with C++, 3rd Edition

Classes Derived from Classes Derived from Derived ClassesDerived Classes

• A base class can also be derived from another class.

• This gives us a “chain” of inheritance where one class is derived from a second, which in turn is derived from a third.

Page 18: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

18

Starting Out with C++, 3rd Edition

Chain of InheritanceChain of Inheritance

Page 19: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

19

Starting Out with C++, 3rd Edition

Multiple InheritanceMultiple Inheritance• Multiple inheritance is when a

derived class has two or more base classes.

Page 20: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

20

Starting Out with C++, 3rd Edition

Multiple Inheritance Multiple Inheritance (cont)(cont)

Page 21: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

21

Starting Out with C++, 3rd Edition

ExamplesExamples• Could have a fruit which falls under two

categories: both fruit and vegetable. A tomato is sometimes referred to as a fruit and sometimes as a vegetable.

• fruit - the ripened reproductive body of a seed plant

• vegetable - edible seeds or roots or stems or leaves or bulbs or tubers or nonsweet fruits of any of numerous herbaceous plant

• Cross-categorization. An apple is a fruit and an apple is a breakfast food.

Page 22: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

22

Starting Out with C++, 3rd Edition

Given the following, organize Given the following, organize them into inheritance or them into inheritance or

compositioncomposition• A test for 1720, a student in 1720• An electronic device, a computer, a

calculating tool• A woman, an employee, a person• A course, a course evaluation• Clothing, scarf, winter clothes, boots• Sesame Street Character, Big Bird,

Oscar the Grouch

Page 23: Starting Out with C++, 3 rd Edition 1 Chapter 15 What is Inheritance? Inheritance allows a new class to be based on an existing class. The new class inherits

23

Starting Out with C++, 3rd Edition

Concerns on multiple Concerns on multiple inheritanceinheritance

• Multiple inheritance is generally thought to be a bad idea because a common base class (B and C inherit from A, and D inherits from both B and C) cause problems with both B and C using the common variables differently.

• Java handles with interface – function use is specified, but no implementation.