Page 83 - DCAP404 _Object Oriented Programming
P. 83
Object-oriented Programming
Notes They occupy space in memory that keeps its state and is operated on by the defined operations
on the object. Each object contains data and code to manipulate the data. Objects can interact
without having to know details of each other data or code.
4.3.1 Objects as Function Arguments
Like any other data type argument, objects can also be passed to a function. As you know
arguments are passed to a function in two ways:
1. by value
2. by reference
Objects can also be passed as arguments to the function in these two ways. In the first method, a
copy of the object is passed to the function. Any modification made to the object in the function
does not affect the object used to call the function. The following Program illustrates the calling
of functions by value. The program declares a class integer representing a integer variable x.
Only the values of the objects are passed to the function written to swap them.
#include<iostream.h>
#include<conio.h>
class integer
{
int x;
public:
void getdata()
{
cout << “Enter a value for x”;
cin >> x;
}
void disp()
{
cout << x;
}
void swap(integer a1 , integer a2)
{
int temp;
temp = a2.x;
a2.x = al.x;
a1.x = temp;
}
};
main()
{
76 LOVELY PROFESSIONAL UNIVERSITY