Data Structures: DR Ahmed Rafat Abas
Data Structures: DR Ahmed Rafat Abas
Chapter 4
4.1 Introduction
Programming approaches can be divided
into two main types:
Procedural approach
Object-oriented approach.
In procedural approach
The program is considered as a
sequence of actions.
In object-oriented approach
The actions take second stage to the
objects upon which the actions occur.
4.2 The difference between procedural
approach and object-oriented (OO) approach
In procedural approach:
the first stage in the analysis is to determine the algorithms to
be used by the program.
Then, data structures are designed.
and modules are constructed.
In OO approach:
the first stage in the analysis is to determine the classes.
The actions for each class are written as individual modules.
The overall algorithm is then implemented by arranging for the
individual objects to act on each other, using the actions
associated with each class.
4.3 Definitions
A class: is a collection of attributes
(properties and actions) which collectively
describe an entity.
class classname
{
private:
statements
public:
statements
};
The above class declaration can be stored
in a header file named as name.h.
This header file should be included in the
file name.cpp that contains all definitions
of class functions using #include
statement.
Definition of A class Function
return_data_type classname::functionname(arguments)
:initializationlist
{
statements
}
4.5 Global Variables
A global variable can be accessed by all
functions of the program.
To define a global variable:
put the statement that define this variable in
the file containing the main() function outside
any function definition.
Then, use the extern statement to declare this
variable in each other file, in which this
variable is accessed, as follows:
extern datatype variablename;
The Elements of C++
Chapter 5
5.1 Basic data structures in C++
int: it represents a signed integer data
type.
short and long: a short integer is allocated
two bytes of storage, while a long integer
is allocated four bytes.
float and double: both represent floating
point data type. They allow different
precision for defined variables.
char: it represents a character data
type. The defined variable is allocated
one byte of storage for storing the ASCII
code corresponding to the stored
character.
unsigned: this keyword is used as a
prefix for any data type to indicate that
all the defined variables take either
positive values or zero.
5.2 The main() function
This function is executed in the start of the
program running. Its structure is as
follows:
int main()
{
variable definitions
statements
}
It is a good practice to put this function in a
file called main.cpp.
5.3 Arithmetic operators
= assignment.
+, -, *, /, % addition, subtraction,
multiplication, division and
modulus.
if (condition)
single_statement
or
if (condition)
{
block_of_statement
}
The if-else statement
if (condition_1)
{
statements
}
else if (condition_2)
{
more_statements
}
else if (condition_3)
{
even_more_statements
}
else
{
final_set_of_statements
}
The switch statement
switch(expression)
{
case value_1:
statements
break;
case value_2:
statements
break;
default:
statements
}
The conditional operator ?:
Or
while(expression)
single statements
The do .. while loop
do
{
statements
} while(expression);
The for loop
for(expression1;expression2;expression3)
{
statements
}