Page 241 - DCAP404 _Object Oriented Programming
P. 241

Object-oriented Programming




                    Notes          The following program example demonstrates the declaration and use of pointer in C++.


                                          Example:
                                   #  include<iostream.h  >
                                                 int  main(  )
                                                 {
                                                        int  i  =  18,  *  p_age;
                                                        p_age    =  &I;
                                                        cout<<i<<  “  <<  *  p_age  <<  endl;
                                                 }



                                     Did u know?  Why Member function pointers are important?

                                     Member function pointers are important because they provide an efficient way to cache
                                     the outcome of a decision over which member function to call.

                                   11.2.1 Accessing Address of Variable

                                   Consider the following statements:
                                   int q, * i, n;

                                   q = 35;
                                   i = & q;
                                   n = * i;
                                   i is a pointer to an integer containing the address of q. In the fourth statement we have assigned
                                   the value at address contained in i to another variable n. Thus, indirectly we have accessed the
                                   variable q through n using pointer variable i.

                                   11.2.2 Pointer Expressions


                                   Like other variables, pointer variables can be used in expressions. Arithmetic and comparison
                                   operations can be performed on the pointers.


                                          Example: If p1 and p2 are properly declared and initialized pointers, then following
                                   statements are valid.

                                                 y = * p1 * *p2;
                                                 sum = sum + * p1;

                                   11.2.3 Pointer Initialization

                                   When declaring pointers we may want to explicitly specify which variable we want them to
                                   point to:








          234                               LOVELY PROFESSIONAL UNIVERSITY
   236   237   238   239   240   241   242   243   244   245   246