首页 > C_C++_notes

C_C++_notes

来源:网络收集  

C and C++ in 5 days Philip Machanick School of ITEE University of Queensland St Lucia, Qld 4072 Australia philip@itee.uq.edu.au copyright © Philip Machanick 1994, 2003

c o n t e n t s Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Part 1—Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 file structure ....................................................................................................2 simple program ............................................................................................2 a few details ..................................................................................................3 hands-on—enter the program on page 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 part 2—Language Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 functions .........................................................................................................5 typ e s .............................................................................................................6 statements.....................................................................................................7 hands-on—a larger program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 part 3—Style and Idioms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 0 switch .............................................................................................................1 0 loops..............................................................................................................1 0 arguments......................................................................................................1 0 pointers and returning values.......................................................................1 1 arrays, pointer arithmetic and array arguments ...........................................1 1 hands-on—sorting strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 3 part 4—Structured Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 4 struct ...............................................................................................................1 4 typedef..........................................................................................................1 4 ‘putting it together: array of struct’ ................................................................1 5 hands-on—sorting employee records. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 7 part 5—Advanced Topics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 8 preprocessor ................................................................................................1 8 function pointers ............................................................................................1 9 traps and pitfalls ............................................................................................2 0 hands-on—generalizing a sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1 part 6—Programming in the Large . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 file structure revisited ....................................................................................2 2 maintainability ................................................................................................2 2 portability.......................................................................................................2 2 hiding the risky parts .....................................................................................2 3 performance vs. maintainability ...................................................................2 3 hands-on—porting a program from UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 5 part 7—Object-Oriented Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 6 identifying objects.........................................................................................2 6 object relationships.......................................................................................2 6 entities vs. actions.........................................................................................2 7 ‘example: event-driven program’ ...............................................................2 7 design task—simple event-driven user interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 8 part 8—OOD and C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 9 language elements .......................................................................................2 9 example ........................................................................................................2 9 hands-on—implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 part 9—Object-Oriented Design and C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3 OOD summary .............................................................................................3 3 objects in C++..............................................................................................3 3 stream I/O......................................................................................................3 4 differences from C ........................................................................................3 5 hands-on—simple example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 7 part 10—Classes in More Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 8 constructors and destructors.........................................................................3 8 inheritance and virtual functions ....................................................................3 9 information hiding ..........................................................................................3 9 static members .............................................................................................3 9

hands-on—adding to a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1 part 11—style and idioms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2 access functions ............................................................................................4 2 protected vs. private....................................................................................4 2 usage of constructors....................................................................................4 2 hands-on—implementing a simple design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 4 part 12—Advanced Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 5 mixing C and C++ .......................................................................................4 5 overloading operators..................................................................................4 5 memory management .................................................................................4 6 multiple inheritance........................................................................................4 7 cloning ............................................................................................................4 8 hands-on—3-D array class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 9 part 13—Design Trade-Offs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 0 case study—vector class .............................................................................5 0 defining operators vs. functions...................................................................5 0 when to inline.................................................................................................5 0 the temporary problem ...............................................................................5 1 hands-on—vector class using operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 part 14—More Advanced Features and Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3 templates ......................................................................................................5 3 exceptions ....................................................................................................5 4 virtual base classes ......................................................................................5 4 ‘future feature: name spaces’ .......................................................................5 4 libraries vs. frameworks................................................................................5 5 i n d e x ....................................................................................... 56

Preface C was developed in the 1970s to solve the problem of implementing the UNIX operating system in a maintainable way. An unexpected consequence was that UNIX also became relatively portable. Consequently, some think of UNIX the first computer virus, but this is erroneous. There are major technical differences between UNIX and a virus. C was designed a time when computer memories were small, especially on the lowend computers for which UNIX was originally designed. At the same time, compilerwriting techniques were not as well developed as they are today. Most of the code optimization technology of the time was oriented towards making FORTRAN floatingpoint programs as fast as possible, and tricks used in modern compilers to index must be computed—to give two examples—were still to be developed. As a consequence, to make C viable for operating system development, the language has many features which are unsafe, and with today’s compiler on technology, unnecessary. Even so, only the best compilers, typically found U N I X systems, keep registers in variables as long as possible, and to minimize the number of times and array implement really good code generation, and typical PC compilers are not as good. Part of the reason is to be found in the instruction set of the Intel 80x86 processor line, which has very few general-purpose registers, and a large range of equivalent instructions to choose from in some circumstances. These notes introduce C with a modern style of programming, emphasizing avoidance of the most risky features, while explaining where their use may still be appropriate. The intended audience is experienced programmers who may be used to a more structured language, such as Pascal, Ada or Modula–2; differences from such languages are noted where appropriate or useful. As a bridge to C++, object-oriented design is introduced with C as a vehicle. This illustrates how object-oriented design is medium for an object-oriented design. The notes are divided into 14 parts, each of which is followed by a hands-on or discussion session. The first half is about C, concluding with object-oriented design and how it relates to C. C++ designs. Since this information was originally compiled in 1994, some details may be dated. Watch for new versions, as errors and omissions are corrected. The notes are intended to supply enough material to absorb in a week; some sources of further information include: Brian W Kernighan and Dennis M Richie. The C Programming Language (2nd edition), Prentice-Hall, Englewood Cliffs, NJ, 1988. ISBN 0–13–110362–8. Margaret A Ellis and Bjarne Stroustrup. The Annotated C++ Reference Manual, Addison-Wesley, Reading, MA, 1990. ISBN 0–201–51459–1. Stanley B Lippman. C++ Primer (2nd edition), Addison-Wesley, Reading, MA, 1989. ISBN 0–201–17928–8. Grady Booch. Object-Oriented Design with Applications, Addison-Wesley, Reading, MA, 1991. ISBN 0–201–56527–7. a separate concept from object-oriented languages—even though an object-oriented language is clearly the better implementation is introduced as a better way of implementing object-oriented acknowledgement András Salamon proof read this document and suggested some clarifications.

Part 1—Overview file structure C source files are organized as compilable files and headers. A header file contains declarations; a compilable file imports these declarations, and contains definitions. A definition tells the compiler what code to generate, or to allocate storage for a variable whereas a declaration merely tells the compiler the type associated with a name. Headers are generally used for publishing the interface of separately compiled files. In UNIX it’s usual to end compilable file names with “.c” and headers with “.h”. A compilable file imports a header by a line such as (usually for system headers): #include or (usually for your own headers): #include "employees.h" <> The difference between the use of contents of the named file. and "" will be explained later. line had been replaced by the When the header is imported, it’s as if the #include caution—in C, this is only a convention—but one that should not be broken: the header could contain anything, but it should only contain declarations and comments if your code is to be maintainable simple program Only a few more points are needed to write a program, so here’s an example: #include void { argc, char *argv[]) main(int i; (i=0; i int for < argc; i++) argument [%d] = %s\n", i, argv[i]); printf("command } line The first line imports a system header, for standard input and output. The second line is the standard way of declaring a main program. A main program can return a result of type int, though this one doesn’t actually return a value, hence the void. The main program has two arguments, the first of which is a count of command-line arguments from the command that started the program. The second is a pointer to an array of strings each of which is a separate command-line argument. By convention, the first string is the name of the program. Note the syntax: a “ * ” is used to declare a pointer, and an empty pair of square brackets is used to denote a variable-sized array. The next thing to notice is the use of curly brackets for a begin-end block. The main program declares a variable i, and uses it as a loop control variable. < Note the convention of counting from zero, and using a The for test to terminate the loop. This convention is useful to adhere to because C arrays are indexed from zero. loop control actions must be in parentheses, and the initialization, test and {} increment are separated by semicolons. The body of the loop is a single statement in this case, so no statements into the body. The body of the loop uses library function printf() %d are needed to group to produce output. The first causes the final argument to string is used to format the arguments that follow. The in the format string causes the next argument to be printed as a decimal integer, and the %s be printed as a string. A “\n” terminates a line of output. what does the program do? No prizes for the answer.

a few details In C, there is no distinction between functions and procedures. There is a distinction between statements and expressions, but it is usually possible to use an expression as a statement. For example, the following is legal C code (note use of comments): void { main(int i; /* this is an expression */ argc, char *argv[]) /* … */ for int i+1; } This is a silly example, but there are cases where the result of an expression is not needed, just its side effect (i.e., what it changes in the global environment). Functions that do not return a result are declared to return type things. A few examples of expressions that might be languages: • assignment—done with “=” in C—so for example, it’s possible to do a string of initializations in one go (comparison for equality uses “==”): int i = i,j; j = 0; /* j = 0 is an expression: returns new value of j */ void, which is a general non-type also used to specify pointers that can’t be dereferenced, among other considered statements in other • procedure call—always a function call in C, even if no value is returned (on the other hand a value-returning function can be called as if it were a procedure call, in which case the value is thrown away) • increment (var_name++) and decrement (var_name--): the exact behaviour of these constructs is too complex for an introduction; they are explained more fully later Unlike some languages (e.g., LISP, Algol-68) that treat everything as expressions, most other statements in C cannot be used to return a value. These include selection ( i f and switch), loops (while, for and do…while—the latter like a Pascal repeat) and {} blocks. 3

您可能感兴趣。。。
All Rights Reserved 摩尔人
所有数据来自网络,数据只供学习参考 点击这里给我发消息