Returning Object
A function can also return objects either by value or by reference. When
an object is returned by value from a function, a temporary object is created
within the function, which holds the return value. This value is further
assigned to another object in the calling function.
Synatx:
classname functionname(argument list)
{
function of body;
}
Program:
#include<iostream.h>
#include<conio.h>
class MCA
{
int a,b;
public:
void getdata(int, int);
void disp();
MCA sum(MCA,MCA);
};
void MCA::getdata(int
x,int y)
{
a=x;
b=y;
}
void MCA::disp()
{
cout<<a<<b;
}
MCA sum(MCA u,MCA v)
{
int z;
z=v.a + u.b;
z=v.b + u.a;
}
void main()
{
MCA s1,s2,s3;
s1.getdata(100,2);
s2.getdata(200,5);
s3.sum(s1,s2);
cout<<s1.disp();
cout<<s2.disp();
cout<<s3.disp();
getch();
}
No comments:
Post a Comment