Вы находитесь на странице: 1из 4

C++

CHAPTER 9: Pointers, Virtual Functions and Polymorphism

Pointers:
A pointer is a derived data type that refers to another data variable by storing the variable’s
memory address rather than data.

General Form of Declaring a Pointer:


Data-type * pointer-variable;
Here, data-type can be any built-in-data-type (int, float, etc.) or user-defined-data-type (complex,
matrix, etc.)
e.g. int * xptr;
(Meaning: xptr will point to some memory location where integer value is stored.)

General Form of Assigning a Memory Address to a Pointer:


pointer-variable = &variable-name;

& 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)

General Form of Declaring a Pointer and Assigning a Memory Address to a Pointer:


Data-type * pointer-variable = &variable-name;
e.g. int * xptr = &x;

One pointer-variable can point to only one variable at a time.

Let us declare two variables:


int x = 50;
int *xptr = &x;

The memory representation will be


x Variable xptr
50 Value 65522

65522 Address of 65520


a variable
Here, xptr is a pointer to an integer (also known as integer pointer).

To dereference a Memory Address via a Pointer we have to use * operator.

cout << *xptr; // internally treated as cout << *(65522); Output: 50


cout << xptr; // It will print address of variable x (Value of xptr) in hex.
cout << (unsigned) xptr; // Same as above but the address will be printed as Decimal.
cout << &xptr; // Output: Address of xptr i.e. 65520.

Using Pointers with Arrays and String:


We can declare the pointer to array as follows:
Example of pointer to a string:
char x[5], * xptr;
xptr = &x[0]; or xptr = x;
Example of pointer to an array:
int arr [ ] = {10,20,30,40};
By Darshit Shah Page 1 of 4
C++

int * iptr, * jptr ;


iptr = &arr [ 0 ]; jptr = (arr + 0 );
Here, iptr and jptr point to 1st value of arr.

Difference between Pointers and Arrays:


 Arrays refer to a block of memory space, whereas pointers do not refer to any section of
memory.
 The memory addresses of arrays can not be changed, whereas the content of the pointer
variables, such as the memory addresses that it refers to, can be changed.

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];

By Darshit Shah Page 2 of 4


C++

Pointers to Members of the Object:


The address of a member can be obtained by applying the operator & to a “fully qualified” class
member name. A class member pointer can be declared using the operator ::* with the class name.
Example:
class A
{
int m:

} x;
We can define a pointer to the member m as follows:
int A :: * ip = &A :: m;
The pointer-variable ip can be used within class A only or with friend function as it refers to private
variable m.
Here. x.m or x.*ip refers to same memory location.

Use of dereferencing operator ->*:


Example:
A * ap = &x;
cout << ap -> *ip; can be used to display the value of m. (only within friend function)

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.

Pointers to Derived Class:


We can use pointers not only to the base class objects but also to the objects of the derived class.
Pointers to objects of a base class are type-compatible with pointers to objects of a derived class.
Therefore, a single pointer variable can be made to point to objects belonging to different classes.
For example, if B is the base class and D is a derived class from B, then a pointer declared as a
pointer to B can also be a pointer to D.
E.g.
B b;
D d;

By Darshit Shah Page 3 of 4


C++

B * ptr = &b; // ptr is pointing to object b (Base class object)


ptr = &d; // ptr is now pointing to object d (derived class object)
Remember, base class pointer can access only those members which are inherited from B and
not the members that originally belongs to D. In case a member of D has the same name as
one of the members of B, then any reference to that member by ptr will always access the
base class member only.

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.

Rules for Virtual Functions:


1. They must be members of some class.
2. They can not be static members
3. They are accessed by using object pointers
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it may not be used.
6. The prototypes of the base class version of a virtual function and all the derived class
version must be identical
7. We can not have virtual constructors, but we can have virtual destructors
8. We can not use derived class pointer to refer to an object of base class.
9. Do not use increment or decrement operator to point to next or previous objects.
10. If virtual function is defined in base class but not in derived class, then calls will invoke the
base function.

Pure Virtual Functions:


A pure virtual function is a function declared in a base class that has no definition relative to the
base class. In such cases, the compiler required each derived class to either define the function or
redeclare it as a pure virtual function. It is also known as “do-nothing” function.
It is defined as under.
virtual return-type function-name(arguments,if any) = 0;
A class containing pure virtual functions cannot be used to declare any objects of its own.
(also known as abstract base classes)

By Darshit Shah Page 4 of 4

Вам также может понравиться