11
if(m >= n)
return(m);
else
return(n);
}
void set :: input(void)
{
cout << "Input value of m and n"<<"\n";
cin >> m>>n;
}
void set :: display(void)
{
cout << "largest value=" << largest() <<"\n";
}
int main()
{
set A;
A.input();
A.display();
return 0;
}
Object as function arguments:
Like any other dta type, an object may be used as a function argument. This can be done in two ways.
i)
A copy of the entire object is passed to the function.
ii)
Only the address of the object is transferred
to the function
First method is pass-by-value. Since a copy of the object is
passed to the function,
any changes made to
the object inside the function do not affect the object used to call the function.
The second method is
called pass-by-references. 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 entire object.
Example program of object as function argument:
Example 1
14
//passing object d1 and d2
d3.sum(d1,d2);
//printing the values
d1.print();
d2.print();
d3.print();
return 0;
}
Output:
Dostları ilə paylaş: