1 / 16

El primer programa en C++

El primer programa en C++. programa en C++. // File: pgm1-1.cpp // Description: displays Hello World! // Date: 1/15/2006 #include <iostream> using namespace std; int main() { int valor; cout << "Hello world!"; return 0; }. programa en C++.

tahrens
Télécharger la présentation

El primer programa en 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. El primer programa en C++

  2. programa en C++ // File: pgm1-1.cpp // Description: displays Hello World! // Date: 1/15/2006 #include <iostream> using namespace std; int main() { int valor; cout << "Hello world!"; return 0; }

  3. programa en C++ • At a fundamental level, all classes and functions, which form the basis of a C++ executable program, consist of both words and special symbols, such as the brace pair, {}. • The words permitted in C++, as in all other programming languages, are collectively referred to as identifiers. • Identifiers consist of three types: reserved words, standard identifiers, and programmer-supplied words, each of which has its own special requirements.

  4. Reserved Words • A reserved word (or keyword) is a word that is predefined by the programming language for a special purpose and can only be used in a specified manner for its intended purpose. Any attempt to use a reserved word outside of its prescribed use will be interpreted by the compiler as a syntax error.

  5. Reserved Words Table 1-2 C++ Reserved Words

  6. Standard Identifiers • Standard identifiers are words that are predefined in C++. Standard identifiers have a predefined purpose, but this purpose can be redefined by a programmer. Unlike reserved words, standard identifiers may be redefined, but this action should only be taken under special circumstances. For example, rand () is a library function. If you were seeking to refine rand (), it would be appropriate to use the header rand () in your function definition. Table 1-3 lists sample C++ standard identifiers.

  7. Standard Identifiers Table 1-3 Sample of Standard C++ Identifiers

  8. Standard Identifiers • Generally, the term standard identifier refers to the names of prewritten classes and functions that are provided in the C++ standard library.

  9. Identifiers • An identifier is any combination of letters, digits, or underscores (_) subject to the following rules: • 1. The first character of the identifier must be a letter or underscore (_). • 2. Only letters, digits, or underscores may follow the initial character. Blank spaces are not allowed.

  10. Programmer-selected identifiers must conform to C++’s identifier rules, with the additional restriction that the identifier cannot be a reserved word • C++ is case-sensitive. Accordingly, note that two identifiers such as “MAX” and “max” will be interpreted as two distinct variable names.

  11. If the roots of more than one word are used to form an identifier, the first letter of the first root should be lowercase. First letters of subsequent roots should be capitalized. For example, consider the identifier, “calcSqrRoot”. • it is a descriptive name with less than 14. Try to align concise names with the identifier’s purpose to improve program readability.

  12. The main Function • The main ( ) function provides the casing for a program made up of other functions and/or classes. An instance of a main ( ) function appears below: • int main() • { • C++ program statements in here; • return 0; • } • The body of main( ) is enclosed by braces. The programming logic is included in the body, along with a return statement (made up of the “return” keyword and a return value). • Each statement inside the function must end with semi-colon

  13. Comments // • The first four lines following the double forward slashes are providing descriptive information about the program.

  14. #include • a preprocessor command that uses the reserved word include. The #include preprocessor command causes the contents of the named file, in this case the iostream file in the STL, to be inserted wherever the #include command appears in the program. The <iostream> file contains the ostream class needed to instantiate the cout object. It is referred to as a header file because it is placed at the top of a C++ program.

  15. using namespace std • The statement “using namespace std;” tells the compiler where to look to find the header files in the absence of any further explicit designation. A namespace is a file directory or folder that is accessed by the compiler when it is looking for prewritten classes or functions.

  16. The cout Object • The cout object, whose name was derived from Console OUTput, is an output object that sends data given to it to the standard output display device. For example, consider the classic output line used in first programs: • cout << “Hello World!”; • The insertion (put) symbol, <<, follows the object’s name, and appears just before the text itself. The message is enclosed in quotation marks, and the statement ends in a semicolon. The message will be passed to a cout object, which then displays it on a screen.

More Related