C ++ learning starts from scratch (1)

Source: Internet
Author: User

From: http://www.duote.com/tech/4/11911.html

 

C ++ learning from scratch (I) Source: 2345 software Daquan time: popularity: 64441 I will comment

Tutorial tips:The middle part of this article has already introduced the meaning of virtual reality, that is, indirect acquisition, and examples show that TV channels allow people to indirectly obtain TV station frequencies. Therefore, in this sense, it is virtual, it may fail because a channel has not been properly tuned, resulting in a snowflake.

The middle part of this article has already introduced the meaning of virtual reality, that is, indirect acquisition, and examples show that TV channels allow people to indirectly obtain TV station frequencies. Therefore, in this sense, it is virtual, it may fail because a channel has not been properly tuned, resulting in a snowflake. The indirect advantage is that you only need to compile a piece of code (by frequency 5 ).

The result may be different each time you execute it. (today, Channel 5 is set to 5 in the center, and tomorrow it can be set to 2 in the center ), this makes the preceding program (by Channel 5) very flexible. Note that the reason why virtual reality can be flexible is that it must be achieved indirectly through "one means", for example, each channel records a frequency. However, this is not enough. There must be another piece of code that can change the results of that method (the frequency of channel recording), such as redeployment.

First look at virtual inheritance. It indirectly obtains the location of the parent class instance from the subclass instance and implements it through the Virtual class table (this is a "method "), then, you must have "another piece of code" to change the value of the virtual table to show its flexibility. First, you can write this code by yourself, but it requires that you know where the compiler places the virtual class table, and different compilers have different implementation methods, so the compatibility of the written code is very poor. C ++ certainly provides "another piece of code", that is, when a class is falsely inherited multiple times in the same class inheritance system, changes the value of the virtual class table so that the parent class instance indirectly obtained by each subclass is the same. This operation only saves memory.

For example:

Struct a {long ;};

Struct B: virtual public a {long B ;}; struct C: virtual public a {long c ;};

Struct D: Public B, public c {long d ;};

Here, D has two virtual class tables inherited from B and C respectively. In the constructor of D, the compiler will write the necessary code to correctly initialize the two virtual class tables of d so that the instances of a inherited by B and the virtual class tables inherited by C are the same. one.

Look at the virtual function. Its address is indirectly obtained through the virtual function table (this is a "method"), and then the content of the virtual function table must be changed. Similarly, if you rewrite the code, the code compatibility is poor, and C ++ also provides "another piece of code". Just like above, you can enter the virtual function table in the constructor of the derived class, write the virtual function table based on the current derived class. It must fill a virtual function table with the address of the type, name, and function that is originally defined as a virtual function that matches as much as possible under the current derived class.

For example:

Struct a {virtual void ABC (), BCD (float), ABC (float );};

Struct B: Public A {virtual void ABC ();};

Struct C: Public B {void ABC (float), BCD (float); Virtual float CCC (double );};

Struct D: Public c {void ABC (), ABC (float), BCD (float );};

In a: A, enter the addresses of two A: ABC and one a: BCD in the virtual function table of.

In B: B, fill B: ABC and the inherited B: BCD and B: ABC in the virtual function table of B.

In C: C, fill C: ABC, C: BCD, And the inherited C: ABC in the virtual function table of C, and add an element: C:: CCC.

In D: D, fill two D: ABC and one D: BCD and the inherited D: CCC in the virtual function table of D.

Here, d inherits from A, B, and C in sequence, and does not generate two virtual function tables because of multiple inheritance. There is only one virtual function table. Although the member functions in D are not modified by virtual, their addresses are still filled in the virtual function table in D, Because Virtual only indicates that the member functions need to obtain their addresses indirectly when using them, it has nothing to do with whether to fill in the virtual function table.

Why do TV sets use channels to indirectly obtain the frequency of TV stations? Because the frequency of the TV station is not easy to remember, and if you know a frequency, slowly adjust the capacitance value of the common harmonic capacitor to make the circuit reach that frequency efficiency is very low. For 10 groups of common harmonic circuits, the capacitance values of each group of circuits are adjusted and no longer moved. Different common harmonic circuits are switched to achieve fast conversion frequency. Therefore, the efficiency can be improved indirectly. In addition, Channel 5 is originally a central five, and later it is difficult to replace it with a central two, then the same action (by Channel 5) will produce different results, the program "by Channel 5" is very flexible.

At least we can know from the above: indirectly used to simplify operations, improve efficiency and increase flexibility. The three indirect uses mentioned here are all based on the idea that "one means" is used to achieve the purpose and another piece of code is used to achieve the purpose mentioned above. The virtual inheritance and virtual functions provided by C ++ can be used as long as they are members or virtual functions inherited by virtual inheritance ". To implement "another piece of code", we can see from the above description that it needs to be achieved by means of derivation. You can change the virtual function table by defining the same functions as the virtual function prototype declared in the parent class in the derived class, in the inheritance system of a derived class, the virtual class table can be changed only when the class that has been falsely inherited is repeated, and it only points to the instance of the same class that has been falsely inherited, it is far from convenient and flexible modification of virtual function tables. Therefore, virtual inheritance is not commonly used, while virtual functions are often used.

  Virtual use

Because the implementation of "virtual" in C ++ requires the use of derivative means, while derivation is the generation type, the "virtual" is generally mapped to the indirect type, rather than the indirect implementation of the above channel through an instance (a set of common harmonic circuits. Note: The "simplified operation" refers to the complex operation of function ing, which simplifies code writing and indirectly executes the corresponding code using the address mapped by function name, virtual functions are called to present multiple execution results. "Improving Efficiency" is an algorithm improvement, that is, the channel is achieved by repeating ten sets of common harmonic circuits, and the authentic space is changed for time, it is not indirectly implemented on the type. Therefore, the "virtual" in C ++ can only increase code flexibility and simplify operations (for the three indirect advantages proposed above ).

For example, when an animal is called, different animals call different methods and make different sounds. In this case, the type of an animal requires "one means" (called) to show different effects (dogs and cats are called different), and this requires "another piece of code" to implement, that is, through derivation. That is to say, from the cat and dog classes of the class animal, you can declare "Call (Gnar)" as a virtual function in animal, and then implement the corresponding Gnar member functions in cat and dog respectively. As shown above, animal: Gnar calls have different effects.

As follows:

Cat cat1, cat2; dog; animal * pa [] ={& cat1, & dog, & cat2 };

For (unsigned long I = 0; I <sizeof (PA); I ++) Pa [I]-> Gnar ();

The above container PA records the reference of a series of animal instances (for reference, refer to "C ++ from scratch (8)"). Its semantics is that this is three animals, as a result, I don't know anything (as if this TV has 10 channels, but I don't know what it is), and then each of the three animals is called once (call

Animal: Gnar. This is what we have said before, increasing flexibility, also known as polymorphism, refers to the same animal: Gnar call, but shows different forms. The above for loop does not need to be written any more. It is "A means". To change its performance, you can use "another piece of code ", that is, different Derived classes are generated, and the instance reference of the derived classes is placed in the array Pa.

Therefore, a member function of a class is declared as a virtual function, indicating that the corresponding function of the resource mapped by this class should be a usage method, rather than an implementation method. The above "call" indicates that an animal "call" does not have to provide parameters or return values. You can call it directly. Therefore, consider the previous radio and digital radio. If one of the functions is the redeployment function, the corresponding function should be declared as a virtual function to indicate that the frequency increase or decrease will be given for the redeployment, the digital adjustment platform and the ordinary adjustment method are obviously different, but no matter. That is to say, people who use the radio don't care how the station is adjusted, but how the station is redeployed. Therefore, virtual functions indicate that the definition of a function is not important. The declaration of a function is important. A virtual function only has an existing meaning in a derived class. The definition of a virtual function provided by the parent class is redundant. Therefore, C ++ provides a special syntax to allow the definition of non-virtual functions. The format is very simple.
0 "is called a pure virtual function.

As follows:

Class food; class animal {public: Virtual void Gnar () = 0, eat (Food &) = 0 ;};

Class Cat: Public animal {public: void Gnar (), eat (Food &);};

Class dog: Public animal {void Gnar (), eat (Food &);};

Void Cat: Gnar () {} void Cat: Eat (Food &) {} void dog: Gnar () {} void dog: Eat

(Food &){}

Void main () {CAT; dog; animal ani ;}

When animal: Gnar is declared above, "= 0" is written after the statement to indicate that the elements mapped to it are not defined. What is the difference between this and "= 0? You can declare animal: Gnar directly without defining it. Note that the above animal ani; will report an error, because in animal: animal, the virtual function table of animal needs to be filled, and it needs the address of animal: Gnar. If it is a common declaration, no error will be reported here, because the compiler will think that the definition of animal: Gnar is in another file, and the connector will process it later. However, because "= 0" is used here to inform the compiler that it is not defined, the above Code will fail during compilation, and the compiler has determined that there is no definition of animal: Gnar.

But what if the animal: Gnar definition is added above? Animal ani; still reports an error because the compiler has determined that there is no definition of animal: Gnar, and the generation of the animal instance is denied even if the function table is not viewed. Therefore, the animal :: the definition of Gnar is useless. However, the ing element animal: Gnar now has a number in the address bar, so there is no problem when cat. Animal: Gnar. If the definition of animal: Gnar is not given, Cat. Animal: Gnar (); is still normal, but an error is returned during connection.

Note that the above dog: Gnar is private, while the animal: Gnar is public, and the result is dog. gnar (); will report an error while dog. animal: Gnar (); but there is no error (because it is a virtual function result or call dog: Gnar), that is, the so-called public is irrelevant to the type, it's just a syntax. There is also class food; you don't have to worry about whether it is declaration or definition. You just need to check what information it provides. There is only one -- there is a type name named "food", which is a type custom type. When declaring animal: Eat, the compiler only needs to know that food is a type name, rather than accidentally typing the wrong word, because food is not used here.

The above animal is called a pure virtual base class. The base class is the top class in the class inheritance system; the virtual base class is a class with pure virtual member functions; the pure virtual base class is a class without member variables and non-pure virtual member functions, only the base class of the pure virtual member function is supported. The above animal defines a rule, also known as a protocol or an interface. That is, animals can be Gnar and eat, and an instance of food must be provided to indicate that animals can eat food. That is to say, the animal type is a manual, indicating the functionality of an animal, and its instance becomes meaningless, and it cannot generate an instance due to the use of pure virtual functions.

What if the above GNER and eat are not pure virtual functions? Then they must all be defined, and then animals are no longer an abstract concept, but there can be instances, then there can be such an animal, it is an animal, but it is not a specific animal (neither a cat nor a dog ). Obviously, such semantics is far from the pure virtual base class.

What about virtual inheritance? The member of the class that is inherited by virtual objects will be indirectly operated. This is its "A means", that is, to operate the member of the class that is inherited by virtual objects, different memory operations may be performed due to different offset values. However, the modification to the virtual table is only limited to modifying it to indirectly operate the same instance if it appears repeatedly, therefore, the root cause of virtual inheritance is to solve the two hunger problems of whales mentioned in the previous article, and its significance is just the implementation of an algorithm. This leads to the inability to determine whether to inherit the parent class from virtual machines when designing marine creatures and breast animals. It depends on whether the derived classes are similar to whales, this is not a good phenomenon.

Static)

As mentioned in "C ++ from scratch (v)", static means that every operation has not changed, while dynamic means that every operation may change. C ++ provides the static mark, which is the same as public and virtual above. It is just a syntax identifier, not a type modifier. It can act before a member to indicate that the member remains unchanged for each instance, as shown below:

Struct a {static long a; long B; static void ABC () ;}; long A:;

Void A: ABC () {A = 10; B = 0 ;}; void main () {A; A. A = 10; A. B = 32 ;}

The above a: A is the static member variable of structure a, and a: ABC is the static member function of. What are the changes? The type of the above ing Element A: A will not be long A: But long. Similarly, the: ABC type is changed to void () instead of void (::)().

First, the members must be static for their class instances, that is, the member variables are the same for the memory address identified by each instance, the memory address modified by the member function for each this parameter remains unchanged. The above changes a: And a: ABC to the normal type, instead of the Offset type, they eliminate their dependencies on the instance A, and then implement the above static.

2 3> full text reading

I still want to go to the original website, but there is no time recently. Remember first ~

 

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.