Chapter1: Review of C++
Program is a sequence of instructions
given to a computer to be executed to perform a desired task. Every program is
written in some programming language and C++ is such a language developed at AT
& T Bell Laboratories by Bjarne Strostrup in the 1980s.
Tokens
They
are smallest individual units of a programming language. It is same as words in
English and other languages. They are,
· Key words
· Identifiers
· Literals
· Punctuators
· Operators
Keywords
They
are reserved words for C++ language, which are used for special purposes. There
are 48 keywords.
Identifiers
It includes letters and digits used to
identify memory locations (variables), or naming a function, labels etc.
Literals
They are represents data items that never
change their values during a program run. They are also called constants. They
may be numeric literals, character literals, string literal and escape
sequence.
Punctuators
They are symbols used as punctuation marks
in the syntax of various codes and as separators to enhance program
readability. They are, parentheses ( ), brackets [ ], braces { } , ; : * = #
(Preprocessor directive).
Operators
They are symbols that are used in
operations such as input, output, computation, assignment etc. + - * / %
<< >> ++ etc are some examples.
Basic Data types and Operators
Data Types
Data types are the means to identify
the type of data and associated operations of handling it. C++ data types are
of two types: Fundamental data type and Derived data type.
Fundamental Data Types
Fundamental data types are those,
which represent atomic data items, i.e; the data items that cannot be
decomposed of. There are five such data types: ‘int’ for integers, ‘char’ for
characters, ‘float’ for real numbers, ‘double’ for real numbers with higher
precision and ‘void’ for null set of data.
Derived Data Types
These data types represent the data
items that are composed of the fundamental data types. derived data types in
c++ are structure, arrays and
enumeration
1. Arrays:
They refer to a named set of finite number of same type of data, each of which
can be referenced by mentioning its position (called subscript) in the list.
There are one-dimensional and multi-dimensional arrays.
2. Structure:
A structure is a group of related data types referenced by a single name. The
keyword used is struct.
3. Enumeration: A user
can define a set of integer type identifiers in the form of new data types
called enumerated data types. The key word used is enum.
Operators
The tokens that trigger some kind of operations are called operators. An
operation requires operator and operands (s). Operands are the data on which
the specified task is carried out.
1. I/O Operators
The operators for input (cin) and output (cout) operations are >> (called
extraction or get from operator) and << (called insertion or put to
operator) respectively.
- Arithmetic Operators
The
symbols (or tokens) that trigger computations in the program are called
arithmetic operators. They are
Operator
|
Example a=5, b=2
|
+ Addition
|
5+2=7
|
- Subtraction
|
5-2=3
|
* Multiplication
|
5*2=10
|
/ Division
|
5/2=2.5
|
% Remainder of the division
|
5%2=1
|
- Increment / Decrement Operators
The Increment operator (++) adds 1 to
its operand and Decrement operator (--) subtracts 1 from its operand.
- Relational Operators
These operators compare two quantities, that is, they determine the relation
between two data. The result of the operation will be either TRUE (1) or FALSE
(0). There are six such operators and all of them are binary operators. They
are: < (less than), <= (less than or equal to), > (greater than),
>= (greater than or equal to), = = (equal to) and ! = (not equal to).
- Logical Operators
Logical operators are used to combine relational expressions. The operands as
well as the result of these operators are Boolean values (TRUE and / or FALSE).
The logical AND (&&), Logical OR (||) and logical NOT (!) are the
operators. Consider two relational operations with following output then,
- Conditional Operator (?:)
This operator returns a value depending on a condition. It is a ternary
operator, which requires three operands to operate upon. The syntax is:
Expression
l ? Expression 2 : Expression 3
- Sizeof() Operator
It is a unary operator that returns the number of bytes
occupied by the variable or data type specified within parentheses. For example
sizeof (float) returns 4.
- Comma Operator
It is used to string together several expressions. The group
of expressions separated by commas will be evaluated from left to right in
sequence and the result of the right most expression will be value of the total
comma separated expression.
Expressions
Operators and operands are combined to form expressions. An expression
represents and operation and normally returns a value as the result of the operation.
It may be integer expression or real expression or logical expression.
Eg. c=a+b;
Type Conversion
1. Implicit type conversion (type promotion):
Here
the conversion is done by the complier without programmer’s intervention. In
this case the complier converts all operands up to the type of the largest
operand.
2. Explicit type conversion (type casting):
Here the user is
responsible for the conversion to a specific type.
Control Statements
1. Selection Statements
The statements, which alter the normal flow of control based on a condition,
are called selection statements. They are also known as conditional statements
or decision statements. There are two such statements in C++: if statements and
switch statement.
The if Statement
The ‘if’ statement has three forms: Simple if, if...else, if...else…if ladder.
The syntax of simple if
if (Expression)
{
Statements;
}
If the Expression
(relational or logical) evaluates to True, the Statement will be executed;
otherwise it will be skipped.
The switch statement
When a number of actions are to be
taken based on a number of different conditions, switch statement is the better
selection statement.
The syntax is:
Switch (Expression)
{
case Constant 1 : Statement Sequence 1;
break;
case Constant 2 : Statement Sequence 2;
break;
_________________
case ConstantN-1 : Statement SequenceN-1;
break;
deafault : StatementSequenceN;
}
2. Iteration Statements
Iteration statements allow a set of instructions to be executed repeatedly
based on some condition. These statements are also known as looping statements.
It has Loop control variable, Initialization expression, test expression,
update expression and loop body.
The for loop
The syntax is:
for (Initialization Expression; Test Expression;
Update Expression)
{
body-of the-loop;
}
The while loop
It is also an entry-controlled loop.
The syntax is:
Initialization expression
while (Test Expression)
{
loop-body ;
Updation expression;
}
The do-while loop
It is an exit-controlled loop.
The syntax is:
Initialization expression
do
{
loop-body;
Updation expression;
}while (Test Expression) ;
Arrays
It refers to a named list of a
finite number of similar data elements. I.e. An array is a collection of same
type of data referenced by a common name. It is a collection of continuous
memory locations referenced by a common name. Each of the data elements can be
referred by set of consecutive numbers, usually 0, 1, 2, 3, 4, ……….., n.
One Dimensional Array
It is the simplest form of an
array that can store a linear set of values with one subscript or one index.
Declaration
Syntax:
Datatype arrayname[size];
Eg:
Int mark[100];
Multidimensional Array
Eg: Two Dimensional Arrays
A 2D array is having rectangular format with rows and columns. It is also known
as table. A 2D array a[m][n] is a table with m rows and n columns. It contains
m x n elements. Row index starts from 0 to m-1and column size starts from 0 to
n-1. Each element has to subscripts. Ie a[0][0], a[0][1],……
Declaration
Syntax of 2D arrays
Datatype arrayname [rowsize][columnsize];
Eg:
Int a[10][10];
String as an array
Strings are group of characters for representing name, house name, city etc.
They are represented in double quotes. Eg: “Geetha”, “Dubai” etc. Here a null
character terminating character \0 is used to represent the end of the array.
Declaration
syntax:
Char name[20];
Functions
To make large programs in to
manageable programs, breakdown them in to smaller sub programs is called
modularization and the sub programs are called functions.
Functions
are classified in to two standard library functions and user defined functions.
User defined functions
A
programmer can define their own functions in a program is called user defined
functions.
It
includes three components, Declaration, definition and Invoking / Calling a
function.
Scope rules of functions and variables
Scope
of function and variables means the ability to access them in a program. It is
classified in to two, Local and Global.
Local variable
The variables declared within a function is said to
have local scope. They cannot access these to outside functions.
Global Variables
Variables declared outside the body of function will
have global scope. They can access any function in the program.
Types of parameters
The
parameters appeared in function call statements is called actual parameters and
the parameters appeared in function definition statements is called formal
parameters.
These
parameters are passed from calling function to function definition in two ways,
Call by value and call by reference.
Call by value
In
call by value method, copies the value of actual parameters in to the formal
parameters are passed. There for any changes made in the formal parameters does
not reflect back to the actual parameters.
Call by reference
In
call by reference method, address of memory location of actual parameters in to
the formal parameters is passed. There for any changes made in the formal
parameters will reflect back to the actual parameters.
No comments:
Post a Comment