1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Programming in C++ - Chapter 15: Inheritance, virtual functions, and polymorphism

21 66 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 21
Dung lượng 500,65 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Lecture Programming in C++ - Chapter 15: Inheritance, virtual functions, and polymorphism. In this chapter, you will learn to: Create an inheritance hierarchy, use inheritance as a form of class interaction, make a virtual class, use polymorphism in an engineering program.

Trang 1

Chapter 15 – Inheritance, Virtual  Functions, and Polymorphism

Trang 2

Name of another way in which classes and objects relate

– Base class

– Derived class

When object of derived class declared

– Object of base class is automatically created  and contained within derived class object

Lesson 15.1

Trang 6

Derived class members convey no special access privileges to base class functions

Lesson 15.1

Trang 7

Base and Derived Objects

Derived object Base object

private data protected data public functions

private data

public functions

derived_object.derived_function( )

derived_object.base_function( )

Trang 8

Use only derived object name to access both derived and base class functions

Derived class takes precedence

– over­riding base class function

Lesson 15.1

Trang 9

Component class object

Composite Class

Object

Derived class        object       Base class object

Lesson 15.1

On all examples, upper boxes represent private data members and lower boxes represent public member functions.

Trang 10

class Derived : private Base

– public and protected members of Base become  private members of Derived

– private members of Base remain private to Base

class Derived : protected Base

– public and protected members of Base become  public and protected member of Derived

– private members of Base remain private to Base

Lesson 15.1

Trang 11

Order of Constructor and  Destructor Function Calls

Both base and derived constructors called when derived class object instantiated

Bass class constructor called first, derived constructor second

Derived class destructor called first, base class destructor second

Order in which objects declared determines order of constructor calls

Lesson 15.2

Trang 12

Well suited to extending or customizing 

existing code

Can make full use of base class features and add specific features

Can modify base classes without requiring derived classes to be modified

– Allows base classes to remain up to date 

Lesson 15.1

Trang 13

Constructor

Trang 14

Object for lowest level class contains sub­objects of all other classes

Order of calling constructors is from top down in inheritance graph

Pass data one step or level at a time

Functions can be overridden in any class down inheritance graph

Can assign derived class objects to base class objects, but not reverse

Lesson 15.3

Trang 15

Object for lowest level class contains sub­objects of all other classes

Lesson 15.3 Earth object

Planet object (no name)

Celestial_body

object (no name)

Trang 16

Class can inherit from more than one class directly

– When one class has "is a" relationship with  more than one class

Lesson 15.4

Trang 17

virtual  type function (type, type) = 0;

Trang 18

Can declare base class type pointer variable and assign address of derived class object

Any address of object down inheritance 

graph

Lesson 15.5

Trang 19

Association of function call with a functionEarly binding

Trang 21

Learned how to:

Ngày đăng: 30/01/2020, 01:59

TỪ KHÓA LIÊN QUAN