Вы находитесь на странице: 1из 5

1

// Fig. 1.2: fig01_02.cpp

// A first program in C++

Comments

#include <iostream>

1. Comments
Written between /* and */ or following
a //.

4
5

int main()

Improve program readability and do not cause the


computer to perform any action. 2. Load <iostream>
preprocessor directive

std::cout << "Welcome to C++!\n";

8
9

return 0;

Outline

3. main

Message to the C++ preprocessor.


3.1 Print "Welcome to
Lines beginning with # are preprocessor
directives.
C++\n"

// indicate that program ended successfully

#include <iostream> tells the preprocessor to


3.2
exit (return
include
the contents
theor
file
<iostream>,
which
C++
programs
containofone
more
functions,
one
of 0)
includes
input/output
which
must
be main operations (such as printing to
the screen).
Program Output
Parenthesis are used to indicate a function

10 }

Welcome to C++!

Prints the string of characters


contained
between
the an integer value.
int means
that main
"returns"
quotation marks.
More in Chapter 3.
return is a way to exit a function
from a function.
A left
brace { begins
The entire line, including
std::cout,
the the
<< body of every function
and a right to
braceC++!\n"
} ends it. and
operator,
return 0, in this case,
means the
thatstring "Welcome
semicolon (;), is called a statement.
the program terminatedthe
normally.
All statements must end with a semicolon.

2000 Prentice Hall, Inc. All rights reserved.

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.

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.

\"

Double quote. Used to print a double quote


character.

Backslash. Used to print a backslash character.

There are multiple ways to print text

Following are more examples

2000 Prentice Hall, Inc. All rights reserved.

// Fig. 1.4: fig01_04.cpp

// Printing a line with multiple statements

#include <iostream>

1. Load <iostream>

4
5

int main()

2. main

std::cout << "Welcome ";

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

9
10

return 0;

Outline

// indicate that program ended successfully

2.1 Print "Welcome"


2.2 Print "to C++!"
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.

// Fig. 1.5: fig01_05.cpp

// Printing multiple lines with a single statement

#include <iostream>

Outline
1. Load <iostream>

2. main

int main()

2.1 Print "Welcome"

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

2.2 newline
2.3 Print "to"

8
9

return 0;

// indicate that program ended successfully

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
statement.

2.4 newline

2000 Prentice Hall, Inc. All rights reserved.

Program Output