C++ 派生类构造函数

来源:互联网 发布:抑郁症是什么感觉 知乎 编辑:程序博客网 时间:2024/06/03 20:04
Because Bulk_itemhas members of built-in type, we should define our own default constructor:
class Bulk_item : public Item_base {
public:
Bulk_item(): min_qty(0), discount(0.0) { }
// as before

}


The constructor initializer also implicitly invokes the Item_base default constructor to initialize its base-class part.


Passing Arguments to a Base-Class Constructor

class Bulk_item : public Item_base {
public:
Bulk_item(const std::string& book, double sales_price, std::size_t qty = 0, double disc_rate = 0.0):
Item_base(book, sales_price),
min_qty(qty), discount(disc_rate) { }
// as before
};


Note:The constructor initializer list supplies initial values for a class' base class and members. It does not specify the order in which those initializations are done. The base class is initialized first and then the members of the derived class are initialized in the order in which they are declared.


Only an Immediate Base Class May Be Initialized
A  class may initialize only its own immediate base class. An immediate base class is the class named in the derivation list. If class C is derived from class B, which is derived from class A, then B is the immediate base of C. Even though every Cobject contains an Apart, the constructors for Cmay not initialize the Apart directly. Instead, class C initializes B, and the constructor for class Bin turn initializes  A. The reason for this restriction is that the author of class Bhas specified how to construct and initialize objects of type B. As with any user of class B ,  the author of class Chas no right to change that specification.


Key Concept: Refactoring
Adding Disc_itemto the Item_basehierarchy is an example of refactoring. Refactoring involves redesigning a class hierarchy to move operations and/or data from one class to another. Refactoring happens most often
when classes are redesigned to add new functionality or handle other changes in that application's requirements. Refactoring is common in OO applications.It is noteworthy that even though we changed the inheritance hierarchy, code that uses the Bulk_item or Item_baseclasses would not need to change. However, when classes are refactored, or changed in any other way, any code that uses those classes must be recompiled.


Key Concept: Respecting the Base-Class Interface
The reason that a constructor can initialize only its immediate base class is that each class defines its own interface. When we define Disc_item, we specify how to initialize a Disc_itemby defining its constructors. Once a class has defined its interface, all interactions with objects of that class should be through that interface, even when those objects are part of a derived object.
For similar reasons, derived-class constructors may not initialize and should not assign to the members of its base class. When those members are publicor protected, a derived constructor could assign values to its base class members inside the constructor body. However, doing so would violate the interface of the base. Derived classes should respect the initialization intent of their base classes by using constructors rather than assigning to these members in the body of the constructor.

0 0
原创粉丝点击