Derived Clsses派生类.ppt

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

C++ Derived Classes Outline Definition Virtual functions Virtual base classes Abstract classes. Pure virtual functions. Definition class Derived : list-of-base-classes { // new data member and member functions }; The list of base classes is formed from: public base_class protected base_class private base_class Example (base class list) class ClassName : public C_1, …, public C_n { // … }; Class ClassName is derived from: C_1, ..., C_n. Access Control The Constructor of a Derived Class Derived classes don’t inherit constructors and destructors. The constructor of the derived class: ClassName(list-of-parameters) : C_1(list1), ..., C_n(list_n) { // … } Example #include iostream using namespace std; class Base { public: void f1(); void f2(); }; The Derived Class class Derived : public Base { public: void f1(); }; Override only the f1 function. Function f2 will be inherited from Base. The Member Functions of the Base Class void Base::f1() { cout Base: f1\n; } void Base::f2() { cout Base: f2\n; f1(); } Member Function of the Derived Class void Derived::f1() { cout Derived: f1\n; } The main function int main() { Derived d; d.f2(); } The selection of the f1 function has been done in compile time. Virtual functions class Base { public: virtual void f1(); void f2(); }; If function f1 is declared as virtual, then the selection of the file will be done in running-time. We have to place only one virtual keyword in front of the declaration of the f1 function, in the base class. In this case all inherited f1 functions will be considered virtual. The main Function int main() { Derived d; d.f2(); } Virtual Base Classes In case of multiple inheritance a derived class can inherit multiple issues of a data member. The Animal Class #include iostream #include cstring using namespace std; class Animal { protected: char name[20]; public: Animal(char* n); }; The Mammal Class class Mammal : public Animal { protected: int weight; public: Mamma

文档评论(0)

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

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

1亿VIP精品文档

相关文档