Sie sind auf Seite 1von 5

1 // Fig. 1.2: fig01_02.

cpp 1
2 // A first program in C++
Outline
Comments
3 #include <iostream>
Written between /* and */ or following a //.
1. Comments
4
Improve program readability and do not cause the
5 int main() computer to perform any action. 2. Load <iostream>
6 { 3. main
preprocessor directive
7 std::cout << "Welcome to C++!\n";
Message to the C++ preprocessor.3.1 Print "Welcome
8
to C++\n"
Lines beginning with # are preprocessor directives.
9 return 0; // indicate that program ended successfully
#include <iostream> tells3.2 theexit
preprocessor
(returnto 0)
10 } include
C++ the contents
programs containofone
theor <iostream>,
filemore which
functions, one of
includes
which be main operations (such as printing to
input/output
must
the screen). Program Output
Welcome to C++! Parenthesis are used to indicate a function
int means
Prints the string of characters that main
contained between the an integer value.
"returns"
quotation marks. More in Chapter 3.
return is a way to exit a function
from a function. A left
The entire line, including brace { begins
std::cout, the the
<< body of every function
return 0, in this case,
operator, and a right to
thatstring "Welcome
means the } ends it. and
braceC++!\n"
the program terminatedthe semicolon (;), is called a statement.
normally.

All statements must end with a semicolon.

 2000 Prentice Hall, Inc. All rights reserved.


2
1.19 A Simple Program:
Printing a Line of Text
• std::cout
– Standard output stream object
– “Connected” to the screen
– std:: specifies the "namespace" which cout belongs to
• std:: can be removed through the use of using statements
• <<
– Stream insertion operator
– Value to the right of the operator (right operand) inserted
into output stream (which is connected to the screen)
– std::cout << “Welcome to C++!\n”;
•\
– Escape character
– Indicates that a “special” character is to be output

 2000 Prentice Hall, Inc. All rights reserved.


3
1.19 A Simple Program:
Printing a Line of Text
Escape Sequence Description

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

• There are multiple ways to print text


– Following are more examples

 2000 Prentice Hall, Inc. All rights reserved.


1 // Fig. 1.4: fig01_04.cpp 4
2 // Printing a line with multiple statements Outline
3 #include <iostream>
1. Load <iostream>
4

5 int main() 2. main


6 {
2.1 Print "Welcome"
7 std::cout << "Welcome ";

8 std::cout << "to C++!\n"; 2.2 Print "to C++!"


9

10 return 0; // indicate that program ended successfully


2.3 newline

11 }
2.4 exit (return 0)

Welcome to C++! Program Output

Unless new line '\n' is specified, the text continues


on the same line.

 2000 Prentice Hall, Inc. All rights reserved.


1 // Fig. 1.5: fig01_05.cpp 5
2 // Printing multiple lines with a single statement
Outline
3 #include <iostream>
1. Load <iostream>
4
2. main
5 int main()
2.1 Print "Welcome"
6 {

7 std::cout << "Welcome\nto\n\nC++!\n"; 2.2 newline

8 2.3 Print "to"


9 return 0; // indicate that program ended successfully
2.4 newline
10 }
2.5 newline

2.6 Print "C++!"


Welcome
to
2.7 newline
C++!
2.8 exit (return 0)
Multiple lines can be printed with one
Program Output
statement.

 2000 Prentice Hall, Inc. All rights reserved.

Das könnte Ihnen auch gefallen