Effective C + + reading notes (vi) inheritance and object-oriented design __c++

Source: Internet
Author: User
clause 32: Make sure your pubilc inherits the is-a relationship of plastic mold Make sure public inheritance models "Is-a" one of the most important rules for –c++ (OOP) object-oriented programming is that public inheritance (open inheritance) means "is-a" (a) relationship. – If you make class D ("derived") Inherit class B ("Base") as public, you tell the compiler that each object of type D is also an object of type B, and vice versa. – "Pubilc inheritance" means is-a. Everything that applies to base classes must also be used for derived classes, because each derived class object is also a base class object.
Clause 33: Avoid concealing the name of succession
Avoid hiding inherited names.

The name within the –derived classes hides the name within the base classes.

– In order for the hidden name to be available, use declarations or (transfer functions) can be used.


Clause 34: Differentiating between interface inheritance and implementation inheritance
Differentiate between inheritance of interface and inheritance ofimplementation

The –pure virtual function has two of the most salient features: they must be declared anew by any "inheriting them" class, and they are usually not defined in abstract class (base class).

• The purpose of declaring a purevirtual function is to derived classes only to inherit the function interface.

• The purpose of declaring a impurevirtual function is for the derived classes to inherit the interface and default implementation of the function.

• The purpose of declaring the Non-virtual function is to make the interface of the derivedclasses inherited function and a mandatory implementation.

– Interface inheritance differs from implementation inheritance. Under Pubilc inheritance, Derivedclasses always inherits the interface of base class.

The –pure virtual function specifically specifies interface inheritance.

The –impure virtual function specifies interface inheritance and default implementation inheritance.

The –non-virtual function specifically specifies interface inheritance and mandatory implementation inheritance.



Clause 35: Consider a choice other than the virtual function consider alternative to virtual functions.

– Using Non-virtualinterface (NVI), which is a special form of the template method design pattern. It wraps the virtual function of the lower access (private or protected) with the public non-virtual member function.

Replace the virtual function with the function pointer member variable. Moving functionality from member functions to class external functions has the disadvantage that non-member functions cannot access non-public members of class



clause 36: Never redefine an inherited non-virtual function

Never Redefine an inherited non-virtual function.

– Never redefine the inherited non-virtual function.



clause 37: Never redefine inherited default parameter values

Nerver redefine a function ' s inherited default Prameter value.

– Never redefine an inherited default parameter value because the default parameter values are statically bound, and the virtual function-----The only thing you should copy-------is dynamically bound.



Clause 38: has-a or "achieve according to STH" by compound moulding Model "Has-a" or "is-implemented-in-terms-of" through composition.

– Composite (composition) is a relationship between types, which is the case when a type of object contains its type of object.

– The meaning of the compound (composition) is completely different from the public inheritance.

– In the application domain (Applicationdomain), the compound means has-a (with one). In implementation domains (implementation domain), compounding means is-implemented-in-terms-of (implemented according to something).



clause 39: Judicious and prudent use of private inheritance Use private inheritance judiciously.

– If the inheritance relationship between classes is private, the compiler does not automatically convert a DerivedClass object to a base class object.

All members that are inherited by the –private base class become Private in the derived class.

C + + determines that all independent (not affiliated) objects must have a non 0 size. Ordered silently to insert a char into the empty object.

EBO (blank base class optimization):
Class Empty ();

Class Holdsanint:private Empty

{

Private

int x;

}

sizeof (holdsanint) = = sizeof (int);


clause 40: Judicious and prudent use of multiple inheritance Use multiple inheritance judiciously

– Multiple inheritance is more complex than single inheritance. It can lead to new ambiguity and the need for virtual inheritance.

–virtual inheritance increases the cost of size, speed, initialization (and assignment) complexity, and so on.

– There is a legitimate use of multiple inheritance. One of these episodes involves a two-phase combination of "public inheriting a interface class" and "private inheriting one of the help-implemented classes"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.