• Similar to passing primary data-type variables, we can also pass object as argument to the function. Objects can be passed by two methods:
  1. Call by value: In this method a copy of entire object is created to pass into another function. When we make changes in the object inside calling function then only local copy of the object will be affected, there is no change in called function. Both copies of the object will be identical.
  2. Call by reference: In this method no separate copy of the object is created, instead we are passing address of object to the function. When an object is modified inside the calling function then the actual object is also affected in the called function. Call by reference method can be used by passing argument via reference variable or pointer.
  • Object as function argument is normally used to communicate between two objects.
  • Consider the program in which we have class ‘Time’. This class has two members variables one is time in hour and another is time in minutes.
  • We are creating two object ‘Theory’ and ‘Practical’ from this class and at last I want to print the total theory and practical time. this can be achieved with the help of passing object to the function as follow:
/* program to demonstrate passing object as function argument. */

#include "iostream.h"
#include "conio.h"
 
class	Time
{
	   int	hour;
	   int 	minute;
   public :
            void total(Time);    // function declaration
       
};

void Time :: total(Time tmp) // Receive object as argument
{
   int hr, min;
   min = minute + tmp.minute;
   hr = hour + tmp.hour + min/60;
   min = min % 60;
   cout << hr <<"Hours and "<< min << "Minutes"<<endl ;
}
void main( )
{
    Time theory, practical;
    theory.total(practical);	// Passing object as argument
}
  • In above program we have member function ‘total’ which receive the object of type ‘Time’ as argument.
  • From the main function we are passing object ‘practical’ as argument.
  • In main function ‘total’ is member function which is called by ‘theory’ object.
  • So in the ‘total’ function we can access the ‘hour’ and ‘minute’ of ‘theory’ directly while we can access the ‘hour’ and ‘minute’ of ‘practical’ object by passing entire object.

Returning Object:

  • As we can pass entire object as an argument, similarly we can return object from the function. We can return entire object from function by specifying its return type as class name just like primary data-types.
  • Consider the program in which we are creating class ‘Box’ having two members-height and width. We are creating two object ‘B1’ and ‘B2’ from this class.
  • From this object we are creating new object ‘B3’ whose height and width is the sum of height and width of ‘B1’ and ‘B2’. To achieve this, we have to specify return type as class.
/* program to demonstrate the use of object as return type. */

#include "iostream.h"
#include "conio.h"

class	Box
{
	   double height;
	   double width;
   public :
            Box sum (Box);    // function declaration with return type as class.
            void read ();    // function to read data. 
            void show ();
       
};

Box Box :: sum(Box tmp) // Receive object as argument
{
   Box b;
   b.height = height + tmp.height;
   b.width = width + tmp.width;
   
   return b;      // return Box object b which is sum of b1 + b2.
}
void Box :: read()
{
   cout<< " Enter height and width of Box" <<endl;
   cin>> height << width ;
}
void Box :: show ()
{
   cout << height<<" Height and "<< width << "Width" <<endl ;
}
void main( )
{
    Box b1, b2, b3;
    b1.read();
    b2.read();
    b3 = b1.sum(b2) ;       // receive new object in b3.
    b3.show();              // show content of b3.
    getch();
}

Array of Object:

  • When we need to deal with large amount of data, we declare an array of primary-data types such as int, float, char, etc. Similarly, When we need to deal with multiple objects, we can create an array of object just like simple array of primary data-type. It saves us from lots of headache of creating separate and unique objects. The format of declaring array of object is as follow:
class_name object_array_name [array_size];
  • We can access the different object from the array by using its index or subscript as follow:
object_array_name [0];    // first object element
object_array_name [1]      // second object element    
...
  • And we can access data member and member function of the object by using dot operator.
object_array_name [0] . data_member ;
object_array_name [0] . member_function() ;
  • When we declare array of the object, for each object data member has given different memory allocation and all the member functions are shared by all the object so there is only one copy of the member function.
  • Consider following example in which we are creating array of Box with size 5 for the previous program.
/*program to demonstrate array of object. */

#include "iostream.h"
#include "conio.h"

class	Box
{
	   double height;
	   double width;
   public :
            Box sum (Box);    // function declaration with return type as class.
            void read ();    // function to read data. 
            void show ();
       
};

Box Box :: sum(Box tmp) // Receive object as argument
{
   Box b;
   b.height = height + tmp.height;
   b.width = width + tmp.width;
   
   return b;     // return Box object b.
}
void Box :: read()
{
    cout<< " Enter Box height and width :" <<endl;
    cin>> height >> width ;
}
void Box :: show ()
{
   cout << height<<" Height and  "<< width << "Width " <<endl ;
}
void main( )
{
   Box b[5];            // array of box object with size 5.
   int i;
    
   for(i = 0; i < 5 ; i++ )
   {
         b[i].read();
   } 
     // show data for all objects.
   for(i = 0; i < 5 ; i++ )
   {
        b[i].show();      
   }
   getch();
}