Learn : C++ programming language / PDF
Learn : C++ programming language / PDF
Sample of the PDF document
Learning C++
A simple
C++ program:
// File simple.cpp
// Description:
Displays Welcome to CSC126
#include
int
main()
{
cout
<< "Welcome to CSC126. ";
return 0;
}
The above
program results in the following message:
Welcome
to CSC126
Parts of
a program:
C++
programs are made up of functions.
Functions get inputs or arguments and sometimes return a value.
The above
program has only one function titled "main". The main function serves as the point at
which C++ programs begin their execution.
Parts of
a function:
int -
type of information the function tells the user
main -
name of the function. Functions are
named using standard C++ rules for naming identifiers.
Rules:
1.
1. The first character must be a
letter or underscore.
2.
2. Rest only letters, digits, or
underscores, no special characters such as ?, !, # etc.
3.
3. No blank spaces
4.
4. No keywords needed by C++ ex: return, int, void (more complete list in
text)
5.
5. C++ is case sensitive so that dog,
Dog, and DOG represent three different identifiers.
6.
6. identifiers should be
meaningful. Ex. An identifier that
represents an average should be called average.
Valid
identifiers: dog dog3
tree TREE dog_tree _hello
Invalid
identifiers: return 3dogs
boy s
The cout Object:
Stands
for Console Output - sends output to the
standard output device (monitor)
displays
the data sent to the cout object. Welcome
to CSC126. gets
displayed. Not the quotes.
Data that
is enclosed in double quotes is called a character string. cout displays the characters inside the
quotes. << are the insertion
symbol. What follows the << is inserted onto the output stream.
The lines
of the program that start with // are called comments. These are informational and are not executed
by the C++ compiler.
#include
preprocessor
command. All preprocessor commands start
with #. These commands cause some action
to be taken before the compiler translates the program code. The above causes
the code in the file iostream.h to be included for compilation along with the
user written code. iostream.h consists
of code for input and output. of data.
Proper
program format:
Order as
above. preprocessor, main, C++
statements. All C++ statements end with a ;
// File simple2.cpp
// Description:
Displays Welcome to CSC126
#include
int
main()
{
cout
<< "Welcome to CSC126. ";
cout
<< "Hope you enjoy the course.";
return 0;
}
output:
Welcome to CSC126. Hope you enjoy the course.
Notice
the space left between sentences and that cout places the output on one line,
even though there were two cout statements.
What if you wanted the output on two lines.
// File simple3.cpp
// Description:
Displays Welcome to CSC126
#include
int
main()
{
cout << "Welcome to CSC126.
\nHope you enjoy the course.";
return 0;
}
Output:
Welcome to CSC126.
Hope you enjoy the course.
\n is an
escape sequence that means move down to the next line.
// File simple4.cpp
// Description:
Displays Welcome to CSC126
#include
int
main()
{
cout << "Welcome to CSC126.
\n\nHope you enjoy the course.";
return 0;
}
Output:
Welcome to CSC126.
Hope you enjoy the course.............
Learn : C++ programming language / PDF
0 commentaires: