1 / 36

C ++ 程序设计

C ++ 程序设计. Chapter 2 Making and Using Objects. Outline of the chapter. Process of building programs using compilers and libraries. Introduce some of the basic libraries of objects in standard C++. 02/24. The process of language translation. There are two kinds of translation:

latham
Download Presentation

C ++ 程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++程序设计 Chapter 2 Making and Using Objects

  2. Outline of the chapter Process of building programs using compilers and libraries. Introduce some of the basic libraries of objects in standard C++. 02/24

  3. The process of language translation There are two kinds of translation: Interpreter(解释器) Compiler(编译器) Interpreter Translate the source code and then execute. Writing code to executing code immediately. Size limitation, speed restrictions. 03/24

  4. The process of language translation Compiler Translate source code to machine code file. Require less memory to run and quick, separate compilation( linker), source-level debugger, large programs, etc. 03/24

  5. The compilation process As a C and C++ programmer ,you should understand the steps and tools in the compilation process. Preprocessor : change the source code to another pattern which is more readability( translation unit). The First Pass : Parses the pre-processed code to create the tree. Global optimizer : to produce smaller, faster code. 03/24

  6. The compilation process The Second Pass : Code generator walks through the parse tree and generates either assembly code or machine code for the nodes of the tree, the end result is an object module (.o or .obj ). Linker : combines a list of object modules into an executable program(启动程序模块). Static type checking : type checking occurs during compilation instead of when the program is running it is called static type checking. (C++ use it.) Dynamic type checking : Perform type checking at runtime. (Java use it) 03/24

  7. C++编译器推荐目录 免费的 Apple C++ Bloodshed Dev-C++ Borland C++ Cygwin (GNU C++) MINGW(Minimalist GNU for Windows) DJ Delorie's C++ development system for DOS/Windows (GNU C++) GNU CC source Intel C++ for linux 03/24

  8. C++编译器推荐目录 收费的 Borland C++ Comeau C++ Compaq C++ Digital Mars C++ Edison Design Group C++ Front End Green Hills C++ HP C++ IBM C++ Intel C++ Interstron C++ Metrowerks C++ Mentor Graphics/Microtec Research C++ Microsoft C++ Paradigm C++ The Portland Group C++ SGI C++ Sun C++ WindRiver's Diab C++ 来源:http://www.research.att.com/~bs/compilers.html 03/24

  9. 使用GCC编译器 参见: GCC编译器常识 http://blog.csdn.net/kastolo/archive/2006/07/21/952641.aspx 如何使用GCC编译器 http://blog.csdn.net/c43w/archive/2005/05/02/370558.aspx GCC和G++编译器参数详解 http://blog.csdn.net/chenlies/archive/2006/05/08/713479.aspx GCC和G++的区别 03/24

  10. 搭建简单的开发编译环境 使用各种IDE MinGW Developer Studio Microsoft Visual C++ Studio Eclipse IDE and CDT UltraEdit + GCC 命令行方式 下载MINGW(http://www.mingw.org/) 配置环境变量 path=C:\MinGW\bin LIBRARY_PATH=C:\MinGW\lib C_INCLUDE_PATH=C:\MinGW\include CPLUS_INCLUDE_PATH=C:\MinGW\include\c++\3.2.3; C:\MinGW\include\c++\3.2.3\mingw32; C:\MinGW\include\c++\3.2.3\backward; C:\MinGW\include g++ hello.cpp –o hello 03/24

  11. Building a C++ program ① Declarations vs. definitions Declaration A declaration introduces a name – an identifier – to the compiler. It tells the compiler “this function or this variable exists somewhere , and here is what it should look like.” Definition A definition tells compiler : “ Make this variable here ” or “ Make this function here.” It allocates storage for the name. (variable ,function) 03/24

  12. Building a C++ program ① Declarations vs. definitions You can declare a variable or a function in many different places, many times. There must be only one definition in C and C++. A definition can also be a declaration. 03/24

  13. Building a C++ program ② Function declaration syntax A function declaration gives the function name, the argument types passed to the function, and the return value of the function. int func1(int, int); if a is another integer the above function might be used this way: a = func1(2, 3); 03/24

  14. Building a C++ program ② Function declaration syntax Argument: we can declare func1( ) in a different fashion that has the same meaning: int func1(int length, int width); Argument different between C and C++ int func2(); In C : “a function with any number and type of argument.” In C++: “a function with no arguments.” 03/24

  15. Building a C++ program ③ function definitions Function definitions look like function declarations except that they have bodies. A body is a collection of statements enclosed in braces. Braces denote the beginning and ending of a block of code. int func1(int length, int width) { } Notice that in the function definition, the braces replace the semicolon. Since braces surround a statement or group of statements, you don’t need a semicolon. arguments in the function definition must have names 03/24

  16. Building a C++ program ④ Variable declaration syntax A variable declaration tells the compiler what a variable looks like.It says, “I know you haven’t seen this name before, but I promise it exists someplace, and it’s a variable of X type.” Declaring a variable using the extern keyword before a description of the variable, like this: extern int a; extern can also apply to function declarations. For func1(), it looks like this: extern int func1(int length, int width); 03/24

  17. Building a C++ program ④Variable declaration syntax In the function declarations, the argument identifiers are optional. In the definitions, they are required (the identifiers are required only in C, not C++). 03/24

  18. Building a C++ program ⑤Including headers A header file is a file containing the external declarations for a library; it conventionally has a file name extension of ‘h’ To include a header file, there are type : #include <header> #include "local.h" <> means find the *.h in include path. "“ means find the *.h in local path. 03/24

  19. Building a C++ program ⑤ Including headers Standard C++ include format:the standard uses a format that allows file names longer than the notorious eight characters and eliminates the extension. For example, instead of the old style of including iostream.h, which looks like this: #include <iostream.h> you can now write: #include <iostream> 03/24

  20. Building a C++ program ⑤ Including headers you can also use the more modern C++ include style by prepending a “c” before the name. Thus: #include <stdio.h> become #include <cstdio> #include <stdlib.h> become #include <cstdlib> The effect of the new include format is not identical to the old: using the .h gives you the older, non-template version, and omitting the .h gives you the new templatized version. You’ll usually have problems if you try to intermix the two forms in a single program. 03/24

  21. Building a C++ program ⑥Linking The linker : collects object modules into an executable program the operating system can load and run. It is the last phase of the compilation process. In general, you just tell the linker the names of the object modules and libraries you want linked together, and the name of the executable, and it goes to work. 03/24

  22. Building a C++ program ⑥Linking Using libraries Include the library’s header file. Use the functions and variables in the library. Link the library into the executable program. 03/24

  23. Building a C++ program ⑦How the linker searches a library linker searches the libraries through index to find a definition in a library , the entire object module, link it into the executable program. 03/24

  24. The first C++ program Namespace -- name resource run out. -- name conflict by individuals. a library or program is “wrapped” in a namespace namespace Mine{ int me; } reference: Mine::me; Keyword : using I want to use the declarations and/or definitions in this namespace. 03/24

  25. The first C++ program std All of the Standard C++ libraries are wrapped in a single namespace, which is std . using namespace std; This means that you want to expose all the elements from the namespace called std. the using directive makes that namespace available throughout the file where the using directive was written. #include <iostream.h> it means#include <iostream> using namespace std; 03/24

  26. The first C++ program "Hello, world!" // C02:Hello.cpp // Saying Hello with C++ #include <iostream> // Stream declarations using namespace std; int main() { cout << "Hello, World! I am " << 8 << " Today!" << endl; } ///:~ 03/24

  27. The first C++ program #include <iostream> //see using namespace std; int main() { //Specifying formats with manipulators: cout << "a number in decimal: " << dec << 15 << endl; cout << "in octal: " << oct << 15 << endl; cout << "in hex: " << hex << 15 << endl; cout << "a floating-point number: " << 3.14159 << endl; cout << "non-printing char (escape): " << char(27) << endl; } ///:~ 03/24

  28. Character array concatenation it’s possible for a statement to continue over several lines. See Concat.cpp 03/24

  29. Reading input This program converts a number typed in by the user into octal and hexadecimal representations. See Numconv.cpp 03/24

  30. Calling other programs This program shows you how easy it is to use plain C library functions in C++; just include the header file and call the function. See CallHello.cpp 03/24

  31. Introducing strings String literals A string literal is a character sequence enclosed with double quotes: “this is string.” terminated by the null character ‘\0’,with the value 0. character array disadvantage: -- fixed size in memory -- hard to manage. String -- string class is designed to take care of (and hide) all the low-level manipulations of character arrays -- easy to use . -- hidden the low-level detailed management. 03/24

  32. Introducing strings <1>C风格字符串的表示和使用方法  C风格字符串的表示有两种 char *a = “hello!”;         char b[] = “hello!”; 这两种的效果是一样的,在内存中的表示都是:a代表字符串的起始地址,而字符串以字符串结束标志‘\0’ 结束,因此实际占的位数比真实位数多一位。在使用时,由于b是const char* 类型的,所以只能使用索引b[i]来访问b中的元素,而企图改变b的指向的操作如b++是不允许的。 <2>C++标准库中的string类 string类是C++标准库中的类,它封装了很多方法,比如size()返回一个string对象的实际长度,find()实现查找匹配字符串的功能等。 03/24

  33. Introducing strings See HelloStrings.cpp You can assign to any string object using ‘=’. To combine strings you simply use the ‘+’ operator, which also allows you to combine character arrays with strings. If you want to append either a string or a character array to another string, you can use the operator ‘+=’. 03/24

  34. Reading and writing files Reading and writing files using file stream To open a file for reading, create an ifstream object To open a file for writing, create an ofstream object See Scopy.cpp See FillString.cpp 03/24

  35. Introducing vector Container : a template class to hold a objects. Vector : a kind of container to hold objects like a array. Vector operator: Define vector <type> variable; put new elements into a vector with push_back( ), use [ ] like as array See Fillvector.cpp, GetWords.cpp, Intvector.cpp 03/24

  36. Summary In the process of showing the ease of OOP when using library classes, this chapter also introduced some of the most basic and useful types in the Standard C++ library: the family of iostreams (in particular, those that read from and write to the console and files), the string class, the vector template. 03/24

More Related