Page 86 - DCAP404 _Object Oriented Programming
P. 86
Unit 4: Classes and Objects
You should see the following output. Notes
Enter a value for x 15
Enter a value for x 50
the value of x belonging to object int1 is 15
the value of x belonging to object int2 is 50
after swapping
the value of x belonging to object int1 is 50
the value of x belonging to object int2 is 15
4.3.2 Returning Objects
Just as a function takes an object as its argument, it can also return an object. The following
program illustrates how objects are returned. The program declares a class integer representing
an integer variable x and defines a function to calculate the sum of two integer values. This
function finally returns an object which stores the sum in its data member x.
#include<iostream.h>
#include<conio.h>
class integer
{
int x;
public:
void getdata(int x1)
{
x = x1;
}
void disp()
{
cout << x;
}
integer sum(integer int2)
{
integer int3;
int3.x = x + int2.x;
return(int3);
}
};
main()
{
LOVELY PROFESSIONAL UNIVERSITY 79