C ++

来源:互联网 发布:手机怎么不能开淘宝店 编辑:程序博客网 时间:2024/06/05 06:37

main

The Operating system uses the value returned by main method to determine whether the program succeeded or failed. A return value of 0 indicates success.

#include

This line is a preprocessor directive. Tell compiler that we need to use the iostream library. The name inside angle brackets is a header. Every program that uses a library facility must include associated header. The directive #include must be written on a single line, the name of the header file and the #include must appear on the same line. In general, #include directives should appear outside any function. Typically, all the #include directives for a program appear at the beginning of the file.

<<

Output operator takes two operands: the left-hand operand must be a ostream object; the right-hand operand is a to print. The operator writes its right-hand operand to the ostream that is its left-hand operand.

endl

enld is a special value, called a manipulator, that when written to an output stream has the effect of writing a newline to the output and flushing the buffer associated with that device. By flushing the buffer, we ensure the user will see the output written to the stream immediately.

using names from the standard library.

std::cout << "Enter two numbers:";
std::cout << std::endl;

The prefix std:: indicates that the names cout and endl are defined inside the namespace named std. Namespaces allows programmers to avoid inadvertent collisions with the same names defined by a library. Because the names that the standard library defines are defined in a namespace, we can use the same names for our own purpose.

One side effect of the library’s use of a namespace is that when we use a name from the library, we must say explicitly that we want to use the name from the std namespace. writing std::cout uses the scope operator(the :: operator) to say that we want to use the name cout that is defined in the namespace std. We will see a way later that programs often use to avoid this verbose syntax.

>>

std::cin >> v1 >> v2;

reads the input. The input operator (the >> operator) behaves analogously to the output operator. It takes an istream as its left-hand operand and an object an its right-hand operand. It reads from its istream operand and stores the value it reads in its right-hand operand. Like the output operator, the input operator returens its left-hand operand as its result. Because the operator returns its left-hand operand, we can combine a sequence of input requests into a single statement. In other word, this input operation is equivalent to

std::cin >> v1;
std::cin >> v2;

<> and ""

#include
#include "Sales_item.h"

Headers for the standard library are enclosed in angle brackets(<>). Nonstandard headers are enclosed in double quotes("").

Variable initialization

A definition specifies a variable's type and identifier. A definition may provides an initial value for the object. C++ supports two forms of variable initialization: copy-initialization and direct-initialization. copy-initialization syntax uses the equal (=) symbol; direct-initialization places the initializer in parentheses:

int ival(1024); // direct-initialization
int ival = 1024; // copy-initialization

many new C++ programmers are confused by the use of the = symbol to initialize a variable. It is tempting to think of initialization as a for of assignment. But initialization and assignment are different operations in C++. This concept is particularly confusing because in many other languages the distinction is irrelevant and and can be ignored. Moreover, even in C++ the distinction rarely matters until one attempts to write fairly complex classes. Nonetheless, it is a crucial concept and one that we will reiterate throughout the text.

There are subtle differences between copy- and direct-initialization when initializing objects of class type. For now, it's worth knowing that the direct syntax is more flexible and can be slightly more efficient.

Declarations and definitions

A definition of a variable allocates storage for the variable and may also specify an initial value for the variable. There must be one and only one definition of a variable in a program.

A Declaration makes known type and name of the variable to the program. A definition is also a declaration: when we define a variable, we declare its name and type. We can declare a name without defining it by using extern keyword. A declaration is not also a definition consists of the object's name and its type preceded by the keyword extern:

extern int i; // declares but does not define i
int i; // declares and defines i

An extern declaration is not a definition and does not allocates storage. In effect, it claims that a definition of the variable exists elsewhere in the program. A variable can be declared multiple times in a program, but it must be defined only once.

An declaration may have an initializer only if it also a definition because only a definition allocates storage. The initializer must have storage to initialize. If an initializer is present, the declaration is treated as a definition even if the the declaration is labeled extern:

extern double pi = 3.1416; // definition

Despite the use of extern, this statement defines pi. Storage is allocated and initialized. An extern declaration may include an initializer only if it appears outside a function.

Because an extern that is initialized is treated as a definition, any subsequent definition of that variable is an error:

extern double pi = 3.1416; // definition
double pi; // error: redefinition of pi

Non const variables are extern by default. To make a const variable accessible to other files we must explicitly specify that it is extern.

原创粉丝点击