**Returning objects by the function-:**

In C++, called functions can return the objects to the calling function.

The question is-:

**Write a program to show the concept of returning objects by the function-:**

And the source code will follow as below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include<iostream> using namespace std; class distances { int feet,inch; public: void input() { cout<<"\nenter the values for feet and inches"; cin>>feet>>inch; } void display() { cout<<feet<<"\nfeet and"<<inch<<"\ninch"; } distances adddistance(distances d1,distances d2) { distances temp; temp.inch=d1.inch+d2.inch; temp.feet=d1.feet+d2.feet+temp.inch/12; temp.inch=temp.inch%12; return temp; } distances subtractdistance(distances d1,distances d2) { distances temp; temp.feet=0; temp.inch=d2.inch-d1.inch; if(temp.inch<0) { temp.inch=temp.inch+12; temp.feet=temp.feet-1; } temp.feet=d2.feet-d1.feet+temp.feet; return temp; } }; int main() { distances d1,d2,d3,d4; cout<<"\nenter start distance"; d1.input(); cout<<"\nenter the ending distance"; d2.input(); distances d5=d3.adddistance(d1,d2); cout<<"\nthe addition of two distances is"; d5.display(); distances d6=d4.subtractdistance(d1,d2); cout<<"\nthe subtraction of two distance is"; d6.display(); return 0; } |

Its output will come as below-:

So, let’s discuss about what is going inside the program. And be sure that at the end of this reading, you will be able to understand the concept of returning objects by functions in a very deep way.

It all starts with the int main() function. *Note that we are doing programming in code::blocks, so I am saying the int main(). If you are still using turbo c for programming( which I also sometimes do for using the graphics.h), then you will be able to use void main() there also. If you don’t find code::blocks, codelite will also work here.*

So, at first, we created d1,d2,d3,d4 objects. Then we asked the user to enter the start distance.

Then we will head towards the input function, by d1.input().

Then, we will go to the input() function. Inside the input function**(which is inside the class so we created an object and called it)**. As seen inside that input function, we will take the values for feet and inch and store the values on feet and inch respectively.

After that we do the same thing for the next object d2.

Then, we go on doing the next thing for solving the above problem and it is to add the two distances d1 and d2. For that, we will need an object which will hold the sum of d1 and d2. Hence, you can see that I created an object called as “distances d5”. That distances d5 will do nothing but add the d1 and d2 objects as d3.adddistance( d1,d2). By d3.etc, what we did was to call that function and by d4=etc what we did was store that value in d4.

So, let’s look what programming will be happening inside the adddistance function.

You can see that we have defined and declared that function inside the class.

**Did you notice the return type of that function? Look LOOK…LOOK DEEPLY AT IT!!! We have created a return type of that function as CLASS NAME because we are going to return objects. If we had to return float values, then our return type will be float instead of distances. If we had to return integer values, then our return type will be int instead of distances.**

So, I hope you have understood this!

Then inside the function, we can see that we created an **object temp. What we did here was class name object name. Isn’t the thing same as we did in int main function??? LOOK…We had created objects in int main function as distances d1,d2…and here in distances temp, we are doing the same thing.**

Then, we at first calculated the sum of inch values by d1.inch+d2.inch and store that value to temp.inch.

Again we added feet values and added the inch equivalent feet to it. We added d1.feet, d2.feet, and then added temp.inch/12 to it.

Now, we will need to update the remaining inch values. We did it as temp.inch=temp.inch%12.

Finally, we returned temp where everything, i.e the sum of feet and inch was stored in **temp.**

Again, we will go back to the int main() function. Inside that function ,we can see that we showed the addition of two time objects output, by calling the display function as d5.display(). Inside the display function, we shown the values of feet and inch. i.e if we had sent 1 feet 1 inch in d1; 1 feet 1 inch in d2; then we we will get 2 feet 2 inch because the **temp, that has been returned through adddistance function will go and store in d5 object, and we are showing the d5 display output as d5.display. **

After that, in the int main() function, we can see that we created a new object d6 and asked that object to calculate the subtraction of two time objects as’ d6=d4.subtractdistance(d1,d2). Now, let’s go deeper inside the subtractdistance function. Inside that function, as we can see, we created an object temp.

Then, we initialized temp.feet=0, and it is not necessary*( I mean to say you can check without initializing the temp.feet=0 and check that program runs or not). *

So, after that we calculate the differences of two inch objects and stored it in temp object. We did it by-:

d2.inch-d1.inch;

So, here is a chance of that the above quantity may become negative. *And the distance negative is no meaning. *So, we want to check that condition and do something to get rid of that condition and save our program from getting a hell lots of errors.

So, we check the condition if temp.inch<0; and we added 12 inch by taking borrow from feet. As we took a borrow from feet, we need to subtract 1 feet from temp.feet and that is exactly what has been done inside that function.

As soon as we come outside of the loop, we see that, we calculated temp.feet as the differences between two feet objects and added `the temp.feet to it(because the temp.feet was used to balance inches so it is equivalent to balanced inch itself). After that, we returned temp object. As in the above figure, you can visualize what is going with this program.

Finally, we will go back to the main function and inside the main function, we see that we displayed the subtraction of two distances objects as d6.display. And, in d6, the values of subtracted feet, inches has been stored, so we will invoke display function using the d6 object where temp has been stored as a subtracted value.

Then finally we ended the program.

I hope this is very much clear.

If there are confusions, then I am ready here to help you.

Now, let us further study the concept of returning object as function argument in detail.

Let’s solve another programming problem related to it. The question is as-:

**(Q) Write a program to add and subtract two type time object by taking time as an argument and returning object by function.**

The source code is as below. This is the same program as above. Difference is that the above program is for distance, and the below program is for time. Try to dry run this program as I did above. You will find that everything is SAME.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #include<iostream> using namespace std; class time { int hour,minute,second; public: void input() { cout<<"\nenter the value of hour,minute and second\n"; cin>>hour>>minute>>second; } void display() { cout<<"hour\t"<<hour<<minute<<"minute\t"<<second<<"second\t"; } time addtime(time t1,time t2) { time temp; temp.second=t1.second+t2.second; temp.minute=t1.minute+t2.minute+temp.second/60; temp.hour=t1.hour+t2.hour+temp.minute/60; temp.second=t1.second+t2.second; temp.minute=t1.minute+t2.minute+temp.second/60; temp.hour=t1.hour+t2.hour+temp.minute/60; temp.minute=temp.minute%60; return temp; } time subtracttime(time t1,time t2) { time temp; temp.hour=0; temp.minute=0; temp.second=t2.second-t1.second; if(temp.second<0) { temp.second=temp.second+60; temp.minute=temp.minute-1; } temp.minute=t2.minute-t1.minute+temp.minute; if(temp.minute<0) { temp.minute=temp.minute+60; temp.hour=temp.hour-1; } temp.hour=t2.hour-t1.hour+temp.hour; return temp; } }; int main() { time t1,t2,t3,t4; cout<<"\nenter the start time\n"<<endl; t1.input(); cout<<"\nenter the stop time\n"<<endl; t2.input(); time t5=t3.addtime(t1,t2); cout<<"\nthe addition of 2 time objects is\n"; t5.display(); cout<<endl; time t6=t4.subtracttime(t1,t2); cout<<"\nthe subtraction of two time objects is\n"; t6.display(); return 0; } |

Its output will come as below(neglect the formating, i tried to keep it simple)