Professional Documents
Culture Documents
2. CHARACTERISTICS OF C++.
C++ have some of the major characteristics over other languages
namely
OBJECT-ORIENTED PROGRAMMING: The possibility to orient programming to
objects allows the programmer to design applications from a point of view more
like a communication between objects rather than on a structured sequence of
code. In addition it allows a greater reusability of code in a more logical and
productive way.
PORTABILITY: We can practically compile the same C++ code in almost any
type of computer and operating system without any modification. C++ is most
used and ported programming language in the world.
MODULAR PROGRAMMING: An application’s body in C++ can be made up of
several source code files that are compiled separately and then linked together.
C COMPATIBILITY: C++ offer back ward compatible with C language, any
code can be written in C can easily be included in a C++ program without any
changes.
SPEED: The resulting code from a C++ Compilation is very efficient, due indeed
to its duality as high-level and low level language and to the reduced size of the
language itself.
MACHINE INDEPENDENT: It is a Machine Independent Language, Thus
programming is easy. It can be used in different type of machine. (Machine
independent: A term applied to software that is not dependent on the properties
of a particular machine, and can therefore be used on any machine.)
FLEXIBILITY: It is highly flexible language with Versatility.
CHARACTER DESCRIPTION
ALPHABETS Upper case and lower case (A a – z Z).
DIGITS Digits ranging from 0 to 9
SPECIAL CHARACTER + - = < > ! ~ : ? { } ( ) [ ] & % . # ; blank and many more
WHITE SPACE Blank space, Horizontal tab, Carriage return, New line, Form
feed
4. C++ TOKENS.
“A token is a smallest individual unit of a program.”
C++ consists of six tokens which are as follows namely
Keywords.
Identifiers.
Literals or Constants.
Operators.
Punctuations.
KEYWORDS.
Some of the words are used in C++ program that have standard predefined
meanings which are nothing but keywords.
“Keywords are predefined reserved words which has a special meaning
to compiler.”
Commonly used keywords are as follows
auto break case catch class
const continue default delete do
double enum else extern float
For friend goto if inline
Int long new operate private
public protected static size switch
template this throw try typdef
union voliate void virtual while
CONSTANTS OR LITERALS
Constant is a fixed value that does not change during the execution of the
program.
Different constant are as follows.
CONSTANTS
Decimal Octal
Hexadecimal
CHARACTER CONSTANTS:
SINGLE CHARACTER CONSTANTS
“A character constant contains a single character enclosed within a pair of single
quote marks.”
Examples of character constants are: ‘5’ ‘X’ ‘;’ ‘‘
Note that the character constant ‘5’ is not the same as the number 5. The last
constant is a blank space. Character constants have integer values known as
ASCII values.
It is also possible to perform arithmetic operations on character constants.
STRING CONSTANTS
“A string constant is a sequence of characters enclosed in double quotes.”
By default the compiler adds a special character called the ‘null character’ (‘\0’)
at the end of the string to mark the end of the string.
For example, the string literal has to be represented as
char str[15] =”Computer Science”;
This is actually represented as char str[15] =”Computer Science \0" in the
memory.
ESCAPE SEQUENCE
C++ supports some special backslash character constants that are used in
output functions. For example, the symbol ‘\n’ stands for new line character.
Escape sequence is special string used to control output on the monitor
and they are represented by a single character.
Each one of them represents one character, although they consist of two
characters. These character combinations are called escape sequences.
Some of the Backslash Character Constant
ESCAPE SEQUENCE MEANING
‘\a’ audible alert(bell)
‘\b’ back space
‘\f’ form feed
‘\n’ new line character
‘\r’ carriage return
‘\t’ horizontal tab
‘\v’ vertical tab
‘\’’ single quote
‘ \” ’ double quote
‘\?’ question mark
‘\\’ backslash mark
‘\0’ null character
PUNCTUATIONS.
Punctuators in C++ have syntactic and semantic meaning to the compiler but do
not by themselves specify an operation that yields a value. Some punctuators
can be either alone or in combination, and they can be significant to the
preprocessor. The following characters are considered as punctuators:
Brackets [] used in arrays for single, two and multidimensional.
Parenthesis () used to enclose expression or an arguments to a function.
Braces {} used to identify the start and end of a block for a statement.
Comma, Semicolon; symbol that can be used as terminators.
Colon: used to provide a label for a statement.
5. OPERATOR.
C++ supports a rich set of built-in operators and there are almost 45 different
operators.
“An Operator is a symbol that tells the computer to perform certain
mathematical or logical manipulations.”
ARITHMETIC OPERATORS:
Arithmetic operators include +, -, *, /, %, which performs all mathematical
manipulations.
These operators can operate on any built-in data type allowed in C++.
Table shows the function of Arithmetic Operators.
OPERATORS MEANING
+ Addition or unary plus
- Subtraction of unary minus
* Multiplication
/ Division
% Modulo Division
RELATIONAL OPERATORS:
Relational operators are used to compare two operands, and depending on their
relation, certain decisions are taken.
For example, it can be used to compare the age of two persons, price of two items and
so on. There are 6 types of relational operators. They are:
OPERATOR MEANING
< Is less than
> Is greater than
<= Is less than or equal to
>= Is greater than or equal to
== Is equal to
!= Is not equal to
A simple relational expression contains only one relational operator and takes the
following form:
LOGICAL OPERATORS:
C++ supports three Logical Operators. They are:
OPERATOR MEANING
&& Logical AND
|| Logical OR
! Logical NOT
TRUTH TABLE
OP-1 OP-2 VALUE OF THE EXPRESSION
Op-1 && op-2 op-1 || op-2
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
ASSIGNMENT OPERATORS:
Assignment operators are used to assign the result of an expression to a variable.
Usually, ‘=’ operator is used. There is an additional ‘shorthand’ assignment operators
of the form
V op = exp;
V= variable
exp = expression and
op = a binary arithmetic operator.
The Operator op= is known as the shorthand assignment operator. The assignment
statement
v op = exp;
is equivalent to :
v = v op (exp);
INCREMENT OPERATOR
This is used to increment the value of a variable in an expression.
Increment operator is denoted by ++.
They can occur in either prefix or postfix position, with possibly different effects
occurring.
There are two types of increment operator.
PRE INCREMENT OPERATOR:
Pre increment operator is used to increment the value of variable before using
expression.
The general syntax is: ++variable
DECREMENT OPERATOR
This is used to decrement the value of variable in an expression.
Decrement operator is denoted by --.
They can occur in either prefix or postfix position, with possibly different effects
occurring.
There are two types of decrement operator
PRE DECREMENT OPERATOR:
Value is first decremented and the used inside the expression.
The general syntax is: --variable
In pre decrement operator there are two steps namely
Value is decrement.
New value is assign.
a--
Here a= a-1
a= New value
Example: a=5;
b=--a
a=a-1
=5-1
a=4
POST DECREMENT OPERATOR:
Value is first used inside the expression and then decrement.
The general syntax is: variable--
In pre decrement operator there are two steps namely
Value is assigned to a variable.
Value is decrement.
--a
Here a= Old Value
a= a-1
Example: a=9;
NOTE:
Post Increment (i++): Current value of ‘i’ is used and then it is incremented by 1.
Pre Increment (++i): First ‘i’ is incremented by 1 and then its value is used.
Post Decrement (i--): Current value of ‘i’ is used and then it is decremented by 1.
Pre Decrement (--i): First ‘i’ is decremented by 1 and then its value is used.
EXAMPLE:
i = 11;
i = i++ + ++i;
i = i++ + ++i
i = (i is used before increment) + (i is used after increment)
i = 11(i=12) + 13(i=13)
i = 11 + 13
i= 24
OPERATOR MEANING
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Left Shift
>> Right Shift
~ One’s Complement
7. ARITHMETIC EXPRESSIONS
An expression is a combination of operators, constants and variables.
An expression may consist of one or more operands, and zero or more
operators to produce a value.
Operand may be variables, constant or expressions.
IMPORTANCES OF <iostream.h>
Declares objects that control reading from and writing to the standard streams.
This is often the only header you need to include to perform input and output
Include statement tells compiler to include Standard Input Output Stream inside C++
program.
This header is provided to user along with the compiler.
COMMENTS IN C++
Program comments are explanatory statements that you can include in the C++ code
that you write and helps anyone reading its source code.
All programming languages allow for some form of comments.
Comment is ignored by Compiler.
Comment are written for user understanding.
All characters available inside any comment are ignored by C++ compiler.
Multi Line comment starts with “/*” and ends with “*/” symbol.
USES OF COMMENTS.
Comments are non-Executable Statements.
Comments are always neglected by compiler.
Comments are replaced by white spaces during the preprocessor phase.
Comments can be written anywhere and any number of times.
Comments can be used for documentation.
Comments can be Single Line or multiple line.