vepew26247
vepew26247

Reputation: 41

Object resets when passed in c++

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

Here is my code:

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

ClockTZ.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() != 11){
            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.

Probably the problem is that 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 I later call getHour, the base class is called, returning the default value of 0.

But I do need both in this case...

Current output: 0

Expected output: 11

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

Here is what I have tried: ClockUTC &clockUTC; as a class member (so the constructor will also have to take its ClockUTC parameter as a reference, then I used the member-initializer-list to initialize it.

As in code:

ClockUTC &clockUTC;

ClockTZ(ClockUTC &clockUTC, int offset);

ClockTZ::ClockTZ(ClockUTC& _clockUTC, int _offset) : clockUTC( _clockUTC ) , offset( _offset) { }

But this didn't change anything at all, and I still get the same output. Please somebody help me make it work!

Update:

int main() {

    ClockUTC greenwich(9,17);
    ClockTZ warsaw(greenwich, 2);


    if (warsaw.getHour() != 11)
        cout << "Error in getHour() 1  warsaw " << warsaw.getHour() <<  endl;
    
    greenwich.setHour(11);
    
    if (warsaw.getHour() != 13)
        cout << "Error in getHour() 2 warsaw " << warsaw.getHour() << endl;
    

    cout << "End of tests." << endl;
    return 0;
}

Expected output: 11 and then 13, but it will always be 11. So reference is not working.

My constructor:

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

Now output will always be 9

Upvotes: 4

Views: 103

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118300

class ClockTZ : public ClockUTC{

private:
   ClockUTC clockUTC;

This is wrong. ClockTZ inherits from ClockUTC. It should not have another instance of ClockUTC as a class member. This creates a duplicate instance of ClockUTC that's a private member of the class, in addition to inheriting from ClockUTC. Remove this clockUTC class member, completely. Just get rid of the clockUTC declaration in its entirety.

ClockTZ::ClockTZ(ClockUTC clockUTC, int offset)
{
    this->clockUTC = clockUTC;

The child class constructor should, instead, contruct its parent class correctly:

ClockTZ::ClockTZ(ClockUTC clockUTC, int offset)
       : ClockUTC{clockUTC}
{

That's it (or, maybe "ClockUTC(clockUTC)", if you are using an old compiler).

and it is calling it's empty constructor and setting the hour to 0 instead of it's value + the offset as intended.

Of course it does. Your child class never calls the parent class's constructor, and instead constructs a duplicate member of the child class, that happens to be the same class as its parent class.

Your C++ book should have more information and examples of correctly constructing parent classes.

Upvotes: 3

Related Questions