vepew26247
vepew26247

Reputation: 41

Object resets when passed

I have been practicing c++ lately, and came across this exercise where I am struggling a little with reference of objects.

ClockUTC.h

#include "ClockUTC.h"

ClockUTC::ClockUTC()
{
    hour = 0;
    minute = 0;
}

ClockUTC::ClockUTC(int hour, int minute)
{
    this-> hour = hour;
    this-> minute = minute;
}

int ClockUTC::getHour()
{
    return hour;
}

int ClockUTC::getMinute()
{
    return minute;
}

void ClockUTC::setHour(int hour)
{
    this->hour = hour;
}

void ClockUTC::setMinute(int minute)
{
    this->minute = minute;
}

ClockUTC.cpp

#ifndef CLOCKUTC_H_INCLUDED
#define CLOCKUTC_H_INCLUDED


#include <iostream>


using namespace std;



class ClockUTC {

private:
int hour;
int minute;

public:
    ClockUTC();
    ClockUTC(int hour, int minute);
    int getHour();
    int getMinute();
    void setHour(int hour);
    void setMinute(int minute);
};


#endif // CLOCKUTC_H_INCLUDED

ClockTZ.h

#ifndef CLOCKTZ_H_INCLUDED
#define CLOCKTZ_H_INCLUDED



#include <iostream>
#include "ClockUTC.h"


using namespace std;



class ClockTZ : public ClockUTC{

private:
ClockUTC clockUTC;
int offset;

public:
    ClockTZ(ClockUTC clockUTC, int offset);
    ClockUTC getClockUTC();
    int getOffset();
    void setClockUTC(ClockUTC clockUTC);
    void setOffset(int offset);
};




#endif // CLOCKTZ_H_INCLUDED

ClockUTC.cpp

#include "ClockTZ.h"


 ClockTZ::ClockTZ(ClockUTC clockUTC, int offset)
{
    this->clockUTC = clockUTC;
    this->offset = offset;
    clockUTC.setHour(clockUTC.getHour() + offset);
}


ClockUTC ClockTZ::getClockUTC()
{
    return clockUTC;
}


int ClockTZ::getOffset()
{
    return offset;
}


void ClockTZ::setClockUTC(ClockUTC clockUTC)
{
    this->clockUTC = clockUTC;
}



void ClockTZ::setOffset(int offset)
{
    this->offset = offset;
}

And here the main file is:

/**
   Unit tests for 2nd EOOP assignment.

   ClockUTC - aggregate 2 integers (hour, minute)
   ClockTZ - view for ClockUTC, store reference to clockUTZ and offset

   Note that this is in a big part "code reading" exercise.
   Based on this file you should implement two classes mentioned above,
   with proper interface, and in right header file.
 */

    #include "ClockTZ.h"
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
        ClockUTC greenwich(9,17);
        ClockTZ warsaw(greenwich, 2);
    
        if (warsaw.getHour() != 12){
            cout << "Error in getHour() 1" <<  endl;
        }
    
        return 0;
    }

I have coded the above files, and the only problem is after creating the ClockUTC class and passing it to the ClockTZ, ClockUTC resets and it is calling it's empty constructor and setting the hour to 0 instead of it's value + the offset as intended. I know it is because that the object is not having any reference and it is being destroyed as soon as passed or something, but don't seem to be able to fix it.

Current output: 0

Expected output: 12

I have tried fixing it by applying the & reference, but nothing seem to work. Can someone help me applying it properly?

Upvotes: 0

Views: 108

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

ClockTZ has two ClockUTC objects: a base class, and a member variable named clockUTC. The ClockTZ constructor will initialize the member variable by copying the value passed in, and will default initialize the base class. When you later call getHour, the base class is called, returning the default value of 0.

Upvotes: 1

Related Questions