1, Inheritance, dynamic binding, and data abstraction become the foundation of object-oriented programming.
2Templates enable us to write generic classes and functions independent of specific types. InC ++Class to inherit from one class: A derived class to inherit the members of the base class. Dynamic binding enables the compiler to determine whether to use functions defined in the base class or functions defined in the class at runtime.
3,C ++Polymorphism is only used to reference or pointer to the type associated with inheritance.
4, DefinedVirtualThe base class is expected to be redefined by the derived class. Reserved WordsVirtualIt only appears in the member function declaration within the class.
5Once the function is declared as a virtual function in the base class, it is always a virtual function.
6The object of the derived class is composed of multiple parts: the object defined by the derived class itself(NonStatic)Add the base class to the member(NonStatic)Child object composed of members.
7And the defined class can be used as the base class.
8If you need to declare (but not implement) A derived class, declare that it contains the class name but does not contain the derived list.
Example
// Error: A Forward Declaration must not include the derivation listclass bulk_item: Public item_base; // The correct Forward Declaration is: // forward declarations of both derived and nonderived classclass bulk_item; class item_base;
9To trigger dynamic binding, two conditions must be met :(1) Can be dynamically bound only when it is specified as a virtual member function ;(2) The function must be called through a reference or pointer of the base class type.
A reference or pointer to a base class type can reference a base class type object or a derived class object.
10The key points of base class type reference and pointer are static type (reference type or pointer type known during compilation) it may be different from the dynamic type (pointer or reference to the type of the bound object, which is only known at runtime.
The static and dynamic types of references and pointers can be different. This isC ++It can support the cornerstone of polymorphism.
11Overwrite virtual functions
Sometimes, to override the virtual function mechanism and force function calls to use a specific version of the virtual function, you can use the scope OPERATOR:
Example
Item_base * basep = & derived; double D = basep-> item_base: net_price (42); // call the base class version
12When a virtual function of a derived class calls the base class version, the scope operator must be explicitly used. If the function of the derived class ignores this, the function call is determined at runtime and will be a self-called function, resulting in infinite recursion. (It refers to calling a function of the same name as the base class in the virtual function of the derived class, so that the base class can do some necessary work)
13Like any other function, virtual functions can also have default real parameters. Generally,If the default real parameter value is used in a given call, the value will be determined during compilation..If a call omitting a real parameter with a default value, the value used is defined by the type that calls the function, regardless of the dynamic type of the object.When you call a virtual function through a base class reference or pointer, the default real parameter is the value specified in the base class virtual function declaration. If you call a virtual function through a pointer or reference of a derived class, by default, the real parameter is the value declared in the version of the derived class.
Using different default real parameters in the base and derived classes of the same virtual function is almost troublesome.If you use a base class reference or pointer to call a virtual function, but the version defined in the derived class is actually executed, a problem may occur. In this case, the default parameter defined for the base class version of the virtual function is passed to the version defined by the derived class, and the derived class version is defined by different default real parameters.
14If the member is in the base classPrivate,Only the friends of the base class and base class can access this member.
15Interface inheritance and implementation inheritance
PublicA derived class inherits the interface of the base class. It has the same interface as the base class and is called interface inheritance.
PrivateAndProtectedThe interface of the derived class that does not inherit the base class is called implementation inheritance. In the implementation of a derived class, the inherited class is used, but the inherited base class is not part of its interface.
16Inheritance: reflectsIs. The combination reflectsHas.
17, You can useUsingDeclare the name of the Access Base Class(UsePrivateInheritance is necessary.).
Example
Class derived: Private base {public: using base: size ;//...};
18, UseClassBy default, a derived class with a reserved word definition hasPrivateAnd useStructReserved Words are defined by default.PublicInheritance. This is their only difference: the default Member Protection Level and the default derived protection level.
19Youyuan relationship and inheritance
The relationship between friends and friends cannot be inherited. The principal of the base class has no special access permission on the class derived from the base class.
InHttp://www.cnblogs.com/mydomain/archive/2011/03/22/1991244.html, I go throughVs2008Compile the environment to test whether the relationship between friends and friends can be inherited. While C ++ primer 4 Th Center 15Chapter describes this situation and it cannot be inherited. We tested the example and found that it can also be compiled.
It should be a compilation environment issue.
Example
# Include "iostream" # include "vector" # include "algorithm" # include "string" # include "functional" using namespace STD; class base {friend class frnd; protected: int I ;}; class D1: public base {protected: Int J ;}; class frnd {public: int MEM (base d) {return D. i;} int MEM (d1 D1) {return d1. I;} // error: Friendship dosenot inherit}; Class D2: Public frnd {public: int MEM (base d) {return D. I ;}; // "base: I": the protected member cannot be accessed. An error occurred during compilation.}; int main () {return 0 ;}
20, Inherited and static members
If the base class is definedStaticOnly one member in the hierarchy . No matter how many derived classes are derived from the base classStaticThe Member has only one instance.StaticThe member follows regular access control: if the member is in the base classPrivate, The derived class cannot access it. If you can access members, you can access them through the base class.StaticMembers can also be accessed through a derived classStaticMember. Generally, you can use either the scope operator or the DOT or arrow member access operator.
Reference
[1]Http://blog.163.com/zhoumhan_0351/blog/static/3995422720100284731826/