C ++ getting started learning notes inheritance

zhaozj2021-02-11  209

*** Inherit, the top of the object-oriented program design, there is a top and down to eat --- skyala *** people (class) -> parents (object) -> children (inherit, multiple inheritance) -> children (more Advantage) --- Wow, my god! What perfect curve!

***********************************statement************** ***************** 1. Inheritance concept and importance 2. Base class 3. Derive class 4. Base class VS derived class 5. Multiple inheritance 6. Application example a. Construction, destructive call sequence, redefine function (tpoint.cpp) b. Multi-inheritance and object, pointer application fully interpret this relationship (TobjPoi.cpp) 7. Question (too much, don't understand) base class, derived Class and pointer, object relationship * Base, send pointer to directly reference the base, send object time * t, c; t = & c; member of the member) * The base class pointer can reference the derived class object, ie The derived pointer can directly assign the base class pointer. The referenced member can only be a base class part * t; C2; T = & C2, T-> C2 (the base class member within it) * Derife pointer cannot directly reference the base Class object (different types of inclusive), that is, the base class pointer cannot directly assign a derived pointer. Let's first conversion: the base class to the base class, the derived class pair or derived the group (same 2) .time * t, c; time2 * t2, c2; t2 = & c (errors). Change 1: t = & c T2 = (TIME2 *) T is equivalent to T2 = (TIME2 *) & C forced base clauses to convert to derived pointers and then assign derived class T = & C2; change 2: t2 = (time2 *) & T total: base Class pointer package base, package party genus pointer, automatic conversion derived class pointer to convert to the basis or convert part to the same type of party pointer ******************************************** ************* Define ******************************* 1. Inherited Concepts and Importance Inheritance: It is a form of software reuse, organizes the associated classes, and is a common data and operational behavior between Heng. The most attractive feature: New categories can be inherited from existing class libraries. Advocating the establishment of a new class with many common class to achieve software reuse can add the characteristics of the base class and replace and improve the characteristic single weight inheriting from the base class, single weight, forming a tree hierarchical structure, by the base class And derived classes constitute a hierarchical relationship, and inherited levels are arbitrary in the limits of the system. The inheritance mechanism defines the parent-child relationship 2. Base class * The parent class defines all subclasses to exchange the external interface and private implementation content, the parent class is called the base class * member function: the private member of the base class can only be classified by the base class The protected members of the function and friend access base class can only be used by the base class and the members of the members and the friend access * Inheritance base class, the public inheritance is the most, the public, the public, protected, the private, private, no protected : Public, Protection Protection, Private: Public, Protection Blood 3. Detective Class * New Class Inherit Predefined Batalog Data Members and Member Functions, without having to revoke data members and member functions, this new class is derived ( Derived) Class * Directive class Never directly access the private member of the base * Refine functionality in which no inheritance in the function class and the base class feature to be extended can redefine member functions, but in the derived class, then call the same name function When you use the scope operator Employee :: Print () * The constructor and destructuring function of the derived class will inherit the basis of the base class because the derived class is inherited, the construction of the base class must be called. The function is initialized to initialize the base class member in the party. The implicitly invoke the base class constructor, or in the constructor of the derived class to provide the initialization value (member initialization value list) to the base class (the member initialization value list), first perform the constructor of the base class, first execute the construction function of the base class - > Member object constructor of derived class -> Detective class constructive function pairing function call order is opposite.

Remember: Detective Class does not inherit the construction function, destructive function and assignment operator of the base class, but the constructor of the derived class and the assignment operator can call the constructor and assignment operation of the base class. .................................................................................................................. .. You can access the member functions of the base class and protected data, and the derived class object assigns to the base class object is also reasonable. Conversely, the objects of the base class cannot be automatically used to become the object of derived class, because the derived class has some no * pointer to the pointer to the base class in the object. The pointer of the base class can be implicit. That is, the derived pointer can directly assign to the base class pointer, but it is not possible to convert the base class pointer to the derived class pointer, if necessary to convert it, you should point it first before conversion. A derived class object (due to derived class objects that do not exist in this object) such as Char * C; (int *) c; * Both relationships Direct class pointer reference base class object: Time * T, C ; * t = & c; * T Return to the object C. Qu -> (member) Directly use the derived class pointer to reference a derived class object with the base class pointer, but only reference base class members (in the polymorphism Very useful) Time * T, Time2 C; * T = & C; * T Returns the reference to the object of the party, T -> (base class member) uses a classic class pointer to reference the base class object, absolutely not. It must be forced to convert to base clauses TIME T, TIME2 * C; * C = & t, * c and c -> (member) error Time T, Time2 * C; * c = (time *) C C-> (member The correct pointer operation is to use an arrow member to select an operator to access members. No matter how to assign a value, the object's reference must have this data or member function - Skyala5. Multiple inheritance * A class can be derived from multiple base classes, which is a multi-inheritance. It is a graphic hierarchy (with a directionless). Note: It is powerful, but it is easy to cause two-purpose, using a binocular scope to solve the method * Usage: A type A, is type B, is also a type C * colon (:) plus public base class list Class T: Public D, public c {} program art - procedures that have no thought will never touch people - Skyala

转载请注明原文地址:https://www.9cbs.com/read-4285.html

New Post(0)