Elements of C++ language
Tokens and Identifiers: Character Set
and Symbols, Keywords, C++ Identifiers
Variables and Constants: Integers &
Characters, Constants and Symbolic constants, Dynamic initialization of
variables, reference variables, enumerated variables
Data Types: Basic data types, arrays and
strings, user defined data types
Operators: Arithmetic, relational operators and operator’s precedence,
logical operators, manipulators, type conversions and type cast operators
Console I/O: cin, cout functions
Control
Statements: The if statement, if-else; else
... if switch statements
Loops: for and do-While statements,
Break, continue, go to
Tokens and Identifiers: Character
Set and Symbols, Keywords, C++ Identifiers
Character
set and symbols: -
1.
Combination of English language i.e.
alphabets and white spaces and math’s symbols i.e. digit and special symbols
are termed as character set.
2.
In C++ programming we can say that, the
characters and symbols which can easily understood and accepted by C++ program.
3.
Character set and symbols are the grouped
to form the commands, expressions, words, C- statement and other tokens for C++
4.
We can also say that a C++ program is a
sequence of characters and these characters from the set of character plays the
different role in different way in C++ programming language.
5.
There are four categories of the character
set they are as follows: -
a.
ALPHABET:
-
As we know that alphabets are represented
by A-Z or a-z. Because of C++ is a
case sensitive programming language there for there is a different meaning for
lower case and upper case.
b.
DIGITS:
-
From 0-9
or combination of 0-9 is known as digits.
c.
SPECIAL
SYMBOL: -
In C++ there are varieties of special
symbol available, i.e. mathematical, logical, and relational operators like: +, -, *, /, \, ^, %, @, !, #, &, (, ),
[, ], {, }, :, ; , , and many more.
d.
WHITE
SPACES: -
Blank spaces, new line, return, horizontal
tab space etc. are known as white spaces.
Keywords
in C++: -
As we already know about
keyword in C programming language. Pre-existing, reserved words, in which each
holding its own position and power and has its own specific function associated
with it are knows as keywords.
There are total 63
keywords in C++ programming language in which 32 belongs to C program.
So we will divide this
into two groups which make us easy to understand and remembered.
First
group belongs to C, they are as follows: -
auto
|
const
|
int
|
short
|
continue
|
else
|
long
|
signed
|
case
|
default
|
register
|
size of
|
char
|
do
|
return
|
static
|
double
|
float
|
struct
|
unsigned
|
for
|
break
|
switch
|
void
|
enum
|
goto
|
typedef
|
volatile
|
extern
|
if
|
union
|
while
|
There
are another 30 reserved word that were not in C, there for they are new for
C++, they are as follows: -
asm
|
dynamic_cast
|
bool
|
explicit
|
catch
|
false
|
typename
|
class
|
this
|
using
|
public
|
throw
|
mutable
|
protected
|
try
|
typeid
|
namespace
|
reinterpret_cast
|
new
|
static_cast
|
operator
|
template
|
friend
|
private
|
const_cast
|
inline
|
Virtual
|
delete
|
true
|
wchar_t
|
C++
identifiers: -
1. Variables,
Classes, Functions, Structures, Arrays and other entity in C++are known as identifiers.
2. In
C++ the programmer are used to assign names of his own choice to arrays,
functions, classes and other data structures are termed as identifiers.
3. Programmers
can use the different types of character sets available in C++ to create an
identifier.
4. There
are some rules to create identifier or Rules
for identifiers in C++.
a. First character of identifier: -
The
first character of identifier must start with a alphabet (A-Z or a-z) or
underscore (_), cannot with any special symbol and any digit.
b. No special character are allowed: -
In identifiers we can use special character
like @, #, $, %, / etc, except underscore (_).
E.g. int
@abc it will give error.
c. Keywords cannot be used as identifiers:
-
As we know that, keywords are reserved and
each one holds a special meaning in the C++ compiler, if we use any one of them
as identifier it would get a compilation error.
E.g. int
goto it will give compilation error because goto is a key word.
d. White spaces cannot used: -
While declaring identifiers, there should
not be used of white space between them.
E.g. float
roll no (it will show error)
float
roll_no can be used
e. Word limit: -
While declaring an identifier word limit is
necessary. Identifier must not exceed 31 characters otherwise, it will
insignificant.
f. Case sensitive: -
Upper case and lower case character in C++
have different meaning because C++ is a case sensitive programming language.
E.g int result and int RESULT are different
in C++.
Variables and Constants: Integers & Characters, Constants and Symbolic constants,
Dynamic initialization of variables, reference variables, enumerated variables
Integers and characters: -
1.
Integer is a data type, which is used for storing
integer values like numbers.
2.
int
is
a keyword which is used to represent integer data type.
3.
It takes 4 bytes of memory space in memory.
4.
Its signed ranged is from -2147483648 to 2147483647 and unsigned
range is from 0 to 4294967295.
5.
Integer is of some types like short int, int, long int, or unsigned int all
of them having different sizes and ranges.
6.
Character is
a data type, which is used for storing characters like alphabets..
7.
char
is
the keyword which is used to represent character data type.
8.
It takes 1 bytes of memory space in memory.
9.
Its signed ranged is from -128 to 127 and unsigned range is from 0 to 255.
Constant:
-
1.
As we know that constant refers to the
values which are fixed or unchangeable, unlike variables whose value can be
changed or altered.
2.
To assign a value as constant in C++, const keyword is used.
3.
Constants are also known as literals, Literals are used to express particular
values within the source code of a program.
4.
Variables declared with the keyword ‘const’ become constants and cannot be
altered by the program.
5.
For example a = 5;
The 5 is this piece of code was a literal
constant.
6.
Literals
constant can be divided into following: -
a.
Integer constant:
- They are numerical constants that identify integer decimal values. (e.g.
digits or its combination)
b.
Floating Point constant: - They express numbers with decimals
and exponents. It can include either a decimal point, an “e” character. (e.g.
value of Pi = 3.141 or exponential e = 6.02e23)
c.
Character and string constant: - There also non-numerical constants
like:
‘z’ ‘p’
“Hello world” “How do you do”
The first two
expressions represent single character constants, and the following two
represent string literals composed of several characters.
d.
Escape
codes:
Character and
string literals have certain peculiarities, like the escape codes. These are
special characters that are difficult or impossible to express inside the
program. List of some escape code are: -
i.
\n newline
ii.
\r carriage
return
iii.
\t tab
iv.
\v vertical
tab
v.
\b backspace
vi.
\f form
feed (page feed)
vii.
\a alert
(beep sound)
viii.
\’ single
quote (‘)
ix.
\” double
quote (“)
x.
\? Question
mark (?)
xi.
\\ Backspace
(\)
e.
Boolean
constant: - There
are only two Boolean values: true and false.
f.
Defined
constants (#define): - We
can define anything (our own names, any number, any character) as constants, by
simply using the #define pre-processor
directive. Its format is:
#define identifier value
For example: - #define Pi
3.14159 (now we can use Pi instead of its value, and its value cannot be change
anywhere)
g.
Declared
constants (const): -
By using const prefix we can declare
constants with a specific type in the same way as we would do with a variable:
const int width = 102;
const int height = 15;
Variables: -
1.
A
variable is a value that can change, depending on conditions or on information
passed to the program.
2.
Variable
is container is used to store the values in our programs.
3.
In
C++ variable are those, where we need to store any value either run time or
compile time, and its value can be change in program.
E.g. int a=5; //declared and
initialized (a is a variable with value 5)
4.
Variable
is the name of the memory location which is allocated by the compiler and its
size depends upon the data type of the variable.
5.
Syntax: - data-type
variable, variable;
6.
There
are some basic types of variable they are as follows: -
a.
bool
(to store Boolean values i.e. true or false)
b.
char (to store character types)
c.
int (to
store integers types)
d.
float and double (to store large number of values and also floating
point)
Declaration and
dynamic initialization
Before using a variable it must be declared first. In
C++ we can declared in the middle of the program because C++ follows bottom-up
approach.
For example: -
int
i; //declared but not initialized
char c;
int
i,j,k; //multiple
declaration
int
i; //declaration
i=10; //initialization
int
i=10; //declaration and initialization in one step
int
i=20, j=10;
int j = i+j; //compile
time error, re-declaration a variable in same scope
Scope of variable: -
Variables have their area of
functioning, and out of that area they don’t hold their values, this area is
known as scope of the variables.
On the basic of their scope variables
can be classified into two types: -
a.
Local
variables
b.
Global
variables
Local variable: -
Variables which are exist
within the function or block. Outside the function or block it reduces the
error. Life time of local variable is until that function or block executed.
For example: -
int main()
{
int m=10;
if
(m<20 o:p="">20>
{
int
n=120; //local
variable declared
cout
< // print n value
}
cout
< //compile time error, n not declared
}
Global variable: -
A global variable is a
variable declared in the main body of the source code, outside all function.
The global function must be declared outside the main() function. Life time of the
global variable is until the program is executed.
For example: -
int
a; //Global variable declared
int
main()
{
x
= 100; //initialized
once
cout
< < ”first x=” < < x;
x=
200; //initialized
again
cout<
< ”second x=” <
}
Dynamic initialization of variable: -
1.
Dynamic initialization of variable refers
to initializing the value of variables at run time i.e. the initial value of
variable/ object is to be provided during run time.
2.
The need of dynamic initialization of
variables are as follows: -
a.
Utilization of memory is efficiently.
b.
By using overloaded formats, various
initialization formats can be provided.
c.
It has the flexibility of using different
formats of data at run time.
For
example: -
int
main()
{
int
i,j;
i=10; //Dynamic initialization at run time
j=14;
//Dynamic initialization at run
time
int
k=i+j;
cout
< <”the value of k is” <
}
OUTPUT: - the value of k is 24
Reference variable: -
1.
A new kind of variable introduced by C++
which is known as reference variable.
2.
It provides an alternate name to the
previously defined variables.
3.
It must be declared at the run time of the
declaration.
4.
Syntax:
-
data-type &refrence_variable =
variable_name
For
example: -
int xyz =100;
int &abc = xyz; //reference variable
Enumerated variables: -
1.
An enumeration is a user-defined data type
which contents an optional storage and can be assigned optional storage.
2.
The value of the storage is defined by the
programmer at the time of declaring enumerated variable.
3.
Syntax:
- enum
enumerated_type_name{value1, value2, value3, …, valueN};
Note: - enumerated_type_name
is the name of variable.
4.
enum is a keyword which is used to defined
enumerated type name.
For example: -
enum grain {oats, wheats,
barley};
Thank u sir
ReplyDeleteyou are welcome..!!!
DeleteThanx sir
ReplyDeleteThe way u write the notes is understandable at all.....thanks for this
ReplyDeleteThank you ..!!
DeleteNice
ReplyDeleteNice
ReplyDeleteThank you..!!
DeleteSir please upload introduction of c++
ReplyDeleteThank you sir thank
ReplyDelete