9. OOP amp; ADTs Introduction to Inheritance.ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
9.OOPamp;amp;ADTsIntroductiontoInheritance.ppt

9. OOP ADTs: Introduction to Inheritance Constructors and Inheritance Consider Employees constructor // Explicit-Value Constructor inline Employee::Employee long id, string last, string first, char initial, int dept myIdNum id; myLastName last; myFirstName first; myMiddleInitial initial; myDeptCode dept; A derived class can use a member-initialization-list to call the base-class constructor to initialize the inherited data members — easier than writing it from scratch. // Definition of SalariedEmployee explicit-value constructor inline SalariedEmployee::SalariedEmployee long id, string last, string first, char initial, int dept, double sal From the derived class constructor, pass the id number, last name, first name, middle initial and dept. code to the Employee constructor to initialize those members. mySalary sal; SalariedEmployee then initializes only its own local member s . : Employee id, last, first, initial, dept * Read Chap. 12 A. Inheritance, OOD, and OOP §12.1 12.2 A major objective of OOP: writing reusable code to avoid re-inventing the wheel . Ways to do this in C++: Encapsulate code within functions Build classes Store classes and functions in separately-compiled libraries Convert functions into type-parameterized function templates Convert classes into type-parameterized class templates An additional approach that distinguishes OOP from the others: Inheritance: Define one class derived class in terms of another base class, reusing the data members and function members of the base class. Example: Suppose a problem requires stack operations not provided in our stack class — e.g., max , min Ways to Approach this: #1: Add function members to the Stack class that implement the new operations. Bad: This can easily mess up a tested, operational class, creating problems for other client programs. new operations push , pop , ... myTop, ... new data members Stack class #2: An adapter approach:

文档评论(0)

gshbzl + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档