#9-Write a program to show the concept of object as a function argument in C++ with explanation

Note that there are 2 #9. i did this deliberately.

Objects as a function argument-:

In C++, like other data-type, objects can be passed as an argument in the function.

And, we can either pass 1 object as an argument, 2 objects as an argument, 3 objects as an argument…i.e to say n number of objects as an argument.

So, let’s discuss a program in this topic.

The question of the upcoming program is as follows.

(Q1) Write a program to add and subtract two types time objects taking time as a function argument and returning object by the function.

The program source code is written as follows-:

Now let us discuss about the program flow. How are things going with the program?

It all starts with the main function. So, at first let’s go to the int main function. Then, inside the main function, we can see that, at the beginning, we created objects t1,t2,t3,t4. We created those objects because we have 2 things to do with 2 variables and 2*2=4.

Then, we gave the thing-:

Enter the start time through the help of cout<< operator.

Note that endl is used to end the line, i.e going to new line in C++.

Then, we took the input for the t1 object as-:


After that, we go on taking input for the t2 object.

Now, we wanted to add them. And for that, we did-:


Then, we displayed it through-:


likewise, for subtracting the time, same procedure was followed.

Whatever that is told above is not so important as what is coming below(just below).

We created an addtime function inside the class, and in that function we passed some parameters, and look, look, look, what are they????

Take a deep breath and look at the parameters.

They are (time t1,time t2).

Whenever, we needed to send the integers, we did as (int a,int b). Whenever we wanted to send float values, we did as (float a, float b). Now, we want to pass the objects, ie we want to do something for object(yes really something!!!). And, we know that the objects are the sons of class. i.e to say that the class contains objects. So, this way, we will be able to give answer of the question because we see an object as function argument being passed here.

So, inside that addtime function, which is non-return type (if you are unclear about the concepts of functions, you can always ask me through comments; because that is one big thing in programming. I am assuming in this tutorials, that you are clear about the concepts of functions). So, we at first calculated the sum of seconds.

Then, we calculated the sum of minutes. Now, here is where things go interesting and confusing.

We have done second/60 and added with minute. Why?

Because 1 minute is also equal to second/60.

You may thing that 1 minute, is also then 1/60 hour…Hmm….but, that is not the case. We need to get the lowest quantity at first. Then add that lowest quantity. After that, take a quantity just greater than that lowest quantity and add them. And also, add how the lowest quantity relates with the second lowest quantity and again finally, update the lowest quantity.

So, we can see that here we updated the lowest quantity second as


Ok, then, after that we did the sum for hours.

In that, we did add two hours as t1.hour+t2.hour and also added a next thing, which was minute/60.

Finally, we updated minute as


This way, we can calculate the remaining minutes after being summed up with hour.

Also, by second=second%60; we can find the remaining minutes after being summed up with minutes.

So, things are getting easier, right? And, they should!!!

Again let’s go and look deep inside the subtracttime function. This function will be used to subtract the time as the name implies. So, at first, inside this function, we initialized hour=0;



now, look at this case, for example the user inputs the second for t2 object lesser than t1 object, then what!!! Shall we show – seconds???? What the hell???? Do negative time exists??????WHATTTT!!!!IS TIME TRAVEL POSSIBLE THEN?????????????????????

So, till now as time travel has not been invented, we will reject that theory of negative time. So, instead we will go towards following another approach.

If second is found to be lesser than zero, then we will add 60 seconds to the second….OHHHH….and, you must remember the basic physics of the universe. It is What goes in=What comes out. So, basically, we added 60 seconds so, we must subtract 60 seconds=1 minute in order to be remained at balance. In order to follow the law of universe(which works pretty much well in the programming), we need to subtract 1 inute=60 seconds. Now, a thing comes in mind, can’t I subtract it in second itself? Yes, you can if you are mental and psycho and mad.

I hated you doing it because that makes no sense at all. You are adding 60 seconds, so that the seconds become greater than zero and again you are subtracting them. SHIT!!! That is nonsense. So, you should be subtracting the 1 minute from minute.

And after that, we did calculate minute’s updated version as-:


the meaning of doing plus minute is nothing but, creating a relationship of second with minute. In the previous addtime function, we had done it differently as we added …+second/60 in minute. Here, that was not possible, as second’s difference could also be equals to less than zero. So, second was balanced by minute. i.e we can say that-:


after that, we updated minute by balancing it as minute=minute-1.

So, that minute was added in the new minute, which is nothing but seconds subtraction balanced.

After that, we went into thinking that what will happen if minute becomes negative(minute<0); we found out that, we needed to add 60 minutes by borrowing from hour, and decreased the hour by 1 as: hour-1.

So, in this way this program runs itself.

I hope this is very much clear.

I don’t think that anything more than this can be said to this program. I gave my best to describe about this in the most interactive way. I hope this helps you to learn programming faster and clear.

 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,

A short request-:

As you can see that, I am giving a lot of information about this material for free. You may say that, you earn through advertisements, but the amount is negligible through Google Adsense, that is not even helping me to buy a coffee in a month. It takes a lot of time an effort to create this content. I need to think, prepare for long hours ,and plan to present it in the most interactive way. The programming subject in itself is a so vast subject. So a lot of hard work is required to do this. So, if you people, want me to keep going, help me by buying amazon products from the links above. I can’t even take a donation because I don’t have PayPal or any payment system here in my country. So, please help me to continue doing this for a long time. buying one amazon product in 2 months will help!!! I know this is not a good offer, to ask you to buy something, but I think it is the only option left for me. Have you any idea about this? Then please share.

Leave a Comment

Your email address will not be published. Required fields are marked *

Do NOT follow this link or you will be banned from the site! Scroll to Top