設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 655|回復: 0
打印 上一主題 下一主題

Chapter 1. Getting Started<C++ Primer, Fifth Edition>

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2016-1-15 09:41:41 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
Chapter 1. Getting Started
Contents
Section 1.1 Writing a Simple C++ Program
Every C++ program contains one or more functions , one of which must be named main.
The operating system runs a C++ program by calling main.
Here is a simple version of main that does nothing but return a value to the operating system:
int main()
{
            return 0;
}
A function definition consists of a declaration, plus the body, which is all the code between the curly braces.
A minimal function declaration consists of the return type, function name, and parameter list (which may be empty).
A function definition has four elements: a return type , a function name , a (possibly empty) parameter list enclosed in parentheses, and a function body .
main (function name) has an empty list of parameters (shown by the () with nothing inside) and have a return type of int
The final part of a function definition, the function body, is a block of statements starting with an open curly brace and ending with a close curly:
{   
     return 0;
}
The only statement in this block is a return, which is a statement that terminates a function.
Note the semicolon(; ) at the end of the return statement. Semicolons mark the end of most statements in C++.
On most systems, the value returned from main is a status indicator.

A return value of 0 indicates success. A nonzero return has a meaning that is defined by the system.
Ordinarily a nonzero return indicates what kind of error occurred.

在命令提示字元處,輸入 cl /clr xyz.cpp。
cl.exe 編譯器會將原始程式碼編譯為包含 MSIL 的 .obj 檔案,然後執行連結器,以產生名為 xyz.exe 的可執行程式
要執行 xyz.exe 程式,請在命令提示字元下,輸入 xyz
ps.cl.exe is a tool that controls the Microsoft C and C++ compilers and linker. cl.exe can be run only on operating systems that support Microsoft Visual Studio.

Section 1.2 A First Look at Input/Output
We use the iostream library. Fundamental to the iostream library are two types named istream and ostream, which represent input and output streams, respectively. A stream is a sequence of characters read from or written to an IO device. The term stream is intended to suggest that the characters are generated, or consumed, sequentially over time. Standard Input and Output Objects
The library defines four IO objects.
To handle input, we use an object of type istream named cin (pronounced see-in ). This object is also referred to as the standard input.
For output, we use an ostream object named cout (pronounced see-out ). This object is also known as the standard output.
The library also defines two other ostream objects, named cerr and clog (pronounced see-err and see-log , respectively).
We typically use cerr, referred to as the standard error, for warning and error messages and clog for general information about the execution of the program. when we read from cin, data are read from the window in which the program is executing, and when we write to cout, cerr, or clog, the output is written to the same window.
  1. #include <iostream>
  2. int main()
  3. {
  4.      std::cout << "Enter two numbers:" << std::endl;
  5.      int v1 = 0, v2 = 0
  6.      std::cin >> v1 >> v2;
  7.      std::cout << "The sum of " << v1 << " and " << v2
  8.                   << " is " << v1 + v2 << std::endl
  9.      return 0;
  10. }
複製代碼
Enter two numbers:
3 7 <followed by a newline>
The sum of 3 and 7 is 10
The first line of our program #include <iostream> tells the compiler that we want to use the iostream library.
The name inside angle brackets (iostream in this case) refers to a header. Every program that uses a library facility must include its associated header.
The #include directive must be written on a single line—the name of the header and the #include must appear on the same line.
In general, #include directives must appear outside any function. Typically, we put all the #include directives for a program at the beginning of the source file.


The first statement in the body of main executes an expression.
In C++ an expression yields a result and is composed of one or more operands and (usually) an operator.
The expressions in this statement use the output operator (the « operator) to print a message on the standard output:
std::cout << "Enter two numbers:" << std::endl;
The << operator takes two operands: [insertion (<<)
The left-hand operand must be an ostream object; the right-hand operand is a value to print.
The operator writes the given value on the given ostream.
The result of the output operator is its left-hand operand.
That is, the result is the ostream on which we wrote the given value. Our output statement uses the << operator twice.
Because the operator returns its left-hand operand, the result of the first operator becomes the left-hand operand of the second.
As a result, we can chain together output requests. Thus, our expression is equivalent to
(std::cout << "Enter two numbers:") << std::endl;
Each operator in the chain has the same object as its left-hand operand, in this case std::cout.
Alternatively, we can generate the same output using two statements:
std::cout << "Enter two numbers:";
std::cout << std::endl;
The first output operator prints a message to the user. That message is a string literal, which is a sequence of characters enclosed in double quotation marks.
The text between the quotation marks is printed to the standard output.
The second operator prints endl, which is a special value called a manipulator. <Insert newline ('\n') and flush>
Writing endl has the effect of ending the current line and flushing the buffer associated with that device.
Flushing the buffer ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.
ps. endl manipulator(格式操作算子), it is designed to be used alone with no arguments in conjunction with the insertion (<<) operations on output streams.

Section 1.3 A Word about Comments
Kinds of Comments in C++
There are two kinds of comments in C++: single-line and paired.

A single-line comment starts with a double slash (//) and ends with a newline.
Everything to the right of the slashes on the current line is ignored by the compiler.
A comment of this kind can contain any text, including additional double slashes.
The other kind of comment uses two delimiters (/* and */) that are inherited from C.
Such comments begin with a /* and end with the next */.
These comments can include anything that is not a */, including newlines.
The compiler treats everything that falls between the /* and */ as part of the comment.
A comment pair can be placed anywhere a tab, space, or newline is permitted.
Comment pairs can span multiple lines of a program but are not required to do so.
When a comment pair does span multiple lines, it is often a good idea to indicate visually that the inner lines are part of a multiline comment.
Our style is to begin each line in the comment with an asterisk, thus indicating that the entire range is part of a multiline comment.
Programs typically contain a mixture of both comment forms.
Comment pairs generally are used for multiline explanations, whereas double-slash comments tend to be used for half-line and single-line remarks:

  1. #include <iostream>
  2. /*
  3. * Simple main function:
  4. * Read two numbers and write their sum
  5. */
  6. int main()
  7. {
  8. // prompt user to enter two numbers
  9.     std::cout << "Enter two numbers:" << std::endl;
  10.     int v1 = 0, v2 = 0; // variables to hold the input we read
  11.     std::cin >> v1 >> v2; // read input  
  12.     std::cout << "The sum of " << v1 << " and " << v2
  13.                  << " is " << v1 + v2 << std::endl;
  14.     return 0;
  15. }
複製代碼




Section 1.4 Flow of Control
Selection statements: if and else
if (condition) statement
Iteration statements (loops):
The while loop:while (expression) statement

The do-while loop:do statement while (condition);
The for loop:for (initialization; condition; increase) statement;
Range-based for loop:for ( declaration : range ) statement;
Jump statements:
The break statement
The continue statement
The goto statement


Another selection statement: switch.
switch (expression)
{
  case constant1:
     group-of-statements-1;
     break;
  case constant2:
     group-of-statements-2;
     break;
  .
  .
  .
  default:
     default-group-of-statements
}




Section 1.5 Introducing Classes
Section 1.6 The Bookstore Program




Chapter Summary
Defined Terms






分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-5-16 19:18 , Processed in 0.287130 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表