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

  • Comp Sci
  • Thread starter shivajikobardan
  • Start date
  • Tags
    C++ Code
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.
  • #1
shivajikobardan
674
54
Homework Statement
How does "min" and "hour" get their values in this C++ code?
Relevant Equations
none but I am studying about operator overloading although this is not operator overloading.
How does "min" and "hour" get their values in this C++ code?
cv5Ca34JiFkFCxt2VO0Q2PO509HQFLBGAAcF0_9BKpZ271InoE.jpg

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 add (Time t)
    {
        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);
    t1=t2.add(t3);
    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.
 
Physics news on Phys.org
  • #2
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.
 
  • Like
Likes shivajikobardan
  • #3
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?
Untitled.png

a34jifkfcxt2vo0q2po509hqflbgaacf0_9bkpz271inoe-jpg.jpg

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:
  • #4
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);
 
  • #5
I honestly hate memorizing. I find all my doubts end up "memorization problems".
 
  • #6
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".
 
  • Like
Likes DrClaude
  • #7
@shivajikobardan, your Time class has the following:
  • Two data members, hour and min
  • Two constructors
  • A member function, add.

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.
 
  • Like
Likes Ibix and shivajikobardan
  • #8
  • #9
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:
  • Like
Likes shivajikobardan
  • #10
thank you for all your replies. looks like i need to memorize it as a rule. There's no way otherwise.
 
  • #11
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 add (Time t)
{
  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).
 
  • #12
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.
 
  • #13
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.
 
  • #14
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.
 
  • #15
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.
 
  • #16
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.
 

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

In C++, the values for "min" and "hour" can be assigned in a variety of ways. One common way is through user input, where the user is prompted to enter a value for each variable. Another way is through a calculation or function that determines the values based on other variables or conditions in the code.

2. Are "min" and "hour" predefined variables in C++?

No, "min" and "hour" are not predefined variables in C++. They are simply variable names chosen by the programmer to represent specific values or data in the code.

3. How are "min" and "hour" used in this C++ code?

The specific use of "min" and "hour" in a C++ code will depend on the purpose and functionality of the code. Generally, they are used to represent time-related values, such as minutes and hours, which can be used in calculations or comparisons.

4. Can the values of "min" and "hour" be changed during the execution of the code?

Yes, the values of "min" and "hour" can be changed during the execution of the code. This can be done through various methods, such as reassigning the values based on user input or updating them through calculations or functions.

5. How are "min" and "hour" stored in the memory in this C++ code?

In C++, variables like "min" and "hour" are stored in the computer's memory as specific data types, such as integers or floating-point numbers. The size and type of data storage will depend on the specific data type chosen by the programmer for each variable.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
836
  • Engineering and Comp Sci Homework Help
Replies
3
Views
749
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
932
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
Back
Top