I am a little simple to understand the virtual function in C ++, pure virtual functions in achieving polymorphism

zhaozj2021-02-11  152

Polymorphism is one of the main differences for object-oriented programming and process programming, what is polymorphic? I remember that there is a one in the article in the C polymorphic article in 9CBS: "Dragon Si Jiuzi, the dividence" polymorphism is the same processing means can be used to handle a variety of different situations, in the teacher's teacher " There is an example in the C programming tutorial: define a primary school class [all codebooks are pseudo code] Class Student {public: student () {} ~ student () {} VoID payment () {} / / ...}; there is a "Tuition fee" handler, because college students and primary school students are similar, we have gave birth to college students from primary school students: Class Acadstudent: public student {public: acadstudent () { } ~ Acadstudent () {} void delivery fee () {}

// .......};

We know that middle school students pay the fee and the payment of college students are different, so although we inherited the "payment fee" operation of middle school students in college students, we don't have to overload it, define our own school's own payment, so We defined a primary school student, a college student:

Student a; acadstudent b;

A. Talent fees (); that is, calling primary school, b. Pay tuition fees (); it is called college students, but the function is realized, but you have to realize that may not only these two, maybe N, primary school, junior high school Born, high school students, postgraduate ..... they can be based on Student [primary school class].

If the system requires you in a group of students, just pick a tuition fee, how do you do it?

: // A for the tuition of tuition {Switch (a)) {Case Pupil: a. Primary School :: Traffic Talent (); Break; Case Junior Middle School: a. Beginning :: Traffic Tips Break; Case high school students: a. High student :: pay tuition fees (); Break;

Default: .............}}

First, we must define a typeOf () to identify its type in each class, and then distinguish it in the program, so that although it is, it is also possible to change the Switch, and I have a process-oriented old road, and I want to do it through a module to achieve difficulty. So the polymorphism is provided in C , which can be dynamically distinguished by the technique after the later connection. Add a Virtual to tell the system before the base class is used to tell the system. This way you can:

VOID general delivery fee (student & a) {a. pay tuition fee ();

I don't get it all, I am not afraid of adding new types! ! ! [Specific Realization Principle Reference: "Inside THE C Object Model"]. If there is no Virtual statement, then the system determines the operation before execution, such as transmitting "college students" to Void general delivery fee (student & a) {a. Pay tuition fee ();}, A. pay tuition fee (); Calling is that the implementation of the virtual function on the "payment fee" in the college class is inherited in the Student class. Why do you have a pure virtual function?

If you are programmed by the above example, all types have inherited primary school classes, we will find that this primary school students' own specific stuff [such as the Articles ();], they have been inherited by college students, although not affecting college students Operation, but with time, the primary school class is more and more, so that the data in college students is more, what is the way to solve? ? ? ?

It is to define an abstract class when defining the base class, such as student classes, and implementing this every family in the student class. This process is called decomposition. This class is not obvious about the description of the pure virtual function, for example, for example:

Class people () {public: // ... VoID eat () {people eat;} // ...... char * name; int Age;};

Class dog () {public: // ...... Void eat () {Dog eat;} // ...... char * name; int Age;};

Human, dog classes have some of the same stuff, such as name, age, eating, etc., some people think of reuse for code, let humans inherit dog class, but the redundancy of data makes this idea completed, so some people want You can define a class:

This class is bound to the human dog class, there are some stuffs with people and dogs, such as age, name, weight, but it does not exist, its existence is just to derive other classes, such as Human, dog classes, this can make the system clear,. . . . . , There are many advantages.

In this world, there is no found in Dongdong, so "Eating" in this "Between Termages" is not significant, you are also difficult to define it for its content, and there is no need. So define it for a pure virtual function, form: Virtual Void eat () = 0; used to tell the system: 1, this function can have no function definition; 2, the class with this function is an abstract class; you may say, The simple virtual function does not make sense, why should it be, what is its role? Preparation for realizing polymorphism!!!

Due to the example of abstract classes, it is not allowed to define an instance of it in C . (If you define this example A, then you call A. What should I do?)

Of course, you can also eat () {} in the base class, the base class is not an abstract class, which can have an example, and it does not affect other aspects.

But you have to admit that such objects are not conscious, it can only make you think about the burden, in the wrong time, do you think that there is such an object in the monster, so C simply provides "virtual functions ", Abstract class, mechanism, add restrictions to us, can also think that insurance [can have an abstract class object], if the code becomes very large, it is necessary! ! ! The younger brother is a little bit, you are laughing.

Yes: msn: mahongxi@hotmail.com Everyone discusses.

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

New Post(0)