《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 14 Polymorphism and Virtual Functions

Chapter 14 ABSOLUTE C++ Inheritance WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 14 Inheritance

Learning Objectives ◆Inheritance Basics Derived classes,with constructors protected:qualifier Redefining member functions Non-inherited functions Programming with Inheritance Assignment operators and copy constructors Destructors in derived classes Multiple inheritance Copyright 2006 Pearson Addison-Wesley.All rights reserved. 14-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-2 Learning Objectives ¨ Inheritance Basics ¨ Derived classes, with constructors ¨ protected: qualifier ¨ Redefining member functions ¨ Non-inherited functions ¨ Programming with Inheritance ¨ Assignment operators and copy constructors ¨ Destructors in derived classes ¨ Multiple inheritance

Introduction to Inheritance Object-oriented programming Powerful programming technique Provides abstraction dimension called inheritance General form of class is defined Specialized versions then inherit properties of general class And add to it/modify it's functionality for it's appropriate use Copyright6 Pearson Addison-Wesley.All rights reserved. 14-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-3 Introduction to Inheritance ¨ Object-oriented programming ¨ Powerful programming technique ¨ Provides abstraction dimension called inheritance ¨ General form of class is defined ¨ Specialized versions then inherit properties of general class ¨ And add to it/modify it’s functionality for it’s appropriate use

Inheritance Basics New class inherited from another class ◆Base class "General"class from which others derive ◆Derived class ◆New class Automatically has base class's: ◆Member variables ◆Member functions Can then add additional member functions and variables Copyright 2006 Pearson Addison-Wesley.All rights reserved. 14-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-4 Inheritance Basics ¨ New class inherited from another class ¨ Base class ¨ "General" class from which others derive ¨ Derived class ¨ New class ¨ Automatically has base class’s: ¨ Member variables ¨ Member functions ¨ Can then add additional member functions and variables

Derived Classes ◆ Consider example: Class of "Employees" ◆Composed of: ◆Salaried employees ◆Hourly employees Each is "subset"of employees Another might be those paid fixed wage each month or week Copyright 2006 Pearson Addison-Wesley.All rights reserved. 14-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-5 Derived Classes ¨ Consider example: Class of "Employees" ¨ Composed of: ¨ Salaried employees ¨ Hourly employees ¨ Each is "subset" of employees ¨ Another might be those paid fixed wage each month or week

Derived Classes Don't "need"type of generic "employee" Since no one's just an "employee" General concept of employee helpful! ◆All have names All have social security numbers Associated functions for these "basics"are same among all employees So "general"class can contain all these "things"about employees Copyright 2006 Pearson Addison-Wesley.All rights reserved. 14-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-6 Derived Classes ¨ Don’t "need" type of generic "employee" ¨ Since no one’s just an "employee" ¨ General concept of employee helpful! ¨ All have names ¨ All have social security numbers ¨ Associated functions for these "basics" are same among all employees ¨ So "general" class can contain all these "things" about employees

Employee Class Many members of "employee"class apply to all types of employees ◆Accessor functions ◆Mutator functions ◆Most data items: ◆SSN ◆Name ◆Pay We won't have "objects"of this class,however Copyright006 Pearson Addison-Wesley.All rights reserved. 147
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-7 Employee Class ¨ Many members of "employee" class apply to all types of employees ¨ Accessor functions ¨ Mutator functions ¨ Most data items: ¨ SSN ¨ Name ¨ Pay ¨ We won’t have "objects" of this class, however

Employee Class Consider printCheck(function: Will always be "redefined"in derived classes So different employee types can have different checks Makes no sense really for "undifferentiated" employee So function printCheck(in Employee class says just that Error message stating "printCheck called for undifferentiated employee!!Aborting." Copyright 2006 Pearson Addison-Wesley.All rights reserved. 14-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-8 Employee Class ¨ Consider printCheck() function: ¨ Will always be "redefined" in derived classes ¨ So different employee types can have different checks ¨ Makes no sense really for "undifferentiated" employee ¨ So function printCheck() in Employee class says just that ¨ Error message stating "printCheck called for undifferentiated employee!! Aborting

Deriving from Employee Class Derived classes from Employee class: Automatically have all member variables Automatically have all member functions Derived class said to "inherit"members from base class Can then redefine existing members and/or add new members Copyright006 Pearson Addison-Wesley.All rights reserved. 14-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-9 Deriving from Employee Class ¨ Derived classes from Employee class: ¨Automatically have all member variables ¨Automatically have all member functions ¨ Derived class said to "inherit" members from base class ¨ Can then redefine existing members and/or add new members

Display 14.3 Interface for the Derived Class HourlyEmployee (1 of 2) Display 14.3 Interface for the Derived Class HourlyEmployee 2 //This is the header file hourlyemployee.h. 3 //This is the interface for the class HourlyEmployee. 4 #ifndef HOURLYEMPLOYEE_H 5 #define HOURLYEMPLOYEE_H 6 #include 7 #include "employee.h" 8 using std:string; 9 namespace SavitchEmployees 10{ Copyright 2006 Pearson Addison-Wesley.All rights reserved. 14-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-10 Display 14.3 Interface for the Derived Class HourlyEmployee (1 of 2)
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 13 Inheritance.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 9 Strings.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 10 Pointers and Dynamic Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 12 Streams and File IO.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 11 Separate Compilation and Namespaces.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 5 Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 8 Operator Overloading, Friends, and References.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 7 Constructors and Other Tools.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 6 Structures and Classes.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 2 Flow of Control.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 3 Function Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 1 C++ Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 4 Parameters and Overloading.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第三章.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第二章 信源与信息熵.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第一章 绪论(主讲:王军选).ppt
- 《信息论与编码》课程教学资源(作业习题)第三章 信道与信道容量(含解答).pdf
- 《信息论与编码》课程教学资源(作业习题)自测题无答案.pdf
- 《信息论与编码》课程教学实验指导书.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding B.pdf
- 《微机技术及应用》课程教学大纲 Microcmputer Technology and aplications.doc
- 《微型计算机技术及应用》课程电子教案(PPT教学课件,共十五章,完整版).pptx
- 《计算机导论》课程教学大纲 Computer Concepts.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-a-Computer History+ Di Devices.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-b-Digital Data Representation.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-a-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-b-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)3-a-b-Computer Software.pdf
- 《计算机导论》课程教学课件(英文讲稿)4- operating system.pdf
- 《计算机导论》课程教学课件(英文讲稿)4-a- File mangement.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-a- LANS_WANS.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-b- LANS_WANS.pdf
- 《计算机导论》课程教学课件(英文讲稿)6-a- The Internet.pdf
- 《计算机导论》课程教学课件(英文讲稿)6-b- The Internet.pdf
- 《计算机导论》课程教学课件(英文讲稿)7-a- Web.pdf
- 《C语言程序设计》课程教学资源(讲义资料)C语言中详解指针.doc
- 《C语言程序设计》课程教学资源(讲义资料)C指针详解(经典详细).pdf
- 《C语言程序设计》课程教学资源(讲义资料)C语言指针用法详解.pdf
- 《C语言程序设计》课程教学课件(PPT讲稿)C语言指针详解.ppt
- 《C语言程序设计》课程教学课件(PPT讲稿)c语言指针完整教程.ppt