C++ 派生类构造函数
来源:互联网 发布:抑郁症是什么感觉 知乎 编辑:程序博客网 时间:2024/06/03 20:04
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.
- 【C++】派生类构造函数
- 派生类 构造函数
- [c++]派生类的构造函数和析构函数
- c++---派生类的构造函数和析构函数
- c++,派生类无法调用基类构造函数
- C#==>派生类的构造函数
- C++——派生类的构造函数实例
- 【C++】探究类派生时构造函数的顺序
- C ++ 派生类构造函数执行的次序
- 派生类的构造函数
- 派生类构造函数规则
- 派生类的构造函数
- 派生类的构造函数
- 派生类的构造函数
- 派生类的构造函数
- 派生类的构造函数
- 派生类构造函数
- 派生类的构造函数
- boost正则表达式( 字母, 数字, 下划线,中文)
- java线程和其工作原理
- hdu1073 Online Judge 一道字符串处理的水题,,wa了我一下午。。。这几天发生了点不寻常的事。
- 单链表的常用操作(二)
- 用QT的model/view/delegate接口实现自定义的窗口渲染
- C++ 派生类构造函数
- 一个简单telnet服务端程序实现
- 尹老师视频(关于char *string ="ahkdh"和char str[20]="ahkdh")
- Akka 资料集合
- 第一周项目零(2):阅读程序
- 例题1.21 子序列 UVa1121
- 阿里校招前端工程师实习生面试心得及体会
- CSDN的markdown编辑器
- JQuery Ajax参数详解