教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 求职职场 >

C++面向对象程序设计第七章

来源:网络收集 时间:2025-04-30
导读: 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 main

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字,全部文档内容请下载后查看。喜欢就下载吧 ……

C++面向对象程序设计第七章.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/118571.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)