Documente Academic
Documente Profesional
Documente Cultură
• Fundamentals of C++
• Structure of C++ Program C++
• Data types
• Variables
• Escape Sequences
• I/O Statements (cin/cout)
Structure of a C++ Program
Opening brace
Closing brace
Opening brace
Closing brace
Preprocessor Directives
• A libraries that contain /* A program to print the string
functions and symbols that are Welcome to C++ to the screen
necessary to run a C++ */
program. #include <iostream>
• Every library has a name and is
int main()
referred as a header file. {
• All preprocessor commands /* print out the message */
cout<<"Welcome to C++"<<endl;
begin with #. return 0;
• In Standard C++, header files }
• The statement
// input statement
• Executable Statements
cin>>c;
a = 10; sum=a+b+c;
//assignment statement
cin>>c; /* output statement */
//input statement cout<<sum<<endl;
cout<<a+b+c<<endl;
//output statement return 0;
}
Comments
• Multiple line comments are /* A program to print the string
Welcome to C++ to the screen */
enclosed between /* and */
#include <iostream>
• Single line comments begin using namespace std;
with // int main()
• Statements that have no {
effect on the execution of // print out the message
the program cout<<"Welcome to C++"<<endl;
The string “hello world\n” is sent to the cout function and the
function will display the message on the screen
The \n that appears in the cout function call will cause the message
to be printed on the screen then moves the printing to the next line
Syntax
• A programming language is a set of rules, symbols, and special words used
to construct a program.
• There are rules for both syntax (grammar) and semantics (meaning).
– Syntax: The formal rules governing how one writes valid
instructions in a language
• The syntax rules tell us which statements (instructions) are legal, that is,
accepted by the programming language and which are not.
Token
• The smallest individual unit of a program written in any language is called a
token.
• Tokens in C++
– keywords
– identifiers
– constants
– string constants
– operators
– punctuators
• Language syntax specifies the valid combinations of tokens in the language into
legal strings.
Token types
• Identifiers
main, payRate, score_1, conversion
• Operators
+ - * / ( )
• Keywords
int, float, double, char, void, return
• Punctuators
. ; ? , {
• Others
<= != == >=
• Comments are not tokens (remember they are removed from the program)
Identifiers
• Identifiers allow us to name variables, constant names, functions and
objects in the program
• Each piece of data in the computer is stored at a unique memory address
• Identifier names allow us to symbolically deal with the memory locations
so that we don’t have to deal directly with these addresses
• A C++ identifier are formed using the following symbols
– Capital letters A to Z
– Small letters a to z
– Digits 0 to 9
– Underscore _ “shift key with minus key in the keyboard”
Identifiers A Syntax diagram of the identifier
• Rules to form and Identifier
– First character must be an Letter
alphabet or an underscore
Letter digit
– Can consist of alphabets, digits and
underscore underscore underscore
– C++ is case sensitive language
A _a a123
ABcDf99_ sum average 1a_a a+2 a$1
Mark1 Mark_2 xy_1_4
A”bc -abc aa.bb
Sum_of_values avg_plus_sum
A abc a9!c
these are not the same
Why they are invalid ?.
Aa aa aA AA aa
Keywords (reserve words)
• Keywords are words (like identifiers) that have a special meaning
to the compiler and cannot be used in your programs for other
purposes.
• On most newer compilers, the data types double and long double are
the same.
• The maximum number of significant digits—that is, the number of decimal
places—in float values is 6 or 7 and double is 15.
int main ()
{
const int noOfStudents = 20;
const char blank = ' ';
const double payRate = 15.75;
return 0;
}
Variables
• Variables don’t have fixed values
throughout the program dataType identifier;
• In C++ language there are a set of
operations to change and
manipulate the value stored in int conversion = 2.54;
these variables
• Every variable used in the program
must be declared before its use = Literal
• Variable names are valid identifiers
and cannot be the same as any of
the reserved words (keywords) type identifier ;
cin>>…
cout<<…
Input (Read) Statement: cin Output : cout
• Syntax of cin together with • The syntax of cout together with <<
>>: is
cin>>variable; cout<< variable;
int a,b,c;
int a, b, c;
The blanks between the identifiers in the second statement
are meaningless.
inta,b,c;
no blank between the t and a changes the reserved word
int and the identifier a into a new identifier inta.
x = y * a;
Consider the following
const double conversion = 2.54;
double centimeters;
double inches;
• Run-together-word
inchperfoot inchPerFoot inch_per_foot.
Prompt Lines
cout<<"Please enter a number between 1 and 10 and"
<<" press ENTER"<<endl;
cin>>num;
When these two statements execute in the order given, first the cout statement
causes the following line of text to appear on the screen:
After seeing this line, users know that they must enter a number and press the return
key.
Escape Sequence
• Escape sequences are used to
represent certain special Escape
Description
sequence
characters within string
\' single quote
literals and character literals.
\" double quote
\? question mark
\\ backslash
\a audible bell
\b backspace
\n line feed - new line
\r carriage return
\t horizontal tab
More about cout
• precision (int) sets the number of significant digits of
float type numbers
float y = 23.1415;
cout.precision(1);
cout << y << '\n'; // Outputs 2e+01
cout.precision(2);
cout << y << '\n'; // Outputs 23
cout.precision(3);
cout << y << '\n'; // Outputs 23.1
Summary
• The body of the function is enclosed between { and } and has two types of
statements.
• Statements enclosed in // have no effect on the execution of the program.
• Identifiers allow us to name variables, constant names, functions and objects in the
program.
• C++ data types fall into three categories: Simple Data Type. ,Structured Data Type.,
Pointers
• Named Constant: A memory location whose content is not allowed to change
during program execution.
• Variable: A memory location whose content may change during program
execution.
• cin reads data from the keyboard and stores it to a buffer
• cout prints out data to the monitor
• In C++, >> is called the extraction operator
• In C++, << is called the insertion operator
33