# How do "min" and "hour" get their values in this C++ code?

• Comp Sci
• shivajikobardan
In summary: This line creates a new Time object that is equivalent to t1. t1.hour and t1.min now have the values that t1.add(t2) would have returned.
shivajikobardan
Homework Statement
How does "min" and "hour" get their values in this C++ code?
Relevant Equations
How does "min" and "hour" get their values in this C++ code?

Full code here:
C++:
#include<iostream>
using namespace std;class Time
{
public:
int hour;
int min;
Time()
{
hour=min=0;
}
Time(int h,int m)
{

hour=h;
min=m;
}
{
Time t1;
int m=min+t.min;
t1.hour=hour+t.hour+m/60;
t1.min=m%60;
return t1;

}
};

int main()
{
Time t1;
Time t2(8,40);
Time t3(5,30);
cout<<t1.hour<<" "<<t1.min;
return 0;
}
<Moderator's note: Please use CODE tags when posting code.>

As shown in the figure above, some of my confusions are clear. But what I fail to understand is how "min" and "hour" gets its value from t2(8,40). I am sorry if this is a too basic question, but I didn't understand so I am asking.
If I am missing any prerequisites to this topic, do tell me about them, that'd help me more for self learning.

The class has what are called "constructors" that allow to set values when an object is created. See https://www.w3schools.com/cpp/cpp_constructors.asp

In this case, line 33 creates an object t1 with hour=min=0 by calling the constructor starting line 10 (called the "default constructor"). Lines 34 and 35, because of the presence of arguments, will call the constructor starting at line 14.

shivajikobardan
DrClaude said:
The class has what are called "constructors" that allow to set values when an object is created. See https://www.w3schools.com/cpp/cpp_constructors.asp

In this case, line 33 creates an object t1 with hour=min=0 by calling the constructor starting line 10 (called the "default constructor"). Lines 34 and 35, because of the presence of arguments, will call the constructor starting at line 14.
thanks for the input. but my question remains. how does "min" and "hour" get their values?

This is my question. t.hour means t3.hour, t.min means t3.min I understand.
But how does min=t2.min and how does hour=t2.min?
I don't understand them. Should I just memorize that when we do t2.add(t3) we can access t2.hour and t2.min by just "hour" and "min"? or is there any concept involved behind it?

Last edited:
shivajikobardan said:
Should I just memorize that when we do t2.add(t3) we can access t2.hour and t2.min by just "hour" and "min"? or is there any concept involved behind it?
I wouldn't call it "memorizing," but indeed when a method is applied to an object, inside the method the contents of the object are directly accessible. The line
C++:
        int m=min+t.min;
inside the add method would be equivalent to the line
C++:
        int m=t2.min+t3.min;
in the main code when the method is applied to the t2 object,
C++:
    t2.add(t3);

I honestly hate memorizing. I find all my doubts end up "memorization problems".

An IDE will usually colour member variables, so hour and min would be a different colour from regular variables. That should hint to you that they mean "the member variable of the object this method was called on".

DrClaude
@shivajikobardan, your Time class has the following:
• Two data members, hour and min
• Two constructors

The default constructor creates a Time instance with hour and min initialized to 0. The other constructor creates a Time instance with hour and min initialized to a pair of specified values.

Here are the first four lines in main(), with a description of what each line is doing.
C++:
    Time t1;
Time t2(8,40);
Time t3(5,30);
t1=t2.add(t3);
Line 1 - The default constructor is called to create an object named t1. t1.hour and t1.min are initialized to 0.
Line 2 - The constructor with two parameters is called to create an obect named t2. t2.hour is initialized to 8 and t2.min is initialized to 40.
Line 3 - Is nearly identical to the previous line except that the object is t3, with t3.hour initialized to 5 and t3.min initialized to 30.
Your drawing in post #3 is an accurate representation of what these three lines do, except I can't quite read what you have for t2 as the font is so small.

Line 4 requires a bit more explanation. Here's the code for the add() member function.
C++:
Time add (Time t)
{
Time t1;
int m=min+t.min;
t1.hour=hour+t.hour+m/60;
t1.min=m%60;
return t1;
}
Line 4 in main() calls the add() function with a parameter of t3. Recall that t3.hour == 5 and t3.min == 30. Inside the add() function, the formal parameter t is a reference to t3, meaning that t.hour == 5 and t.min == 30.
In the body of add(), the local variable t1 is initialized using the default constructor, so t1.hour and t1.min are both initialized to 0.

The second and third lines in add() require some explanation, as it's not obvious what is happening here. Here is how add() was called in line 4 of main(): t1=t2.add(t3);

Member functions (like add() here) have an implied first argument - the object that is calling the method. In this case, it's the t2 object. Inside the add() function, the identifier hour means t2.hour and min means t2.min.

The second line of the body of add() calculates min + t.min (IOW, t2.min + t.min, or 40 + 30 == 70), and stores this valuein the local variable m.
The third line of the body of add() calculates hour+t.hour+m/60 (IOW, t2.hour + t.hour + m/60, or 8 + 5 + 70/60 == 14) and stores this value in t1.hour. Keep in mind that for 70/60, integer division is being performed. At this point in the program, t1.hour == 14 and t1.min == 0, still.

The third line in the body of add() sets t1.min to m%60, the remainder when 70 is divided by 60, so t1.min is set to 10.

The last line returns t1, with t1.hour == 14 and t1.min == 10.

The net result of this is that adding a time of 8:40 to 5:30 results in a time of 14:10.

Ibix and shivajikobardan
Baluncore said:
Memoization (or memoisation) is not the same as memorization (or memorisation).
https://en.wikipedia.org/wiki/Memoization#Etymology
Agreed that memoization/memoisation is not the same as memorization/memorisation, but the OP didn't ask about memoization.

Last edited:
shivajikobardan
thank you for all your replies. looks like i need to memorize it as a rule. There's no way otherwise.

shivajikobardan said:
thank you for all your replies. looks like i need to memorize it as a rule. There's no way otherwise.
Certainly some memorization is necessary to gain understanding, but it's not a rule that you're memorizing, but rather, how the language is defined and how it works. The ideas that are important that you need to understand are these:
1. A member method/function of a class has an implied first argument (called this) that is a reference to the object that is calling the method.
2. A member method/function can access the object's data members without qualifying them with an object's name.
Consider these two code snippets:
C++:
t1=t2.add(t3);  // the call to add()
and
C++:
// The body of add()
{
Time t1;
int m=min + t.min;
<...>
}
Because add() is a member function of the Time class, the compiler includes a hidden first parameter, this, which is a pointer to the object that is calling the function.
In the call to add(), the hidden parameter is a pointer to t2.

Inside the body of add(), min is a member of the calling object, t2 while t.min is a member of the t3 object, the object that is the parameter in the call.

To make this a bit more explicit, the add() function could be written like this:
C++:
Time add(Time t)
{
Time t1;
int m = this->min + t.min;
t1.hour = this->hour + t.hour + m / 60;
t1.min = m % 60;
return t1;
}
Note that in this version I have used the this pointer to make the distinctions between the min and hour members of the parameter object t (t.min and t.hour), and the calling object t2 (this -> min and this -> hour).

shivajikobardan said:
thank you for all your replies. looks like i need to memorize it as a rule. There's no way otherwise.
Did you memorize what 452 + 13 is in elementary school or did you learn a method for how to perform addition of integers?
Same thing here, you are learning a new of manipulating numbers.

malawi_glenn said:
Did you memorize what 452 + 13 is in elementary school or did you learn a method for how to perform addition of integers?
I don't believe this is the problem that the OP was having. I'm reasonably sure that if you asked what time it would be 5 hr 30 min after 8:40. Instead, I believe, it was not understanding the somewhat mysterious connections between implicit and explicit method parameters and the calling object in object-oriented programming. Quite a lot is happening under the hood, so a complete understanding of this mechanism requires learning (i.e., committing to memory) how it works. At least, that was my take, and the direction I took in my detailed explanation.

Mark44 said:
I don't believe this is the problem that the OP was having. I'm reasonably sure that if you asked what time it would be 5 hr 30 min after 8:40. Instead, I believe, it was not understanding the somewhat mysterious connections between implicit and explicit method parameters and the calling object in object-oriented programming. Quite a lot is happening under the hood, so a complete understanding of this mechanism requires learning (i.e., committing to memory) how it works. At least, that was my take, and the direction I took in my detailed explanation.
I know that OP did not have any issues regarding how the clock works... you are once again taking what I write too literaly. Let the OP reply for themselves.

malawi_glenn said:
I know that OP did not have any issues regarding how the clock works...
My point is this: if you know how basic addition works and have memorized what are termed "basic arithmetic facts" with single digits, and know how carrying works., then you can reason out how to do more complicated addition such as 23 + 39, etc.

However, you can't "reason out" the way that member functions work in C++ and other O-O languages without knowing (memorizing) the underlying mechanism that involves unstated parameters.

There is a thin line between memorizing and knowing. Just as one learn/memorize rules for differentation of polynomials etc one can learn/memorize how o-o programming languages works.

## How do "min" and "hour" get their values in this C++ code?

"min" and "hour" typically get their values from input provided by the user, a file, or another function in the code. They can be initialized using standard input/output functions like cin, or through function parameters.

## What data types are "min" and "hour" in this C++ code?

"min" and "hour" are usually of integer data types such as int or unsigned int, as they represent time in minutes and hours, which are whole numbers. However, they could also be of other numeric types depending on the specific requirements of the code.

## Can "min" and "hour" get their values from a system clock in C++?

Yes, "min" and "hour" can get their values from the system clock. This can be done using libraries such as in C++, which provides functions to get the current time and extract hours and minutes from it.

## How do you handle invalid values for "min" and "hour" in C++ code?

Invalid values for "min" and "hour" can be handled using input validation techniques. For example, you can use conditional statements to check if the values are within the valid range (0-59 for minutes and 0-23 for hours) and prompt the user to re-enter valid values if they are not.

## Is it possible to initialize "min" and "hour" using command-line arguments in C++?

Yes, it is possible to initialize "min" and "hour" using command-line arguments in C++. You can access command-line arguments through the parameters of the main function, specifically argc and argv, and then convert the string values to integers using functions like stoi (string to integer).

Replies
8
Views
1K
Replies
3
Views
905
Replies
2
Views
1K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
4
Views
3K
Replies
10
Views
1K
Replies
15
Views
2K
Replies
7
Views
1K
Replies
2
Views
2K