企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
# Chapter 1. Getting Start ## 1.1 Writing a Simple C++ Program **main function**: Every C++ program contains one or more functions, one of which must be named main. They operating system runs a C++ program by calling main. A function definition has four elements: a **return type**, a **function name**, a **parameter list** enclosed in parentheses, and a **function body**. **built-in type**: the type defined by the language. The value returned from main is a status indicator, a return value of 0 indicate success; a nonzero return has a meaning that is defined by the system. Ordinarily a nonzero return indicates what kind of error occurred. ### 1.1.1 Compiling and Executing Our Program The value returned from main is accessed in a system-dependent manner. To obtain the status by: ```bash UNIX System $ echo $? Windows System $ echo %ERRORLEVEL% ``` ## 1.2 A First Look at Input/Output **iostream library**: an extensive standard library that provide IO in C++, the istream and ostream, which represent input and output streams respectively, fundament to the iostream library. * cin: standard input; * cout: standard output; * cerr: standard error for warning and error message; * clog: for general information about the execution of the program; **expression**: an expression yields a result and is composed of one or more operands and on operator. **endl**: a special value called a manipulator, writing endl has the effect of ending the current line and flushing the buffer associated with that device. **namespace**: namespace allow users to avoid inadvertent collisions between the names that defined and used of those same names inside a library. ## 1.3 A Word about Comments **comments**: comments help the human readers of the programs, they are typically used to summarise an algorithm, identify the purpose of a variable, or clarify an otherwise obscure segment of code. There are two kinds of comments in C++: single-line and paired. A single-line comment starts with a double slash \(//\) and ens with a newline. The paired uses two delimiters \(/\* and \*/\) that are inherited from C. ## 1.4 Flow of Control ### 1.4.1 The while Statement A **while statement** repeatedly executes a section of code so long as a given condition is true. ```cpp while (condition) statement ``` A **condition** is an expression that yields a result that is either true of false. ### 1.4.2 The for Statement The pattern, using a variable in a condition and incrementing that variable in the body, is called **for statement**. ```text for(definition; condition; incremention) statement ``` ### 1.4.3 Reading an Unknown Number of Inputs An `istream` becomes invalid when it hits **end-of-file** or encounter an invalid input, such as reading a value that is not an integer. An `istream` that is in an invalid state will cause the condition to yield false. Entering an end-of-file: control-z in Windows and control-d in UNIX. ### 1.4.4 The if Statement C++ provides an **if statement** that supports conditional execution. C++ uses = for assignment and == for equality. Both operators can appear inside a condition. It is a common mistake to write = when you mean == inside a condition. ## 1.5 Introducing Classes In C++ we define our own data structures by defining a class. Every class define a type, and the type name is the same as the name of the class. **member function**: a function that is defined as part of a class.