C++ 派生类Derived Classes

To define a derived class, we use a class derivation list  to specify the base class(es). A class derivation list names one or more base classes and has the form

class classname: access-label base-class


Ordinarily, derived classes redefine the virtual functions that they inherit, although they are not requried
to do so. If a derived class does not redefine a virtual, then the version it uses is the one defined in its
base class.


With one exception, the declaration (Section 7.4, p. 251)of a virtual function in the derived class must exactly match the way the function is defined in the base. That exception applies to virtuals that return a  reference (or pointer) to a type that is itself a base class. A virtual function in a derived class can return a reference (or pointer) to a class that is publicly derived from the type returned by the baseclass function.


Note:Once a function is declared as virtual in a base class it remains virtual; nothing the derived classes do can change the fact that the function is virtual. When a derived class redefines a virtual, it may use the virtual keyword, but it is not required to do so.


A  derived object consists of multiple parts: the (nonstatic)members defined in the derived class itself
plus the subobjects made up of the (nonstatic)members of its base class. 


A  class must be defined before it can be used as a base class. Had we declared, but not defined, Item_base, we could not use it as our base class:

class Item_base; // declared but not defined
// error: Item_base must be defined
class Bulk_item : public Item_base { ... };


If we need to declare (but not yet define) a derived class, the declaration contains the class name but does not include its derivation list. For example, the following forward declaration of Bulk_itemresults in a  compile-time error:
// error: a forward declaration must not include the derivation list
class Bulk_item : public Item_base;


The correct forward declarations are:
// forward declarations of both derived and nonderived class
class Bulk_item;
class Item_base;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值