Docstoc

Passing objects to a function in c++

Document Sample
Passing objects to a function in c++ Powered By Docstoc
					                Objectives

On completion of this period, you would be able to
  know…
• Passing objects to a function.

• Types of objects.

• Pass by value.

• Pass by reference.
                       CM505.16                      1
             RECAP

• What is meant by pass by value?

• What is meant by pass by reference?




                CM505.16                2
      Passing object to function

• An object may be used as a function
  argument.

• An object can be passed to a function.




                  CM505.16                 3
Types of parameter passing mechanisms

• Pass by value
• Pass by reference




                      CM505.16      4
              Pass by value

• A copy of the object is passed to the function.

• Any modifications made to the object inside
  the function is not reflected in the object used
  to call the function.




                       CM505.16                      5
        program on pass by value
#include<iostream.h>
class time
{
  private:
       int hours;
       int minutes;
 public:
    void gettime(int h, int m)
      {
         hours=h;
         minutes=m;
         }
                   CM505.16        6
     program on pass by value

     void puttime(void)
      {
          cout<<hours<<“hours and”;
          cout<<minutes<<“minutes”<<“\n”;
         }
         void sum(t1,t2);
};



                CM505.16                    7
     program on pass by value


void time::sum(time t1, time t2)
{
       minutes=t1.minutes+t2.minutes;
       hours=minutes/60;
       minutes=minutes%60;
       hours=hours+t1.hours+t2.hours
  }



                 CM505.16               8
          program on pass by value
void main()
 {
      time t1,t2,t3; cout<<“t1=“; t1.puttime();
      t1.gettime(2,45); //get t1
      t2.gettime(3,30); // get t2
      t3.sum(t1,t2);      //t3=t1+t2;
      cout<<“T1=“; t1.puttime(); //display t1
      cout<<“T2=“; t2.puttime(); //display t2
      cout<<“T3=“; t3.puttime(); //display t3
   }
                       CM505.16                   9
 Simple program on pass by value

OUTPUT:


          T1 = 2 hours and 45 minutes
          T2 = 3 hours and 30 minutes
          T3 = 6 hours and 15 minutes




                    CM505.16            10
          Pass by reference
• When an address of the object is passed, the
  called function works directly on the actual
  objects used in the call.

• Any changes made to the object inside the
  function will reflect in the actual object.




                    CM505.16                     11
Example :-reference variables

  void swap(int & a, int & b)
    {
        int t = a;
        a = b;
        b = t;
    }
     swap(x,y); // call by passing
                   // reference variables


                   CM505.16                 12
          Example :-call by reference

void swap1(int *a, int *b) // function definition
  {
     int t;
      t = *a; // assigning the value at address a to t
     *a = *b; // put the value at b into a
     *b = t; // put the value at t into b
  }
  swap1(&x,&y); // call by passing
                // addresses of variables

                       CM505.16                      13
               SUMMARY
•   Passing objects to a function.

• Parameter passing mechanisms:

          • Pass by value.

          • Pass by reference.

          • Reference variables


                      CM505.16       14
  Frequently Asked Questions

1. What is call by value?

2. What is call by reference?

3. Explain passing of objects to a function?




                    CM505.16                   15
                    Quiz

1. An object may be used as a _________.




                    CM505.16               16
                     Quiz

1. An object may be used as a function argument.




                     CM505.16                  17
                      Quiz

2. In call by value _____of the object is passed
   to the function.




                      CM505.16                     18
                      Quiz

2. In call by value copy of the object is passed to
   the function.




                      CM505.16                    19
                       Quiz

3. Any changes made to the object inside the
   function will effect in the ______if the object is
   passed by reference.




                       CM505.16                     20
                        Quiz

3. Any changes made to the object inside the
   function will effect in the actual objects.if the
   object is passed by reference.




                        CM505.16                       21
THANK YOU…




     CM505.16   22

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:3/10/2012
language:
pages:22