

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


































Fig.
fig01_02.cpp
first
program
in
#include
int
main()
cout
"Welcome
to
C++!\n";
return
indicate
that
program
ended
successfully
Welcome
to
preprocessor directive Message to the C++ preprocessor.Lines beginning with
#
are preprocessor directives.
#include
tells the preprocessor to
include the contents of the file
, which
includes input/output operations (such as printing tothe screen). Comments Written between
/*
and
*/
or following a
//
.
Improve program readability and do not cause thecomputer to perform any action.C++ programs contain one or more functions, one ofwhich must be
main
Parenthesis are used to indicate a function int
means that
main
"returns" an integer value.
More in Chapter 3.A left brace
{
begins the body of every function
and a right brace
}
ends it.
Prints the
string
of characters contained between the
quotation marks.The entire line, including
cout
, the
<<
operator
,^ the
string
"Welcome
to
C++!\n"
and the
semicolon
( ;
), is called a
statement
.
All statements must end with a semicolon.
return
is a way to exit a function
from a function. return
0 , in this case, means that
the program terminated normally.
A Simple Program:Printing a Line of Text^
Cout^ ◦
Output stream object ◦^
“
Connected
”
to the screen
<<^ ◦
Stream insertion operator ◦^
Value to the right of the operator (right operand)inserted into output stream (which is connected tothe screen
)
◦^
cout <<
“
Welcome to C++!\n
”
;
**^ ◦
Escape character ◦^
Indicates that a
“
special
”
character is to be output
Creating an Executable from SourceFiles^
Creating a program module that you canexecute from your C++ source code isessentially a two-step process.
◦
In first step, your compiler converts.
cpp
files
to
object files
that contains machine code
equivalent of the source file contents. ◦
In second step, the linker combines the objectfiles produced by the compiler into a filecontaining the complete executable program.
Creating an Executable from SourceFiles(Contd.)
Declaration of variables^
In order to use a variable in C++, we must firstdeclare it specifying which data type we want it tobe.
The syntax to declare a new variable is to writethe specifier of the desired data type (like
int,
bool,
float
...) followed by a valid variable
identifier. For example:
Declaration of
variables(Contd.)
These are two valid declarationof variables.
The first one declares a variable of type
int
with the
identifier
a.
The second one declares a variable of type
float
with the identifier
mynumber
.
Once declared, the variables
a
and
mynumber
can be
used within the rest of their scope in the program.