#10-Write a program to show the concept of “Returning Objects by Function”

c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

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.

Its output will come as below-:

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

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.

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

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. Try to dry run this program as I did above. You will find that everything is SAME.

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

Homework-:

I haven’t given homework since last week. So today I am giving it. Don’t take it as a burden, instead take it as an opportunity. When I learn something from internet/online, I become happy if that person gives me some homework with solution. While giving solution in programming is giving the source code, so, I will only give a hint on how to solve it. And if you can’t solve it anyway, you can ask below in the comment section or in the facebook page. So, alright?

Write a program to add and subtract two metric distance objects, taking object as a function argument, and returning object by the function.

Hint-:

1) Metric means metre and kilometer.

I still can’t figure it out!

2) All processes are same as above.

Share this and have a good day!

Facebook

2 Comments

  1. this is heavily useful for me, i read this material before class, after class.. thanks so much. You deserve much more views and much more success in this field.

Leave a Reply

Your email address will not be published.


*