Objects
as Function arguments
Objects
can also be passed as arguments to a member function just like any other data
type of C++. Objects can also be passed in both ways
(1)
Pass By Value
(2)
Pass By Reference
When
an object is passed by value the function creates its own copy of the object
and works on it . Therefore any changes made to the object inside the function
do not affect the original object.
we
can assign one object to another object, Similar to structure object
#include <iostream>
using namespace std;
class Sample
{
private:
int num;
public:
void set (int
x)
{
num = x;
}
void pass(Sample obj1,
Sample obj2) //objects are passed
{
obj1.num=100; // value of the object is changed inside
the function
obj2.num=200; // value of the object is changed inside
the function
cout<<"\n\n Changed value of object1
"<<obj1.num;
cout<<"\n\n Changed value of object2
"<<obj2.num;
}
void print( )
{
cout<<num;
}
};
int main()
{
//object declarations
Sample s1;
Sample s2;
Sample s3;
//assigning values to
the data member of objects
s1.set(10);
s2.set(20);
cout<<"\n\t\t Example program for pass by
value\n\n\n"; //printing the values before passing the object
cout<<"\n\nValue
of object1 before passing";
s1.print();
cout<<"\n\nValue
of object2 before passing ";
s2.print();
passing object s1 and s2
s3.pass(s1,s2);
//printing the values
after returning to main
cout<<"\n\nValue
of object1 after passing ";
s1.print();
cout<<"\n\nValue
of object2 after passing ";
s2.print();
return 0;
}
Example program for PASS BY VALUE
Value of object1 before passing 10
Value of object2 before passing 20
Changed value of object 1 100
Changed value of object 200
Value of object 1 after passing 10
Value of object 2 after passing 20
In
the above program the objects s1,and s2 are passed to pass( ) method. They are
copied to obj1 and obj2 respectively. The data member num’s value is changed
inside the function. But it didn’t affected the s1 and s2 objects data member.
When
an object is passed by reference , its memory address is passed to the function
so the called function works directly on the original object used in the
function call. So any changes made to the object inside the function definition
are reflected in original object.
#include <iostream>
using namespace std;
class Sample
{
private:
int num;
public:
void set (int x)
{
num = x;
}
void pass(Sample &obj1,
Sample &obj2) //objects are passed
{
obj1.num=100; // value of the object is changed
inside the function
obj2.num=200; // value of the object is changed
inside the function
cout<<"\n\n Changed value of object1
"<<obj1.num;
cout<<"\n\n Changed value of object2
"<<obj2.num;
}
void print()
{
cout<<num;
}
};
int main()
{
clrscr();
//object declarations
Sample s1;
Sample s2;
Sample s3;
//assigning values to
the data member of objects
s1.set(10);
s2.set(20);
cout<<"\n\t\t
Example program for pass by reference\n\n\n";
//printing the values
before passing the object
cout<<"\n\nValue
of object1 before passing"; s1.print();
cout<<"\n\nValue
of object2 before passing "; s2.print();
//passing object s1
and s2 s3.pass(s1,s2);
//printing the values
after returning to main
cout<<"\n\nValue
of object1 after passing "; s1.print();
cout<<"\n\nValue
of object2 after passing ";
s2.print();
return 0;
}
Example program for PASS BY REFERENCE
Value of object1 before passing10
Value of object 2 before passing 20
Changed value of object1 100
Changed value of object2 200
Value of object1 after passing 100
Value of object2 after passing 200
In
the above program the objects s1,and s2 are passed as reference to pass( )
method. So obj1 and obj2 become reference(alias name) for s1 and s2
respectively. The data member num’s value is changed inside the function
affected s1 and s2 objects data member.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.