//Hi all,can you please explain why using this-> in this code is optional,that is the purpose?
//Code can work with not this-> too.


CTime_24 operator+(int seconds);



CTime_24 CTime_24 :: operator+(int seconds)
{
    CTime_24 temp;
    temp.m_Seconds = this->m_Seconds + seconds;
    temp.m_Minutes = this->m_Minutes + temp.m_Seconds / 60;
    temp.m_Seconds %= 60;
    temp.m_Hours = this -> m_Hours + temp.m_Minutes / 60;
    temp.m_Minutes %= 60;
    temp.m_Hours %= 24;
    return temp;



}
 int main()

{

CTime_24 time2(6, 20, 59);
    time2.GetTime(hour, minute, second);
    cout << "time2=" << hour << " : " << minute << " : " << second << endl;


    ////OVERLOADED OPERATOR CALL///////
    time2 = time1 + 45;
    time2.GetTime(hour, minute, second);
    cout << "time2 =" << hour << " : " << minute << " : " << second << endl;

}

Within a class' member functions, you can use that class' member variables directly. That's simply how classes work. The function CTime_24 CTime_24 :: operator+(int seconds) is a class function of the class CTime_24, so you can directly access the class' members.

Accessing a class' members by getting a pointer to the class, and dereferencing that pointer, does the same thing, but in an unnecessary way. When you access a class' members as follows:

this->m_Minutes

you're taking a pointer to the class object (this) and dereferencing it. You don't need to do it that way because, as mentioned above, when you're inside a class you can directly access the class' members. Doing it with the object's this pointer is a waste.

//How do i do += operator assignment in this case : ?

// .h
    CTime_24 operator+=(int seconds);
    CTime_24 operator+=( CTime_24& sTime);
//.cpp 
CTime_24& CTime_24 :: operator+=(int seconds)
{
    this->m_Seconds += seconds;
    this->m_Minutes += this->m_Seconds / 60;
    this->m_Seconds %= 60;
    this->m_Hours += this->m_Minutes / 60;
    this->m_Minutes %= 60;
    this->m_Hours %= 24;
    return *this;


}

////+= operator ///
CTime_24  CTime_24 :: operator+=(  CTime_24& sTime)
{
    CTime_24 temp;
    temp.m_Seconds = this->m_Seconds + sTime.m_Seconds;
    temp.m_Minutes = this->m_Minutes + sTime.m_Minutes + temp.m_Seconds / 60;
    temp.m_Seconds %= 60;
    temp.m_Hours = this->m_Hours + sTime.m_Hours + temp.m_Minutes / 60;
    temp.m_Minutes %= 60;
    temp.m_Hours %= 24;
    return temp;

}
///////Main/////////
////Time 4/////
    CTime_24 time4(3, 20, 45);
    time4.GetTime(hour, minute, second);
    cout << "time4  =" << hour << " : " << minute << " : " << second << endl;

    //// time 4 +=  overloaded operator call /////

    time4 = time1 + 45;
    time4.GetTime(hour, minute, second);
    cout << "time4 with += operator is  = " << hour << " : " << minute << " : " << second << endl;

    //// time4 +=  overloaded operator call /////
    //How do i call  here lets say time4 += time1;
CTime_24& CTime_24::operator+=(int seconds)
{
    m_Seconds += seconds;
    m_Minutes += this->m_Seconds / 60;
    m_Seconds %= 60;
    m_Hours += this->m_Minutes / 60;
    m_Minutes %= 60;
    m_Hours %= 24;
    return *this;
}

////+= operator ///
CTime_24 CTime_24:: perator+=(CTime_24& sTime)
{
    m_Seconds = m_Seconds + sTime.m_Seconds;
    m_Minutes = m_Minutes + sTime.m_Minutes + m_Seconds / 60;
    m_Seconds %= 60;
    m_Hours = m_Hours + sTime.m_Hours + m_Minutes / 60;
    m_Minutes %= 60;
    m_Hours %= 24;
    return *this;

}

Thank you all for help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.