2. Important Features of OOP
There are three major features of
Object Oriented Programming
1. Encapsulation: Achieved by dividing
a program into classes (similar to
structures)
2. Inheritance: Classes are derived
from other classes
3. Polymorphism: Achieved by use of
virtual functions and pointers
3. Pointers …Background
Three information are associated with
variable declaration i.e. type, name and
address (memory location)
Computer memory has various locations
(unique address) for each of its byte
Program occupies a certain range of
memory locations
Similarly, the variables used in the
program also occupy certain memory
locations (addresses)
These memory locations (related to
variables) can be accessed using
pointers
4. Pointer Variables
The variable that is used to hold the
memory address of another variable is
called pointer variable or simply a
pointer
Pointer variable is associated with
another variable
It contains the memory of address of
another variable
The type of variable (whose address
to be held) and the type of pointer
variable should be same
5. Declaration of Pointer
Variable
Pointer variable is declared by
1. Type: the of pointer should be kept
as the type of its pointing variable
(whose address to be held)
2. Asterisk sign ( * ): that indicates its
pointer type/nature
3. Name: any valid name (like variable
name)
e.g. int *xyz;
6. Declaration of Pointer
Variable
Example of variable declaration and
declaring its pointer
int abc, abd;
int *abcd, *abdd;
float xyz;
float *xy;
float *yz;
7. Accessing Memory Address
through Pointer
Memory address of a variable can be
accessed through pointer using address
operator (&), unary “and” operator
int abc, abd;
int *abcd, *abdd;
abcd = &abc;
abdd = &abd;
float xyz;
float *xy;
xy = &xyz;
8. Void Type of Pointer
Usually the type of pointer is same as
the type of variable that for whom the
memory is hold
If the type of a point is set as void,
then it can hold the memory of any
type
void keyword is used before the name
of declaring void pointer
e. g. void *p, *abc;
9. Pointers and Arrays
There is a close relation between pointers
and arrays as:
Array consists of consecutive memory
locations, so the memory of whole array can
be calculated from the memory address i.e.
pointer of any cell
e.g. int abc[5]
int *p;
p=x; //points to the initial element (no //need of
& for no address
p=&abc[0]
Now the 2nd, 3rd element address can be
found as p+1 and p+2 and so on.
10. Pointers …
Lab Assignments:
1. Write a program that finds and
displays the memory address of
different variables using pointers
2. Write a program that finds the
memory address of a 30 elements of
an array, use different types (int,
float, double etc.) of array