In many of my letters, I have seen some interesting phenomena and some ideas worth organizing. Therefore, based on my personal past studies, my teaching experience, and the experiences of friends around me, I can write this article or give it a warning for later scholars.
● Preface to the multi-type and virtual series: multi-type and virtual series (Hou Junjie/songgang/1998/07, c ++ is a difficult language to learn and use. C ++ is hard to learn. In the beginning, the compiler has made too many operations on our code, we are used to thinking in sequence, and our engineering head is at a loss. And long, and facing new ways of thinking, we must turn around our habits of thinking. The ease of use of C ++ is due to its huge elasticity. It can be used in various types such as polymorphism, virtual, template, and generic, allow existing codes to process unknown and future data types. Of course, to be easy to use, you must first be able to use it. If it is not easy to use or unavailable, "Writing C ++ programs" is just "using C ++ compiler", which is a joke that everyone uses to ridicule each other. In the context of "difficult to learn", "Easy to use" is the driving force behind us. More and more university information departments have offered C ++ courses in their freshman year. Although C ++ is so important, it has also suffered from new information personnel. In fact, the biggest crux of "hard to learn" is that it is rare for a book to point out the importance of multi-type and virtual. After we have a rough syntax basis, directly direct us to the most important and core idea, and provide sufficient necessary foundation in the process of establishing this idea.
● C ++ is a difficult language to learn and use. The difficulty of C ++ learning lies in the fact that there are too many scenes in the language itself, and in paradigm shift (the transfer of thinking models ). In traditional sequential languages such as C, Pascal, basic, and FORTRAN..., except for seemingly slightly different languages, basically all calls are called by function, which is similar and easy to grasp. The actions you Want to do are clearly visible in the code. What you don't see is that the compiler adds a small segment of code (prologue and epilogue) for processing stacks to your function. Basically, this small segment of code is used for housekeeping, it doesn't matter if you don't see it. It doesn't affect your thinking about program logic. C ++ is different. c ++ has too many actions that are closely related to the program logic, and the compiler adds them to us. In other words, the C ++ compiler "encodes" us 」. If you do not know this section, learning C ++ is like watching flowers in fog, fog is not fog, and flowers are not flowers. What code does the compiler add to our c ++ program? A lot! When an object is born, the ctor operator is aroused, and the dtor is aroused when the object is dead. This is the result of code addition. Set vtpr and vtbl in ctor, which is also the result of Code adding. A memory block cookie is generated when a new object is created. When a new object array is created, an internal structure record object size and class ctor... are generated, which are also post-curtain work. It can be said that all the actions that cannot be seen in the Code but must be completed related to the program logic are all the results of the code added by the C ++ compiler. When "inheritance" occurs, the entire situation becomes a little complicated. "Multi-inheritance" is more complex, and "virtual inheritance" is more complex. The Theme behind the curtain can be categorized as the so-called C ++ Object Model (Object Model ). If you do not know these underlying mechanisms, you can just put "make Destructors virtual in base classes" (<valid tive C ++>, item14) or "never treat arrays polymorphically" (<more effective C ++>, item 3. Lin Yutang said: "Not brilliant 』. He only knows how and does not know why. Hou Jie said, "Not brilliant 』.
● Difficulty 2: The second learning difficulty of C ++ lies in "paradigm shift" (Transfer of thinking mode ). Do not design your own classes. Simply using others' classes is a transfer of thinking and behavior patterns. MFC (or owl or VCL) programmer can understand and understand what I mean. What is the relationship between your code and the framework using the so-called Application Framework (a large-scale, cohesive class library with object-oriented public infrastructure? What are the meanings and values of a bunch of modifiable virtual functions provided by framework? Why can the various beautiful properties and algorithms designed by the framework be applied to our own class types? When the framework is designed, we do not know its existence! This is the power of polymorphism in object orientation. The C ++ object model mentioned earlier belongs to the lower layer of programming. The thought mode transfer mentioned here is the high-level aspect of programming. We can give full play to the power of new thinking models, such as object-oriented polymorphism (multi-type) and generalization (generic ). If you do not use these two features, you can leave them blank when entering C ++ Baoshan.
● Reverse refining, cyclic shock. Imagine C ++ is a tool used to solve program problems. If it is to be strong and sharp, it must be tempered several times, refined between hot and cold. After learning the C ++ syntax (syntax), you should try to use polymorphism as soon as possible (roughly the use of virtual functions ). When we have the ability to take control of the overall situation of OOP, but we are not very clear about many small details of C ++, that is, the time to repeat the C ++ object model. Growth is a reverse shock between the higher order (polymorphism) and the lower order (Object Model) to be able to shock to a higher level, rather than being mediocre to the middle order (C ++ syntax) A beach of dead water.
● Don't sink into C ++ syntax100. I told him that he understands C ++/Oop, but less than 10% can make me think that he is not arrogant. Too many people cannot access polymorphism and object model. In this way, it is stuck in the C ++ syntax. I learned C ++ in my freshman year and graduated from my senior year. I can't say anything about virtual functions. Sometimes I think it's hard for me to be so harsh, because the students are actually in an ignorant state and do not know how to learn C ++/oop, I don't know which books can teach them that way. Therefore, it is better to blame the teacher for being harsh. As we all know, the university professor taobao.com is not a start-up employee. The general mentality is "the first in the thesis, the first in the descending order; the programming language? Ah, the last stream !」. The "Last stream" course is usually taught by professors in turn, Who is the best person to teach; so there is often "C ++ language to be taught next semester, this semester is cold (hot) it's time to get a book to make up for it. It's just a matter of programming language. It's just a matter of mouth, you must do it, and you must always do it. What can students learn if the teacher does not understand the spirit of C ++/oop? In some schools, the Information Department does not teach specific programming languages. The attitude of teachers is "language is a good thing for you to learn. If you get to the University Hall, you are not going to enter the school 」! Therefore, we should lay a good foundation for students, but they will be able to talk about abstract ideas. Teachers who have read well may ignore this. A student who has no technical foundation needs not a metaphysical means, but an easy-to-use tool. Can we first appreciate concrete paintings or abstract paintings? We don't always yell at Bica-suo's painting "What is this 」, it was not until I had enough artistic resources, I was full of reading and practicing my life, I was able to take a look at my work, and I was able to access the context of my work, in turn, can we appreciate or even enter the abstract Artistic Conception of Bijie? Teachers have their own expertise. I think it is too difficult for them to teach Big class and basic courses that are not dedicated to others. In this case, it is better to blame the school authorities for being harsh on teachers. If the school authorities can hire experienced and enthusiastic engineers to teach such practical subjects, isn't it a pleasure for all three parties? Don't say that the system is rigid and hard to break through. The University is a highly autonomous region. Do you have to hire several full-time teachers? Are they all within the scope of responsibility? When students fail to learn what they want in their courses, they have to leave their own lessons closed. However, the sequential language still has the possibility of self-learning. The object-oriented language is difficult for college students, only a half bottle of water is made. Cannot manage school! No professor is in charge! I hope you will see this article and learn how to learn C ++/oop.
● Do not indulge in C ++ object model friends who are interested in the underlying knowledge and explore the object model field, we will be very happy to have a lot of fun playing between object size, object layout, vptr/vtbl, and many off-screen technologies. It is certainly good to understand these things, but it is the pleasure and sense of accomplishment of exploring the mysteries of these things that makes some friends "play" at this level and play well in small places, the ultimate goal of C ++/OOP is ignored. The ultimate goal is polymorphism! I would like to say that in C ++ syntax and lower-level C ++ semantics, do not go too far. It will hurt me. Some exercises attached to the typical C ++ book seem to me a little overdo. In addition to basic skills in practice, the selection of hundreds of questions and questions are boring. Programming should be a combination of imagination and creativity, for example, isn't it interesting to implement a class system for the operation of a celestial body or a biological classification system (subject species and species? When preparing the materials, check the encyclopedia, and you also find the geometric data of the nine planets in the solar system, the orbital cycle of Halley huixing, or the English name of the "program category" of the black-faced Heron, isn't it any more interesting than studying the issues of ++ I or ---- I or * & P or other mental gymnastics questions? (I have read many of these funny questions, but I have to write a few computational formulas. You should know what kind of mind gymnastics I'm talking about) Of course, in the field of science and engineering, there is no technology to stand for, but don't make yourself rigid, it is so clever. Rigidity and skill are the greatest sinks of our education system. At the higher level, the failure was revealed.