Vous êtes sur la page 1sur 23

GETTING STARTED WITH

C++
C++ (pronounced "cee plus plus")
It was developed by Bjarne Stroustrup starting in
1979 at Bell Labs as an enhancement to the C
language. Originally named C with Classes, the
language was renamed C++ in 1983.
C++ CHARACTER SET
Character set is a set of valid characters that a language
can recognize.



Letters A-Z, a-z
Digits 0-9
Special
Characters
Space + -
* / ^ \ () [] {} = != <> $ , ; :
% ! & ? _ # <= >= @
Formatting
characters
backspace, horizontal tab, vertical tab,
form feed, and carriage return
TOKENS (LEXICAL UNITS)
The smallest individual unit in a program is known as
a TOKEN or a LEXICAL UNIT.
C++ uses the following types of tokens.
Keywords
Identifiers
Literals
Punctuators
Operators.
Keywords
These are some reserved words in C++ which have
predefined meaning to compiler called keywords. Some
commonly used Keyword are given below:
auto break case catch
Identifiers
The identifier is a sequence of characters taken from
C++ character set. The rule for the formation of an
identifier are:
An identifier can consist of alphabets, digits and/or
underscores.
It must not start with a digit
C++ is case sensitive that is upper case and lower case
letters are considered different from each other.
It should not be a reserved word.
Myfile, _CHK, File13 (valid)
29File, break, my.file (invalid)
Literals
Literals (often referred to as constants) are data
items that never change their value during the
execution of the program. The following types of
literals are available in C++.
Integer-Constants
Character-constants
Floating-constants
Strings-constants

Integer Constants
Integer constants are whole number without any
fractional part. C++ allows three types of integer
constants.
Decimal integer constants : It consists of
sequence of digits and should not begin with 0
(zero). For example 124, - 179, +108.
Octal integer constants: It consists of sequence of
digits starting with 0 (zero). For example. 014, 012.
Hexadecimal integer constant: It consists of
sequence of digits preceded by 0x or 0X. E.g. 0XC

Character constants
A character constant in C++ must contain one
character and must be enclosed in single quotation
marks.
For example 'A', '9', etc
C++ allows non graphic characters which cannot be
typed directly from keyboard, e.g., backspace, tab,
carriage return etc. These characters can be
represented by using an escape sequence. An
escape sequence represented by a backslash(\)
followed by character. The following table gives a
listing of common escape sequences.

Escape Sequence Nongraphic Character
\a Bell (beep)
\n Newline
\r Carriage Return
\t Horizontal tab
\0 Null Character
Floating constants
They are also called real constants. They are
numbers having fractional parts. They may be written
in fractional form or exponent form. A real constant in
fractional form consists of signed or unsigned digits
including a decimal point between digits. For
example 3.0, -17.0, -0.627 etc.
A real constant in exponent form consists of two
parts : mantissa and exponent.
For e.g. 0.58 x 10
3
= 0.58E03
Mantissa = 0.58
Exponent 03
String Literals
A sequence of character enclosed within
double quotes is called a string literal.
String literal is by default (automatically)
added with a special character \0' which
denotes the end of the string. Therefore
the size of the string is increased by one
character.
For example "COMPUTER" will re
represented as "COMPUTER\0" in the
memory and its size is 9 characters.
Punctuators
The following characters are used as
punctuators(also known as separators) in C++.

Brackets [ ]
Opening and closing brackets indicate single and
multidimensional array subscript.
Parentheses ( )
Opening and closing brackets indicate functions calls,;
function parameters for grouping expressions etc.
Braces { }
Opening and closing braces indicate the start and end of
a compound statement.
Comma , It is used as a separator in a function argument list.
Semicolon ; It is used as a statement terminator.
Colon :
It indicates a labeled statement or conditional operator
symbol.
Asterisk *
It is used in pointer declaration or as multiplication
operator.
Equal sign = It is used as an assignment operator.
Pound sign # It is used as pre-processor directive.
Operators
Operators are special symbols used for specific
purposes. These are the tokens that trigger
some computation when applied to variables and
other objects in an expression.
C++ provides six types of operators.
Arithmetical operators
Relational operators
Logical operators
Unary operators
Assignment operators
Conditional operators
Comma operator
First C++ Program
// my first program in C++
#include <iostream.h>
int main ( )
{
cout << "Hello World!";
return 0;
}
Hello World!
// my first program in C++
This is a comment line. All lines beginning with two slash signs (//)
are considered comments and do not have any effect on the
behavior of the program. The programmer can use them to include
short explanations or observations within the source code itself.
#include <iostream>
Lines beginning with a hash sign (#) are directives for the
preprocessor. In this case the directive #include
<iostream> tells the preprocessor to include the iostream
standard file. This specific file (iostream) includes the
declarations of the basic standard input-output library in
C++
int main()
is a function. C++ program is a collection of functions.
Every program in C++ begins executing at the function
main(). Every C++ program has exactly one main function
and a collection of other functions.
Brackets () after main signify that main is a function.
Every function should be followed by a pair of brackets
Opening brace ( { ) and closing brace ( } ) mark the
beginning and end of a function. Anything which is inside
the braces is the body of that function.

cout << "Hello World!";
This line is a C++ statement. A statement must always
terminate with a semicolon (;) otherwise it causes a
syntax error. This statement introduces two new features
of C++ language, cout and << operator.
cout is the name of the standard output stream in C++,
and the meaning of the entire statement is to insert a
sequence of characters (in this case the Hello World
sequence of characters) into the standard output stream
(cout, which usually corresponds to the screen).
return 0;
The keyword return is a special statement which is used
to exit a function and return a value. This statement exits
the function main indicating its completion and returns the
value 0 to the operating system indicating that the
program successfully ran to completion. return statement
is included at the end of every main function.


Role of Compiler
Compilers job is to analyze the program code for
correctness.
Some common forms of program errors are :-
Syntax Error
Syntax errors occur when rules of a programming language are
misused. Syntax refers to formal rules governing the
construction of valid statements in a language.
Eg: int a,b //Did not keep ; (semicolon) at the end of statement.
Semantics Error
Semantic errors occur when statements are not meaningful.
Semantics refers to the set of rules which give the meaning of a
statement.
Eg: X * Y = Z;
(Siva plays Guitar is Syntactically and Semantically correct but
Guitar plays Siva is Syntactically correct but Semantically
incorrect).
Runtime Errors
(Execution Error):A run time error is that occurs
during the execution of a program. It is caused of some
illegal operation taking place or unavailability of desired or
required conditions for the execution of the program.
Eg: Division by zero,
Trying to open a file which does not exist or it could not be
opened,
If enough memory is not available.
Logical Errors
A logical error is that error which causes a program to
produce incorrect or undesired output. For instance, if we
are trying to print the table of a number 3 and if we say
ctr=1;
while(ctr>10)
{ cout<<n*ctr;
ctr=ctr+1;
}
Here the loop would not be executed even once as the
condition(ctr>10) is not fulfilled at all.

USING I/O OPERATORS
A program in C++ includes header file iostream.h for
standard stream input/output facilities.
In C++ all devices are treated as files.
Standard input device (Keyboard)
Standard output device (Monitor)
Standard error device (Monitor)
All above are treated as files. At its lowest level, a file
is interpreted simply as a sequence or stream of
objects.
The predefined stream objects for input, output and
error as follows:-
cin (see-in) stands console input
cout (see-out) stands console output
cerr(see-err) stands for console error
Using I/O Operators
By default, the standard output of a program is the
screen, and the C++ stream object defined to access
it is cout.
cout is used in conjunction with the insertion
operator (put to), which is written as << (two "less
than" signs).
Operator << is the insertion stream operator. It sends
contents of variable on its right to the object on its
left.
cout<<My first C++ Program

int x = 10 ;
cout << "Output sentence"; // prints Output sentence on
screen
cout << 120; // prints number 120 on screen
cout << x; // prints the content of x on screen
cout<< 8-2;
Using I/O Operators
Computer program is an interactive program which
takes values from user. To input values, C++ uses
the Standard Input Stream cin operator.
cin>>number;
The operator >> is known as extraction or get from
operator. It Extracts the value from keyboard and
assigns it to the variable on its right.
int age;
cin >> age;
The first statement declares a variable of type int
called age, and the second one waits for an input
from cin (the keyboard) in order to store it in this
integer variable. cin can only process the input from
the keyboard once the RETURN key has been
pressed.
# include <iostream>
int main ()
{
float var1, var2, sum, avg;
cout << "Input first value" << endl;
cin >> var1;
cout << "Input second value" << endl;
cin >> var2;
sum = var1+ var2;
avg = sum / 2;
cout << "Sum is" << sum << endl;
cout << "Average is " << avg << endl;
return 0;
}

Cascading of I/O Operators

The multiple use of input or output operators (>> or << )
in one statement is called cascading of I/O operators.
#include
int main()
{
int a,b;

cout << "Enter value of a:" ;
cin >> a ; //reads the value of a from
keyboard
cout << "Value of a=" << a;

cout << "Enter value of a and b:";
cin >> a >> b; //reads the value of a and b

cout << "Value of a=" << a << "\n Value of b=" << b ;

}



Comments in C++ Program
C++ has two ways to insert comments into source code.
Comments provide the programmer with a clear, easy-to-
read description of what the program does. They are
required because uncommented programs are hard to
understand.
Single line comment with //
Multiline or block comment with /*..*/
// my first program in C++
#include <iostream.h>
int main ( )
{
cout << "Hello World!";
return 0;
/* simple prgm written for the purpose
of explaining commnets */
}


EDITOR
SOURCE FILE
COMPILER
OBJECT FILE
LINKER
EXECUTABLE FILE
LIBRARY FILE
COMPILED CODE
LINKED PROGRAM

Vous aimerez peut-être aussi