Passing and Returning of Objects To and From Functions
Submitted by Muzammil Muneer on Tuesday, October 21, 2014 - 12:04.
Passing and Returning of objects to/from Functions
In this part you will learn: 1. How to pass objects to a function 2. Arrow operator 3. Syntax of passing an object 4. How to return an object from a function 5. Syntax of returning an object 6. Program with object passing and returning 7. Output Passing an Object to Function Just like built-in data types like int, char, float , user-defined class’s object can be passed to a function in c++. A little difference is that in case of classes, function have access not only to the functions that are passed but also to the caller object of the function. This caller object’s members can be either directly accessed in the function or by using “this” pointer. “this” pointer is a special type of pointer which points to the caller object of the function in which we are currently in. To access members of the caller object using “this” pointer we use arrow operator (->). Just like we use dot operator for accessing members of object of a class, we use arrow operator to access the members of an object being pointed by a pointer. We will see its use in the program at the end of this topic. Let us come back to the passing of objects, just like normal data types. The objects are passed in two ways. • Pass by value • Pass by reference In pass by value, a separate copy of the object is created using copy constructor. As discussed earlier, it is one of the cases when copy constructor is called. So if we have pointers as the members of object, it is a good programming practice to pass object by value and perform deep copy in the constructor. This is because the function may perform some changes like deletion of member pointer of passed object which will cause loss of data and later when the destructor of the object will be called, it will try to delete memory which is already been deleted. Note: If there is no constructor available and we an object by value, bit by bit copy would be done, which will work fine in case of member variables other than pointers. The other way of passing an object is, pass by reference. In pass by reference, there is no copy created but the reference of original object is passed in the function. Which means that any changes performed in the function on that object will have its effect on the passed object too. Syntax return_type function_name(Class_Name &(as requirement) Object_Name1, Class_Name Object_Name2, Class_Name Object_Name2 ) { //function operations } We can pass ass many parameters to a function as we want. Returning an Object from a Function We can return an object from a function by simply writing the return_type as the name of the class of which that object is made of. But there is a little issue in it. As we know that for objects, as soon as their scope ends, their respective destructors are called. While after calling the return function, the scope of the object ends, its destructor is called before reaching back to the calling point of the function we are in. So the object which we want to return gets deleted even before reaching to the caller of the function. Again this is the case when copy constructor is called for that object. i.e. whenever an object is returned. Its copy constructor is called which copies the data before the calling of destructor. In this way our object returned safely. Syntax Class_name_of_object_we_want_to_return function_name() { //function operations return Object_we_want_to_return; } Program with Object passing and returning In this example I will illustrate how static data member work, for this purpose, let us take example of sets. Basic Step:- #include<iostream>
- #include<conio.h>
- using namespace std;
- class fraction
- {
- private: //access specifier
- int num;
- int denom;
- public:
- void getfraction(){
- cout << "Fraction is: " << num << "/" << denom;
- }
- void setfraction(){
- cout << "Enter numerator: ";
- cin >> num;
- cout << "Enter denominator: ";
- cin >> denom;
- }
- fraction addfraction(fraction f){
- fraction sum;
- sum.num = (this->num*f.denom) + (this->denom*f.num);
- sum.denom = this->denom*f.denom;
- return sum;
- }
- };
- int main()
- {
- fraction f1; //initializing a variable f1 of type fraction
- fraction f2; //initializing a variable f2 of type fraction
- f1.setfraction();
- f2.setfraction();
- fraction f3;
- f3 = f1.addfraction(f2);
- f3.getfraction();
- }
Add new comment
- 66 views