Thinking C programming
In the ideal state, you complete a programming task divided into three steps. First, you must understand the problem (analysis analysis), then you want to define the key concepts in the solution (Design Design), in the end, you must express solutions in the program (programming programming). However, problems and (key) concepts in the solution can only be clearly expressed by the results of expressing and running procedures in the program, which is the trouble of selecting programming languages.
In most of the programs, some (key) concepts cannot be easily expressed through basic types or functions that do not contact data. Given such a concept, you can express it in a class in the program. A C class is a type (Type), which indicates what actions are made: how they are created, how they are operated, how they are destroyed. The class can also indicate how the object (instance of the class) is behaving, although it will not be the main part in the early stage of programming. The key to writing a good program is to design a good class, it can clearly express a separate concept (this means you often focus on the following questions: This class object ( Example) How is it created? Can an object (instance) can be copied or destroyed? What operator can be applied to this object? If there is no good answer to these questions, then a concept of Concept is not clear. Use more considerations and plan solutions instead of immediate startup coding may be a good idea.
The concept that is easier to cope with is those already have a traditional mathematical model: numbers, collection, geometry, etc. Character-oriented I / O, string, basic container, basic algorithms on these containers, and some mathematical computing classes are part of the C standard class library. In addition, a variety of rough a variety of copies of the universal and special fields are also available.
A concept of a concept is impossible to exist, it always gathers a set of connections, the connection between classes is organized, which is true, this is the independent connection between the solutions, this is often It is more difficult to set a separate class than the beginning. It is best not to make the dependency of the Depend On. Assuming two classes A and B, like "A Call B", "A Create B" and "B is a member member" member "have rarely result in serious errors. However, it is best to be excluded like "a member variable used by b".
One of the most powerful intelligent tools for managing complex is the inheritance command, that is, the concept of organizing a group of associates to a tree structure, the most general (Concept) of the General) as the root. In C , the derived class is expressed in such a structure. A program is often organized for collection or non-cycling class diagrams. It is the programmer specifying some base classes, and each class has a subclass of sending. The virtual function is often used to define the operation of the base class. When necessary, the interpretation of these operations can be redefined in subclasses.
Sometimes a direct non-cycling class is not enough to organize the concept of a program; some concepts look naturally dependent. In this case, we work hard to rely on these cycles, so that Does not affect the entire program structure. If you can't rule out or locate these inherent dependencies, you will be likely to fall into these dilemmas, and there is no programming language to help you. Unless you can imagine an easy-provoking relationship between some base contents (Easily Stated Relationships). Otherwise the program will become unmanageable.
The best tool that unlocked the dependencies is to separate the interfaces and achieve clear separation. Abstract classes are the main tools for C .
Another universal form can be expressed by a template, and a class template can specify a family (a family of) class. For example, a list (list) template specifies "List of class T", class T can be any class. Thus, the template is such a device that specifies how a type is replaced by another type of parameter. The most general template is some container class, a list of listings, arrays, a joint arrays, and the most basic algorithms used on these containers. If you use inheritance expressions, a class is used as a class or function, it is best to use a template. Remember that many programs can use the original type, data structure, simple functions, and very little class libraries, clear and simple completion. You may not define a new type unless it (defined new type) is really necessary.
Question "How to write a good C program?" And questions "How to write a good language in English?" Is very similar. There are two questions: "Do you want to say what you want" and "practice, imitate Writing", these two issues are for writing English prose, they are equally applicable to writing C programs, which is also difficult.