C++面向对象程序设计第七章
C++面向对象程序设计
13Object-Oriented Programming: Polymorphism
C++面向对象程序设计
OBJECTIVESIn this chapter you will learn: What polymorphism is, how it makes programming more convenient, and how it makes systems more extensible and maintainable. To declare and use virtual functions to effect polymorphism. The distinction between abstract and concrete classes. To declare pure virtual functions to create abstract classes. How to use run-time type information (RTTI) with downcasting, dynamic_cast, typeid and type_info. How C++ implements virtual functions and dynamic binding "under the hood." How to use virtual destructors to ensure that all appropriate destructors run on an object.
C++面向对象程序设计
13.1 13.2 13.3
Introduction Polymorphism Examples Relationships Among Objects in an Inheritance Hierarchy 13.3.1 13.3.2 13.3.3 13.3.4 13.3.5 Invoking Base-Class Functions from DerivedClass Objects Aiming Derived-Class Pointers at Base-Class Objects Derived-Class Member-Function Calls via BaseClass Pointers Virtual Functions Summary of the Allowed Assignments Between Base-Class and Derived-Class Objects and Pointers
13.4 13.5
Type Fields and switch Statements Abstract Classes and Pure virtual Functions
C++面向对象程序设计
13.6
Case Study: Payroll System Using Polymorphism 13.6.1 13.6.2 13.6.3 13.6.4 13.6.5 13.6.6 Creating Abstract Base Class Employee Creating Concrete Derived Class SalariedEmployee Creating Concrete Derived Class HourlyEmployee Creating Concrete Derived Class CommissionEmployee Creating Indirect Concrete Derived ClassBasePlusCommissionEmployee
Demonstrating Polymorphic Processing
13.7 13.8
(Optional) Polymorphism, Virtual Functions and Dynamic Binding "Under the Hood" Case Study: Payroll System Using Polymorphism and Run-Time Type Information with Downcasting, dynamic_cast, typeid andtype_info
13.9 13.10 13.11
Virtual Destructors (Optional) Software Engineering Case Study: Incorporating Inheritance into the ATM System Wrap-Up
C++面向对象程序设计
Software Engineering Observation 13.1With virtual functions and polymorphism, you can deal in generalities and let the execution-time environment concern itself with the specifics. You can direct a variety of objects to behave in manners appropriate to those objects without even knowing their types (as long as those objects belong to the same inheritance hierarchy and are being accessed off a common base-class pointer).
C++面向对象程序设计
Software Engineering Observation 13.2Polymorphism promotes extensibility: Software written to invoke polymorphic behavior is written independently of the types of the objects to which messages are sent. Thus, new types of objects that can respond to existing messages can be incorporated into such a system without modifying the base system. Only client code that instantiates new objects must be modified to accommodate new types.
C++面向对象程序设计
1 2 3 4 5 6 7 8 9
// Fig. 13.1: CommissionEmployee.h // CommissionEmployee class definition represents a commission employee. #ifndef COMMISSION_H #define COMMISSION_H #include <string> // C++ standard string class using s
td::string; class class CommissionEmployee
Outline
Commission Employee.h
(1 of 2)
10 { 11 public: public: 12 13 14 15 16 17 18 19 20 21 22 23 24 25 void setGrossSales( double ); // set gross sales amount const; double getGrossSales() const; // return gross sales amount void setSocialSecurityNumber( const string & ); // set SSN getSocialSecurityNumber() const; string getSocialSecurityNumber() const; // return SSN void setLastName( const string & ); // set last name const; string getLastName() const; // return last name void setFirstName( const string & ); // set first name getFirstName() const; string getFirstName() const; // return first name CommissionEmployee( const string &, const string &, const string &, 0.0, double = 0.0, double = 0.0 );
C++面向对象程序设计
26 27 28 29 30 31 33 34 35 36 37 39 40 #endif const; double earnings() const; // calculate earnings const; object void print() const; // print CommissionEmployee object string firstName; string lastName; string socialSecurityNumber; double grossSales; // gross weekly sales double commissionRate; // commission percentage void setCommissionRate( double ); // set commission rate const; double getCommissionRate() const; // return commission rate
Outline
32 private: private:
Commission Employee.h
(2 of 2)
38 }; // end class CommissionEmployee
C++面向对象程序设计
1 2 3 4 5 6 7 8
// Fig. 13.2: CommissionEmployee.cpp member// Class CommissionEmployee member-function definitions. #include <iostream> using std::cout; #include "CommissionEmployee.h" // CommissionEmployee class definition
Outline
// constructor 9 CommissionEmployee::CommissionEmployee( CommissionEmployee::CommissionEmployee( 10 const string &first, const string &last, const string &ssn, 11 double sales, double rate ) 12 : firstName( first ), lastName( last ), socialSecurityNumber( ssn ) 13 { 14 setGrossSales( sales ); // validate and store gross sales 15 setCommissionRate( rate ); // validate and store commission rate 16 } // end CommissionEmployee constructor 17 18 19 20 21 22 23 24 25 26 27 28 // set first name void CommissionEmployee::setFirstName( const string &first ) { firstName = first; // should validate } // end function setFirstName // return first name string CommissionEmployee::getFirstName() const { return firstName; } // end function getFirstName
Commission Employee.cpp
(1 of 4)
C++面向对象程序设计
29 30 // set last name 31 void CommissionEmployee::setLastName( const string &last ) 32 { lastNa …… 此处隐藏:5441字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [求职职场]加法运算定律的运用练习题
- [求职职场]大型石油化工工业过程节能新技术
- [求职职场]2015-2020年中国箱纸板行业分析与投资
- [求职职场]NADEX-IWC5A点焊机故障代码
- [求职职场]英语阅读 非常有用
- [求职职场]鲁卫疾控发〔2012〕2号(联合,印发山东
- [求职职场]2014年莆田公务员行测技巧:数字推理的
- [求职职场]基于最近发展区理论的高中数学课堂有效
- [求职职场]与贸易有关的知识产权协议
- [求职职场]【王风范】微演说·职场演说三
- [求职职场]新时代国珍健康大课堂
- [求职职场]群论期末考试复习题
- [求职职场]施工现场消防安全专项施工方案(范本)-
- [求职职场]初中物理光学知识点归纳完美版
- [求职职场]毕业设计总结与体会范文
- [求职职场]江南大学2018年上半年展示设计第1阶段
- [求职职场]景尚乡民兵参战支前保障方案
- [求职职场]【优质】2019年工会职工之家建设工作总
- [求职职场]数据库技术与应用—SQL Server 2008(第
- [求职职场]汽车变速箱构造与工作原理
- 首钢工业区工业遗产资源保护与再利用研
- 第4课 《大学》节选
- 2016程序文件——检验检测结果发布程序
- 2011年高考试题文言文阅读全解释__2011
- 化学是一门基础的自然科学
- 海外做市商制度的借鉴意义
- 外国建筑史复习资料(
- 七年级下思想品德期末综合测试(二)
- 思政课部2013年上学期教学工作总结
- 电大国际公法任务3 0004
- 《圆的认识》教学设计
- 中国轨道交通牵引变流器行业市场发展调
- 中泰证券#定期报告:坚守时代硬科技和
- 浅论企业财务管理与企业经营投资风险的
- 大功率半导体激光器光纤耦合技术调研报
- 中国传统家具的现状与发展探讨
- Broadcom数字电视芯片助海尔扩展高清电
- 新HSK4词汇练习 超全(五)
- 2013届高考数学单元考点复习12
- 雨霖铃精品课件