Derived Clases派生类_第1页
Derived Clases派生类_第2页
Derived Clases派生类_第3页
Derived Clases派生类_第4页
Derived Clases派生类_第5页
已阅读5页,还剩40页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、derived classesc+2outline4definition4virtual functions4virtual base classes4abstract classes. pure virtual functions.c+3definitionclass derived : list-of-base-classes / new data member and member functions;4the list of base classes is formed from:public base_classprotected base_classprivate base_cla

2、ssc+4example (base class list) class classname : public c_1, , public c_n/ ;4class classname is derived from: c_1, ., c_n. c+5access controlin the base classbase class access specifierin the derived classpublicpublicpublicprotectedpublicprotectedprivatepublicno accesspublicprotectedprotectedprotecte

3、dprotectedprotectedprivateprotectedno accesspublicprivateprivateprotectedprivateprivateprivateprivateno accessc+6the constructor of a derived class 4derived classes dont inherit constructors and destructors.4the constructor of the derived class:classname(list-of-parameters) : c_1(list1), ., c_n(list

4、_n) / c+7example#include using namespace std;class base public:void f1();void f2();c+8the derived classclass derived : public base public:void f1();4override only the f1 function.4function f2 will be inherited from base.c+9the member functions of the base classvoid base:f1()cout base: f1n;void base:

5、f2()cout base: f2n;f1();c+10member function of the derived classvoid derived:f1()cout derived: f1n;c+11the main functionint main() derived d;d.f2();4the selection of the f1 function has been done in compile time.base: f2base: f1output:c+12virtual functionsclass base public:virtual void f1();void f2(

6、);4if function f1 is declared as virtual, then the selection of the file will be done in running-time.4we have to place only one virtual keyword in front of the declaration of the f1 function, in the base class. 4in this case all inherited f1 functions will be considered virtual.c+13the main functio

7、nint main() derived d;d.f2();base: f2derived: f1output:c+14virtual base classes4in case of multiple inheritance a derived class can inherit multiple issues of a data member.animaldomesticmammaldogc+15the animal class#include #include using namespace std;class animal protected:char name20;public:anim

8、al(char* n);c+16the mammal classclass mammal : public animal protected:int weight;public:mammal(char* n, int w);c+17the domestic classclass domestic : public animal protected:int comportment;public:domestic(char* n, int c);c+18the dog classclass dog : public mammal, public domestic protected:bool ba

9、rk;public:dog(char* n, int w, int c, bool b);void display();c+19constructor of the animal classanimal:animal(char* n)strcpy(name, n);c+20other constructorsmammal:mammal(char* n, int w): animal(n)weight = w;domestic:domestic(char* n, int c): animal(n)comportment = c;c+21constructor of the dog classdo

10、g:dog(char* n, int w, int c, bool b): mammal(n, w), domestic(n, c)bark = b;c+22the display member functionvoid dog:display()cout name (mammal): mammal:name endl;cout name (domestic): domestic:name endl;c+23the display member functioncout weight: weight endl;cout comportment: comportment endl;if ( ba

11、rk ) cout barkingn;else cout no barking;c+24the main functionint main() dog v(hungarian vizsla, 12, 9, true);v.display();4in the display member function we cant access the name data member simply, because this data member was inherited in two different way.c+25outputname (mammal): hungarian vizslana

12、me (domestic): hungarian vizslaweight: 12comportment: 9barking4we can access the name data member in the dog class only by using the scope operator.c+26virtual base class4if we would like to have only one issue of the name data member we have to use virtual base classes.4thus, we have to place the v

13、irtual keyword in the base class list in front of the class (if we intend to make that base class virtual).c+27the mammal classclass mammal : public virtual animal protected:int weight;public:mammal(char* n, int w);c+28the domestic classclass domestic : public virtual animal protected:int comportmen

14、t;public:domestic(char* n, int c);c+29constructor of the dog classdog:dog(char* n, int w, int c, bool b): animal(n), mammal(n, w), domestic(n, c)bark = b;4mammal and domestic doesnt call animal automatically.c+30the display member functionvoid dog:display()cout name (mammal): name endl;cout weight:

15、weight endl;cout comportment: comportment endl;if ( bark ) cout barkingn;else cout no barking;c+31the main functionint main() dog v(hungarian vizsla, 12, 9, true);v.display();4we can access the name data member without using the scope operator.c+32outputname: hungarian vizslaweight: 12comportment: 9

16、barkingc+33abstract classes. pure virtual functions4a base class can have some known features, but we are not able to define them, only in the derived class.4in this case we declare a virtual function, but we dont define it in the base class.4if a virtual member function is declared in the base clas

17、s, but isnt defined, we call it a pure virtual function.c+34declaration of pure virtual functions4pure virtual functions are declared in the regular way, but the declaration ends with =0. this means, that we dont want to define the function right now.4if a class contains at least one pure virtual fu

18、nction, then we name it abstract class.4no instance of an abstract class can be defined.c+35overriding the pure virtual functions4we have to override all pure virtual functions in the derived class.4in other case the derived class will be also abstract.c+36exampleanimaldovebearhorsec+37the animal cl

19、ass#include using namespace std;class animal protected:double weight;double age;double speed;public:c+38the animal class animal( double w, double a, double s); virtual double average_weight() = 0; virtual double average_age() = 0; virtual double average_speed() = 0; int fat() return weight average_w

20、eight(); int fast() return speed average_speed(); int young() return 2 * age average_age(); void display();c+39constructor of the animal classanimal:animal( double w, double a, double s)weight = w;age = a;speed = s;c+40the display member functionvoid animal:display()cout ( fat() ? fat, : thin, );cout ( young() ? young, : old, );cout ( fast() ? fast : slow ) endl;c+41the dove classclass dove : public animal public:dove( double w, double a, double s): animal(w, a, s) double average_wei

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论