博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 派生类构造函数
阅读量:4090 次
发布时间:2019-05-25

本文共 3174 字,大约阅读时间需要 10 分钟。

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.

转载地址:http://iyyii.baihongyu.com/

你可能感兴趣的文章
linux 脚本保留日志
查看>>
680. Valid Palindrome II
查看>>
Laravel5.1 路由 -路由分组
查看>>
php 验证码
查看>>
Windows的线程使用
查看>>
LeetCode : Palindrome Linked List
查看>>
Python 编程(小试水)
查看>>
[Webkit]最简单易用的webkit学习环境-ISee
查看>>
day01_步入百万年薪的第一天
查看>>
*HDU1800字典树
查看>>
HDU2527 哈夫曼编码
查看>>
Pycharm远程连接服务器(windows下远程修改服务器代码)
查看>>
用Python调用阿里云的短信接口
查看>>
sctp和tcp的区别
查看>>
瞧,这就是UE4 C++
查看>>
Linux 系统优化参数总结
查看>>
修改virtualbox虚拟硬盘容量
查看>>
python学习一使用list和tuple、条件判断、循环
查看>>
注册这么久,终于开始写博客了
查看>>
Oracle的sql语句,查询条件加括号与不加括号区别 -
查看>>