Page 247 - DCAP404 _Object Oriented Programming
P. 247

Object-oriented Programming




                    Notes                 temp  =  *x;
                                          *x  =  *y;
                                          *y    =  temp;
                                   }
                                   Output: Original values
                                   a = 7, b = 4

                                   Swapped values
                                   a = 4, b= 7





                                     Notes  The called function does not create own copy of original values by the addresses
                                     (passed through pointers) it receives.

                                   11.4.2 Function Returning Pointers

                                   The way a function can returns an int, a float, a double, or reference or any other data type, it can
                                   even return a pointer. However, the function declaration must replace it. That is, it should be
                                   explicitly mentioned in the function’s prototype. The general form of prototype of a function
                                   returning a pointer would be type function-name (argument list);
                                   The return type of a function returning a pointer must be known because the pointer arithmetic
                                   is relative to its base type and a complier must know what type of data the pointer is pointing to
                                   in order to make it point to the next data item.


                                          Example: Program to illustrate a function returning a pointer.
                                   #include<iostream.h>
                                   #include<conio.h>
                                   int  *big(int&,  int&)                     //prototype
                                   int  main(  )
                                   {      clrscr(  )

                                          int  a,  b,  *c;
                                          cout<<“Enter  two  integers  \n”;
                                          cin>>a>>b;
                                          c=  big(a,  b);
                                          cout<<“The  bigger  value  is  “<<  *c<<  “\n”;
                                   return  0;
                                   }

                                   int  *big(int  &x,  int  &y)
                                   {  if    (x>y)
                                          return(&x);




          240                               LOVELY PROFESSIONAL UNIVERSITY
   242   243   244   245   246   247   248   249   250   251   252