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

  • Context: Comp Sci 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    C++ Code
Click For Summary

Discussion Overview

The discussion revolves around understanding how the member variables "min" and "hour" in a C++ class are assigned values when objects are created and methods are called. Participants explore the role of constructors and member functions, particularly in the context of the provided code snippet that implements a simple time addition functionality.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants explain that constructors are used to initialize object values, with the default constructor setting "hour" and "min" to zero, while the parameterized constructor sets them to specified values.
  • There is a discussion about how member functions, like "add", can access member variables directly without needing to qualify them with the object name, due to an implied first argument (this) that refers to the calling object.
  • One participant expresses confusion about whether they need to memorize this behavior or if there is a conceptual understanding behind it.
  • Another participant suggests that member variables are visually distinguished in IDEs, which may help in understanding their context within methods.
  • Clarifications are provided regarding the behavior of the "add" method, specifically how it calculates the total minutes and hours based on the member variables of the calling object and the parameter object.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of constructors and member function access, but there is some disagreement on whether understanding should be based on memorization or conceptual clarity. The discussion remains unresolved regarding the best approach to learning these concepts.

Contextual Notes

Some participants mention the potential for confusion between "memorization" and "memoization," indicating a need for clarity in terminology. There are also references to the limitations of understanding based solely on memorization without grasping the underlying principles of object-oriented programming in C++.

shivajikobardan
Messages
637
Reaction score
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);
    count<<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
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   Reactions: 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?
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:
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".
 
  • Like
Likes   Reactions: DrClaude
@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   Reactions: 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:
  • Like
Likes   Reactions: 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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K