A little understanding of the virtual function in C ++, pure virtual functions in polymorphism

zhaozj2021-02-17  67

Title: A little awareness of C in the virtual function, pure virtual function in polymorphism (original name) I have a little simple to know the virtual function in the C , and the pure virtual function is a bit shallow understanding of MAHONGXI (original) polymorphism. One of the main differences of object programming and process programming, what is the polymorphism? 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 payment () {} // .......}; We know that middle school students pay the fee and the payment of college students are different, so although we have inherited middle school students in college students. "Tuition fee" operation, but we don't have to overload it, define a college student's own payment, so when we define a primary school student, a college student: student a; acadstudent B; a. Pay tuition fee (); Pupils, b. Pay tuition fees (); it is called college students, but you have to realize that it may be, it may not only these two, maybe n, primary school students, high school students, postgraduate ... They can all 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 fee (); break; default: ...........}} First, we have to define a Typeof in each class () Used to identify its type, then the difference is to be distinguished in the program, so that although it is, it is also possible to change the Switch, and then the old way to the process, and want to pass a module It is also difficult to perform operation. 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. In this way: VOID is universal payment fee (student & a) {a. Take a tuition fee ();} I will get it all, you are not afraid of adding new types! ! ! [Specific Realization Principle Reference: "Inside THE C Object Model"].

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

New Post(0)