《C++ Primer Plus》第十三章学习笔记

 

143:面向对象编程的主要目的之一是提供可重用的代码。

 

144:公有继承时,基类的私有部分只能通过基类的公有或保护方法访问。

 

145:What needs to be added to these inherited features?

• A derived class needs its own constructors.

• A derived class can add additional data members and member functions as needed.

 

146:When creating an object of a derived class, a program first calls the base-class constructor and then calls the derived-class constructor. The base-class constructor is responsible for initializing the inherited data members. The derived-class constructor is responsible for initializing any added data members. A derived-class constructor always calls a base-class constructor. You can use the initializer list syntax to indicate which base-class constructor to use. Otherwise, the default base-class constructor is used.

When an object of a derived class expires, the program first calls the derived-class destructor and

then calls the base-class destructor.

 

147:派生类与基类之间的一些特殊关系:

         1、a derived-class object can use base-class methods, provided that the methods are not private

         2、a base-class pointer can point to a derived-class object without an explicit type cast

         3、a base-class reference can refer to a derived-class object without an explicit type cast

 

148:C++ has three varieties of inheritance: public, protected, and private.

 

149:There are two key mechanisms for implementing polymorphic public inheritance:

• Redefining base-class methods in a derived class

• Using virtual methods

 

150:关键字virtual只用于类声明的方法原型中,而不用于方法定义中。

 

151:The setf() method returns a value representing the format state before the function was called.

Eg: ios_base::fmtflags initialState = cout.setf(ios_base::fixed, ios_base::floatfield);

 

152:you have a way of representing a collection of more than one type of object with a single array. This is polymorphism.

 

153:如果要在派生类中重新定义基类的方法,则将它设置为虚方法;否则,设置为非虚方法。

 

154:How Virtual Functions Work:to add a hidden member to each object. The hidden member holds a pointer to an array of function addresses. Such an array is usually termed a virtual function table (vtbl).Every different object has different vtbl.If the derived class provides a new definition of a virtual function, the vtbl holds the address of the new function. If the derived class doesn’t redefine the virtual function, the vtbl holds the address of the original version of the function. If the derived class defines a new function and makes it virtual, its address is added to the vtbl.

 

155:Normally, you should provide a base class with a virtual destructor, even if the class doesn’t need a destructor.

 

156:当类声明中包含纯虚函数时,则不能创建该类的对象,包含纯虚函数的类只用作基类。

 

157:纯虚方法用于定义派生类的通用接口。

 

158:Inheritance and Dynamic Memory Allocation:

when both the base class and the derived class use dynamic memory allocation, the derived-class destructor, copy constructor, and assignment operator all must use their base-class counterparts to handle the base-class component. This common requirement is accomplished three different ways.

For a destructor, it is done automatically.

For a constructor, it is accomplished by invoking the base-class copy constructor in the member initialization list, or else the default constructor is invoked automatically.

For the assignment operator, it is accomplished by using the scope-resolution operator in an explicit call of the base-class assignment operator.

 

 

 

书中的错误:

P451倒数第七行多了个{;

P467 倒数第四行的“Star*”应改为“Star&”;“Star::star”应改为“Star::Star”;

P472 第13行的“有元”应改为“友元”。

 

 

 

 

 

 

posted on 2012-01-31 09:27  zyearn  阅读(119)  评论(0编辑  收藏  举报