C + + from scratch (11) Part 1 - kind of knowledge

  C + + from scratch (11) Part 1 

  – Kind of knowledge 

  It has been introduced since the definition of the types of variables and function of the concept, and gives their respective semantics, the definition of this paper to describe the contents of the remaining types, and that their semantics. 

  Authority 

  Members of the function provided, making the definition of types of semantic resources has been elevated to a function of resources.    What is a function of resources?    For example, to radio for digital mapping, mapping the need to adjust the operation of the radio frequency to receive different radio, adjusting the radio volume; opened and closed the radio in order to prevent the loss of electricity.    To this end, the radio should be mapped to the structure, similar to the following: 
  Struct Radiogram 
  ( 
  Double Frequency; / frequency * * / void TurnFreq (double value); / / frequency change 
  Float Volume; / * volume * / void TurnVolume (float value); / / change the volume 
  Float Power; / * power * / void TurnOnOff (bool bOn); / / Switch 
  Bool bPowerOn; / / is opened 
  ); 
  Radiogram above:: Frequency, Radiogram:: Volume and Radiogram:: Power due to the definition of Radiogram members, their semantic difference for a radio frequency, the volume of a radio and a radio power.    The remaining three members of the semantic function also for a change in the frequency of radio, a change in the volume of the radio and a radio turned on or off the power supply.    This attention of the "a", which said that the specific radio do not know, only through operators will be members of the left of a specific combination of radio and they only know what radio, which is why they are called offsets types.    This is the next one will be elaborate. 
  Attention to the question: Why should just mapping operations for the three members of Radiogram function?    This is because the function of the radio?    So the election watermelon, melon and eat watermelon Qixi, Are we going to define a structure, and then to define the three elections, cutting, a member of eating function?    ?    Not ridiculous?    The former operator of the three members of the variable structure, which is the structure itself.    Well into eating fast food, eating fast food hamburgers, French fries and fast food to eat fast food drink cola.    If the two here to eat and drink in a fast-food operation has become a member function that is the function of fast food?    !    This is actually a programming thinking of the problem, and this fact is the so-called object-oriented programming thinking Although it is a very good idea, but not necessarily suitable, and next will be discussed in detail. 
  The reason we said above Radio is the time for a function, because they are practical, we can not directly change the radio frequency must be elected by rotating knob that Taiwan to change the frequency of receiving the same, the volume is also stressed by adjusting the volume spin button to achieve, and the boot as a result of decreased electricity is not a direct result of us, but indirectly through a result of listening to the radio.    So the above Radiogram:: Power, Radiogram:: Frequency, and other members of variables have a special characteristic - the outside world, radios outside the things they can not change the.    To this end, C + + provides a semantic syntax to achieve this.    Fu in the type definition, this is the format: <competence>:.    Here <competence> for public, private and protected in one of the public were called, and the protection of private ownership, as follows: 
  Class Radiogram 
  ( 
  Protected: double m_Frequency; float m_Volume; float m_Power; 
  Private: bool m_bPowerOn; 
  Public: void TurnFreq (double); void TurnVolume (float); void TurnOnOff (bool); 
  ); 
  Can be found, and before the definition of the format of the same grade, but not Xiuchifu statements that can struct ABC (private:);.    Here not insist in private: followed by statements, because it is not Xiuchifu statement.    From its beginning until the next such grammar, all between the statement and the definition of variables or functions are with the members it represents semantic.    For example, the above categories Radiogram, the Radiogram:: m_Frequency, Radiogram:: m_Volume and Radiogram:: m_Power is to protect the members of variables, Radiogram:: m_bPowerOn is a private member variables, and function of the remaining three members are members of the public function.    Note that the above grammar can be repeated, such as: struct ABC (public: public: long a; private: float b; public: char d;);. 
  What does that mean?    Very simple, the members of the public can visit the outside world, the protection of the members of the outside world can not access, private outsiders and members of the class could not visit.    On the back of the class.    Look at the public.    For the above, the error will be as follows: 
  Radiogram a; a.m_Frequency = 23.0; a.m_Power = 1.0f; a.m_bPowerOn = true; 
  Because on the face of a three operators have used a protected or private members, the compiler error will be, because the two members of the outside world can not visit.    The a.TurnFreq (10); there is no problem because members of Radiogram function:: TurnFreq members of the public, access to the outside world.    So what is the outside world?    For a certain type of self-definition, since the definition of this type of the function of the body other than the function can write all the parts of the code known as the outside world.    Therefore, the above Radiogram, only three members of its function in vivo function of its members can visit variables.    Below is the code will be no problem. 
  Void Radiogram:: TurnFreq (double value) (m_Frequency + = value;) 
  Because m_Frequency used in the local Radiogram:: TurnFreq a function of the body, not outside. 
  Why should this be?    The first performance of the semantic said.    First, members of the above will be defined as public or private generated code for the end have no effect.    Then, I said that before receiving the regulation of radio frequencies through the adjustment of the total harmonic capacitor inside the capacity to achieve, and the capacity of this capacitor must be done with components, and will receive frequencies into digital mapping, since it is digital, CPU will be able to modify.    If direct a.m_Frequency + = 10; revision of the code on the significance of its on: the implementation of this approach will be the increasing frequency radio receiver 10 KHz, which would run counter to our objective world, and not in front of semantics.    Therefore it as a kind of grammar provided by the compiler to be reviewed so that we can produce more in line with the world we live in the semantic code. 
  Attention should be (long a union ABC; private: short b;);.    Here's ABC:: a no-modified before that it is public or protected?    I believe the move from the front have been so many examples that should be public, which is why I have been using before struct and union to define custom types, otherwise the examples will be before the error.    And the first chapter of the structure and said that only a small difference, that is, when members not modified, the category that members will be private rather than public, the following will be wrong. 
  (Long a class ABC; private: short b;); ABC a; aa = 13; 
  ABC:: The front of a class and be regarded as private.    From this point on, we can see that structure used for mapping of resources (can be used directly resources), and a mapping function for the type of resources.    Next they will be discussed in detail in the semantic difference. 

  Analysis of structure and conformation 

  Understand the things mentioned above, it is obvious there are the following questions: 
  Struct ABC (private: long a, b;); ABC a = (10, 20); 
  The above also initialize a variable assignment correct?    Of course, mistakes in grammar, or even if it has a flaw (outsiders can be amended to modify the members).    But some time is also needed to ensure that the initialization logic, this proposed C + + Analysis of the structure and the concept of structure, corresponding to initialization and off the work.    In the understanding of this before, so let us look at an example of what is (Instance). 
  Example is abstract concept that an objective existence, and the next will introduce the "World" concept closely linked.    For example: "This is the table" and "table", the former "table" type, the latter's "table" is an example.    There are 10 sheep, says there are 10 examples of sheep and goats is only one type.    Can simply be considered to be examples of the objective world objects, for reasons of convenience and humanity to a sub-category of objects, it is TV that did not give examples of TV, and a TV show is to the example of a TV.    Similarly, the process of code written by the little significance, only when it is implemented, we said that the procedure is an example of operation.    If it had not been implemented in the end when it requested the implementation of the operating system, the multi-task operating system, can be said that two examples of procedures are being implemented, such as the point at the same time opened two Word documents Show, there are two Word examples of procedures in the operation. 
  In C + +, can be operated only figures, is an example of a figure (in the notes in the next can be seen), more general, said the number of memory identification records for a case in the address, that is, variables that One example, and the type of response that is the object of the above types.    For example: long a, * pA = & a, & ra = a; here on the formation of the two examples, one of the examples is long, one of the examples is long * (ra attention because it is long & not create instances, but still ra is an example).    Similarly, for a custom type, such as: Radiogram ab and c [3];, said the formation of the four Radiogram examples. 
  Since the definition of the type of case, when its was generated, it will be called the corresponding structure function; when it was destroyed, will be called Structure Analysis of the corresponding function.    Who is calling?    Compiler is responsible for helping us prepare the necessary code to achieve the corresponding structure Analysis of the structure and call.    Constructor function prototypes (corresponding function of the type, such as the float AB (double, char); the prototype float (double, char)) format: directly from the definition of the type of function as a type of no return value types, parameters are casually.    Analysis of the structure function, a type name for the previous corresponding increase symbol "~", there is no return value type, parameters must be no.    As follows: 
  (Struct ABC ABC (); ABC (long, long); ~ ABC (); bool Do (long); long a, count; float * pF;); 
  ABC:: ABC () (a = 1 count = 0; pF = 0;) 
  ABC:: ABC (long tem1, long tem2) (a = tem1; count = tem2; pF = new float [count];) 
  ABC:: ~ ABC () (delete [] pF;) 
  Bool ABC:: Do (long cou) 
  ( 
  * P = float new float [cou]; 
  If (! P) 
  Return false; 
  Delete [] pF; 
  PF = p; 
  Count = cou; 
  Return true; 
  ) 
  Extern ABC g_ABC; 
  Void main () (ABC a, & r = a; a.Do (10); (ABC b (10, 30);) ABC * p = new ABC [10]; delete [] p;) 
  ABC g_a (10, 34), the new ABC g_p = [5]; 
  ABC on the above definition of the structure of the two constructor function (note the two override function), the name for both ABC:: ABC (actual compiler will be transferred into different symbols for the links.)    Analysis also defines a structure function (Note that only a definition, because of its parameters must not, it can not be a heavy load), the name for ABC:: ~ ABC. 
  See main function, ABC a first pass; definition of a variable, as in a stack on the allocation of memory, that is, to create a digital (created with digital memory will lead to the creation of a digital, not because of memory installed), then ABC created a precedent, then called ABC constructor.    Because here did not give parameters (behind that), calling the ABC:: ABC (), then aa 1, a.pF and a.count to 0.    Then defined variables r, but because it is & ABC, and there is no memory allocated in the stack, which does not set up the examples and not call ABC:: ABC.    Call a.Do then, the allocation of memory and an address on the first of a.pF. 
  Note that the above definition of variable b, the use of the function mentioned before-initialization method.    Through the function call format calls the ABC constructor ABC:: ABC (long, long) b initialization ABC examples.    Therefore ba 10, b.count 30, b.pF a memory address of the first block.    However, attention should be paid to such initialization methods before and the reference to "()" of the different ways of the former is to a function call to initialize, which is the compiler to initialize (by generating the necessary code) .    Do not call functions, slightly faster speed (on the overhead functions in the "C + + from scratch (15)" explanation).    It should also be careful not to ABC b = (1, 0, 0);, because ABC has defined two constructor function, it can only be used for initialization function initializes way, and not through "()" Initialization of the way. 
  B above in a large square brackets, I recall the variables mentioned in front of the scopes, so when running the ABC * p = new ABC [10];, variable b has disappeared (beyond its scope), it is the distribution of grammatical memory has been released (actually, since it is in the stack, and had not been released), then call the ABC Analysis of configuration functions, will b in the ABC:: ABC (long, long), the allocation of memory release swap off function to achieve. 
  The adoption of the new memory allocation in Duishang because it is new ABC [10], it will create 10 examples of ABC, which calls for each of the first examples of ABC:: ABC (), here not call attention to ABC:: ABC ( long, long), because the new operator on the one-time allocation of the 10 examples of the required memory space, C + + does not provide grammar (for example, using "{}") to achieve a one-time allocation of the 10 examples to initialize.    Then call the delete [] p;, which has just released the allocation of memory, the destruction of 10 examples, it will call ABC Analysis of the structure function 10 times for 10 off the work. 
  Note that the above statement g_ABC the global variables, as a statement is not defined, not allocate memory, it has not produced an example, it does not call the constructor function ABC, g_a Because it is global variables, C + + global variables guarantee the constructor function at the beginning of the implementation of main function prior to the call, all the global variables Analysis of the structure function in the implementation of End after the main function call (This is the compiler to achieve, and in the "C + + from scratch (19)," will further discussion).    Therefore g_a.ABC (10, 34) call in the a.ABC (), even if it's in a position of the definition of the phrase behind.    G_p and global variable initialization through new figures are calculated on the basis of the operator, the results will be in Duishang allocate memory, thereby generating five examples and ABC called the ABC:: ABC () five times, as in the early g_p before the time of the distribution, it is also five times call a.ABC () before.    As g_p merely recorded the first address, and to the release of the five examples must call delete (not necessarily, or not delete still call the return of the release of new memory, in the "C + + from scratch (19)," Note), but the above does not call, it will be until the end of proceedings would not call it five out examples of configuration functions, it will be?    Behind the abnormal to discuss the so-called memory leak problem. 
  Therefore structure means that the allocation of just a memory, not yet initialized, this memory is called the original data (Raw Data), said earlier figures must be mapped into the algorithm of resources, the effective figure there sexual.    For example, mapping the age, this figure can not be negative, because of no significance.    So when are the original data, it should first go through the constructor function call to ensure that the corresponding examples of the correct meaning.    Analysis of the structure and function to that off the work, as above, to a certain examples of the operation of the period (that is the example of this operation code enforcement period) a number of dynamic memory allocation, it should ensure that its release was correct.    Or again this example and other examples that, due to ensure the lifting of (because this case will soon be destroyed), such as a linked list of nodes with type mapping, then this node should be deleted in its structure function lifting Analysis with the other nodes relations. 

  Derivative and inheritance 

  Our definition of the above categories Radiogram to mapping radios, if necessary mapping and digital radios, and radio like it, that is what it has radios have, but found more automatic Taiwan, storage, Taiwan, the Taiwan election platform and delete functions.    Here proposed a type system, if that is an example of digital radio, which it definitely is the radio, that is an example of the radio.    For example, apples and pears are fruits, apples and pears are examples of some of the examples of fruit.    Here for the three types: fruits, apples and pears.    Apple said fruit which is the parent class (father types), Apple is the fruit of the subclass (subtype).    Similarly, the fruit is the father of pears and pear fruit is a subclass.    This type of system is of great significance, because humanity is to use this approach to understanding the world, it very much in line with human thought habits, C + + has also put forward a special syntax to provide support to such semantics. 
  Since the definition in the definition type, type in the name followed by one ":", and then to public or private or protected, and then write the type of father who, at the end is the type definition of "()" and related writing, as follows: 
  Class DigitalRadiogram: public Radiogram 
  ( 
  Protected: double m_Stations [10]; 
  Public: void SearchStation (); void SaveStation (unsigned long); 
  Void SelectStation (unsigned long); void EraseStation (unsigned long); 
  ); 
  Above will Radiogram DigitalRadiogram definition to the parent category, Radiogram DigitalRadiogram definition of a sub-category, and has been called a derivative of Radiogram DigitalRadiogram category, a category DigitalRadiogram succession of Radiogram. 
  Formation above the five mapping element is the four members of the above function and a member variable, but not practical.    Because it is derived from Radiogram, it will also generate seven mapping, is the type Radiogram seven members, but the name change, all become DigitalRadiogram:: modified, rather than the original Radiogram:: modified, but not type changes.    For example, one of the names on the map elements for DigitalRadiogram:: m_bPowerOn, type bool Radiogram::, mapping offset value did not change, still 16.    Equally, there is a mapping elements DigitalRadiogram:: TurnFreq, type void (Radiogram::) (double), the address mapping still not changed, as Radiogram:: TurnFreq the corresponding address.    So we can as follows: 
  Void DigitalRadiogram:: SaveStation (unsigned long index) 
  ( 
  If (index> = 10) return; 
  M_Station [index] = m_Frequency; m_bPowerOn = true; 
  ) 
  DigitalRadiogram a; a.TurnFreq (10); a.SaveStation (3); 
  Although there is no DigitalRadiogram above statement:: TurnFreq, but can still call it, because it is derived from the past Radiogram.    As attention a.TurnFreq (10); not write full name, it is actually a.DigitalRadiogram:: TurnFreq (10), left because of the number of operators is DigitalRadiogram type.    If DigitalRadiogram not derived from Radiogram, it is not generating the above said seven mapping, the results a.TurnFreq (10); will be wrong. 
  Note that the above SaveStation, direct writing of the m_Frequency, the equivalent to this-> m_Frequency, because this is DigitalRadiogram * (because DigitalRadiogram:: SaveStation a function of the body), the reality is this-> DigitalRadiogram:: m_Frequency, therefore, If it is not derived from Radiogram, it will be above error.    And by the type of match, it is easy to know: void (Radiogram:: * p) (double) = DigitalRadiogram:: TurnFreq;.    Although it is DigitalRadiogram:: TurnFreq, but it is the type of void (Radiogram::) (double). 
  It should be noted in the use of the m_bPowerOn SaveStation, Radiogram, this was defined as private members, that is also not the right type of visit, and his son SaveStation category is a member function, and hence will be above error, lack of competence. 
  Above and through derivatives generated seven mapping elements of their respective competence?    Let us look at the above derivative code: 
  Class DigitalRadiogram:… public Radiogram (); 
  Here the use of public, and has been called DigitalRadiogram Radiogram from public inheritance, protected, as if into the protection of succession, if it is private is private inheritance.    What is the difference?    Through public succession generated mapping elements (referring to Radiogram derived from the generation of seven mapping elements), the competence of the respective attributes do not change, that is, above DigitalRadiogram:: m_Frequency DigitalRadiogram, the category is still protected, and DigitalRadiogram:: m_bPowerOn also remain private.    Inheritance is the protection of all members of the public have become protect its members, the other unchanged.    If the protection of inheritance, DigitalRadiogram:: TurnFreq for DigitalRadiogram, will be protected.    Private inheritance will be the father of all the members of a sub-category is the private.    So if the above private inheritance, DigitalRadiogram:: TurnFreq DigitalRadiogram is for the private. 
  The top can be seen very simply that no matter what inheritance, a designated authority, the father of any higher authority in the mapping elements, they must be reduced to the competence of the respective competence of this (note the pair, category) , and then inherited by subclasses.    Above has been stressed that "the sub-class," What does that mean?    As follows: 
  (Struct A long a; protected: long b; private: long c;); 
  Struct B: protected A (void AB ();); 
  Struct C: private B (void ABC ();); 
  Void B:: AB () (b = 10, c = 10;) 
  Void C:: ABC () (a = 10, b = 10, c = 10; AB ();) 
  A a; B b; C c; aa = 10; ba = 10; b.AB (); c.AB (); 
  B above equivalent to the definition of protected (struct B: long a, b; private: long c; public: void AB (););. 
  The above definition equivalent to the C struct C (private: long a, b, c; void AB (); public: void ABC ();); 
  Therefore, B:: AB in the b = 10; no problem, but c = 10; a problem, because the compiler see B:: c inherited from the father of generation, and the father of it is private members , the right to visit sub-class, wrong.    Then see C:: ABC, a = 10, and b = 10; no problem because they are all for the protection of members of B, c = 10; will be wrong, because C:: c B, the father of is a private members do not have access failure.    Then AB (); because C:: AB category B, the father of members of the public, no problem. 
  Followed by aa = 10;, no problem; ba = 10, error, because B:: B is the protection of a member of b.AB ();, no problem; c.AB (), error, because C: : C AB is a private members.    It should be noted that: public, private and protected types Xiuchifu not only grammatical provided some information, inherited from the type of members will not change, regardless of its protection of inheritance or succession public, role of the local authority is need to use members of the local, and no type of relationship.    What is the use of members of the local?    As follows: 
  Long (A:: * p) = & A:: a, p = & A:: b; 
  Void (B:: * pB) () = B:: AB; void (C:: * pC) () = C:: ABC; pC = C:: AB; 
  On the face of the p variable initialization no problem here on the use of the A:: a.    However, p = & A:: b;, due to the use of the A:: b, the compiler code to check which areas are found outside the A, and therefore the error, not competence.    Below on the same assignment pB no problem, but pC = C:: AB; on the error.    For ba = 10; here because of the use of operators and the members of the Class B B:: a, so check here competence, and thus found a lack of competence error. 
  Good, why do so complicated?    Lane what protection, private and public inheritance?    First recall that the previous succession to provide Why, because I want to reflect on the type of code from the system, if that is an example of an instance of the class, it certainly is a father instance of the class, according to the father of the definition of operation it.    Although this can also be said that before the adoption of the conversion pointer type to achieve, but the former directly from the code can be displayed on the type of inheritance semantics (ie, sub-class category derived from the father), which only shows that the use of different types to look at the same an example. 
  Then why do you give the succession of authority?    To the outside world that this class or subclass it to the father of the posture to look at it.    For example, the chicken can be consumed, but the cause of the specimens can not be eating chicken.    Therefore category of "chicken samples" in the protection of succession should inherit the father of "chicken" to the outside world that are not allowed (but allow their derivatives) as it is chicken.    It is no longer a chicken, but it is actually coming from the chicken change.    And the protection of private inheritance therefore very suitable for the actual performance of the evolutionary relationship between animals.    For example, people in the evolution of monkeys, but people are not monkeys.    Here people should use private inheritance, because people do not want the outside world and sub-categories - black, yellow and white people, etc. - to the father of the "person" as a monkey.    And the public that the succession to the outside world and sub-categories can be regarded as an example of the type Father instance of the class.    As follows: 
  (Struct A long a, b;); 
  Struct AB: private A (long c; void ABCD ();); 
  Struct ABB: public AB (void AAA ();); 
  Struct AC: public A (long c; void ABCD ();); 
  Void ABC (A * a) (a-> a = 10; a-> b = 20;) 
  Void main () (AB b; ABC (& b); AC c; ABC (& c);) 
  Void AB:: ABCD () (AB b; ABC (& b);) 
  Void AC:: ABCD () (AB b; ABC (& b);) 
  Void ABB:: AAA () (AB b; ABC (& b);) 
  AC above categories of public succession, so its example in the implementation of ABC c (& c), the compiler will be carried out implicit type conversion, this is a very peculiar characteristics, the paper will describe the next.    But the succession of AB is a private, in the ABC (& b), the compiler will be no implicit type conversion, error, the type does not match.    For this only ABC ((A *) & b) to show that a type-no problem. 
  Attention to the front of The Scarlet Letter, private and the outside world that it inherited from the base class can not use the father of the posture to look at it, so ABB:: AAA, this is the sub-type AB, here's ABC (& b); will be the error.    In AC:: ABCD, for here is outside AB, the error.    In AB:: ABCD, here is their own, that is not sub-class is not outside, so ABC (& b) will be no problem.    If AB will be replaced by the protection of inheritance, in the ABB:: AAA in the ABC (& b); will no longer be wrong. 
  On the next paper and paper discussed by the semantics, in the "C + + from scratch (12)," will be devoted to a concept of a programme to give guidance on how to design and of the relationship between the various types.    Due to space limitations, this article is divided into the upper and lower two, the remaining contents of this paper in the next note. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “C + + from scratch (11) Part 1 - kind of knowledge”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.