Object as Function Arguments

Like and other data type, an object may be used as function argument. this can be done in to ways:
  1. 1. a copy of the entire object is passed to the function 
  2. 2.only the address of the object is transferred to the function
the first method is college pass by Value. since a copy of the object is passed to the functions .any changes made to the object inside the functions do not affect the object used to call the function the second method is college pass- by- Reference. when an address of the object is passed the called function works directly on the actual object used in the call. this means that any changes made to the object inside the function will reflect in the actual object . the pass- by Reference method is more efficient since it requires to pass only the address of the object and not the entrie object
this program below given illustrates the use of object as function arguments it perform the additional of time in the hour and minutes format.
#Include <iostream.h>

Class time   
{
         int    hours;
    int minutes;
public:  
  void gettime(int h, int m)   
{hours = h; minutes = m; }
void puttime(void) 
{
cout  << hours <<  " hours and ";  
cout << minutes  << " minutes "<< "\n";  
}
  void sum(time, time); \\ declaration with objects as argument
};
Void time ::  sum(time t1,  time t2)  \\t1, t2 are objects
{
minutes = t1.minutes+ t2.minutes!
Hours = minutes/60;
Minutes = minutes%60;
Hours = hours + t1.hours + t2.hours;
}
int main()
{ time Tl, T2, T3;  
T1.gettjme(2,45); \\ get T1
T2.gettime(3,30); \\ get T2
T3.sum(Tl,T2); //  T3=T1+T2  
Cout << " T1 = "; T1.puttime(); \\ display T1
Cout << " T2 = "; T2.puttime(); \\ display T2
Cout << " T3 = "; T3.puttime(); \\ display T3
return 0;

No comments:

Post a Comment

सवाल पूछने के बाद "Notify me" आप्शन को ✔(Right Mark) करे, ताकि आपके पूछे गये सवाल का जवाब मिलने पर आपको सुचना मिल जाये:-