Академический Документы
Профессиональный Документы
Культура Документы
Pointers:
A pointer is a derived data type that refers to another data variable by storing the variable’s
memory address rather than data.
& is overloaded to fetch the address of a variable (Remember, & is bit-wise-and-operator also).
e.g. xptr = &x;
(Meaning: Address of x is assigned as value of xptr)
Pointer Arithmatic:
The following arithmetic operations on pointers are allowed:
A pointer can be incremented (++) or decremented (--).
e.g. iptr++; or jptr--; where iptr & jptr are integer pointer.
Any integer can be added to or subtracted from a pointer.
When you add any integer in the pointer variable, actually it adds sizeof(pointer-variable) in it.
e.g. jptr = arr + 2 will result jptr to point to 3rd value in the arr.
One pointer can be subtracted from another.
Comparison of two pointer variables of same type.
e.g.
cout << ((iptr = = jptr)?"Eq.":"! Eq.“);
But,
Do not attempt
Addition of two pointers.
Multiplication of a pointer with a constant.
Division of a pointer with a constant.
Arrays of Pointers:
The array of pointers represents a collection of addresses.
General Form:
Data-type * array-name[size-of-array];
e.g.
int a,b,c,d,e;
int * array[5];
array[0] = &a; array[1] = &b; array[2] = &c; array[3] = &d; array[4] = &e;
Pointers to objects:
A pointer can point to an object created by a class. They are useful in creating objects at run-time.
E.g.
matrix m1;
matrix * mptr = &m1;
We can also use object pointer to access the public members of an object.
E.g.
m1.display( ); can be written as
mptr->display( ); or
(*mptr).display( ); // Parentheses are necessary as the dot operator has higher
// precedence than the dereference operator *.
We can create objects at run-time using new operator as follows:
matrix * mptr = new matrix;
We can create an array of objects using pointer as follows:
matrix * mptr = new matrix[5];
this Pointer:
C++ uses a unique keyword called this to represent an object that invokes a member function. this
is a pointer that points to the object for which this function was called. E.g. the function call
m1.display( ) will set this pointer to the address of the m1. The starting address is the same as the
address of the first variable in the class structure.
This unique pointer is automatically passed to a member function when it is called. Recall that,
when a binary operator is overloaded using a member function, we pass only one argument to the
function. The other argument is implicitly passed using the pointer this.
One important application of the pointer this is to return the object it points to. E.g. the statement
return * this; inside a member function will return the object that invoked the function. This
statement assumes importance when we want to compare two or more objects inside a member
function and return the invoking object as a result.
person & person :: greater (person & x) Suppose, we invoke this function by the call
{ max = A.greater(B);
if (x.age > age) The function will return object B (argument)
return x; if the age of the person B is greater than that of
else A, otherwise, it will return the object A
return * this; (Invoking object) using the pointer this. Here,
} * produces the contents at the address
contained in the pointer this.
We can also use object pointer to access the public members of an object.
Virtual Functions:
Polymorphism refers to the property by which objects belonging to different classes are able to
respond to the same message, but in different forms. It means we should be able to refer to objects
without any regard to their classes. That means, we have to use a single pointer variable to refer to
all the derived objects. But, as we know that derived class members can not be accessed with base
class pointers. How do we then achieve polymorphism? It is achieved using what is known as
‘virtual’ functions.
When we use the same function name in both the base and derived classes, the function in base
class is declared as virtual using the keyword virtual preceding its normal declaration. When a
function is made virtual, C++ determines which function to use at run time based on the type of
object pointed to by the base pointer, rather than the type of the pointer.