setting a subclasses internals

Hi I have two header files TimeRepresentation.h and GlobalTime.h, which has a TimeRepresentation variable declared in the private section of GlobalTime.h. I was wondering if it was possible to set the internals of the TimeRepresnetation localTime in a test driver by doing something like globaltime.get_localTime().set_seconds(15);?
or any other way to set a subclasses internals would help. Thank You
UInteger32 is typedef int
i've already tested that it complies with the driver, just need a way to set the internals of a subclass
get_localTime() returns localTime and set_seconds(15) sets the variable seconds in TimeRepresentation to 15 using seconds = x, where x is 15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*start timeRepresentation.h*/
#ifndef TIMEREPRESENTATION_H
#define TIMEREPRESENTATION_H
#include "datatypes_dep.h"
#include "constants.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream>

using namespace std;

class TimeRepresentation{

public:

        /*************Constructor************/
        TimeRepresentation();

        /****************accessors***********/
        UInteger32 get_seconds() const;
        Integer32 get_nanoseconds() const;

        /****************mutators***********/
        void set_seconds(UInteger32 s);
        void set_nanoseconds(Integer32 ns);

        /*******Operator*******/
        TimeRepresentation &operator=(const TimeRepresentation & other_t);

        friend ostream& operator<<(ostream& s, const TimeRepresentation& it);


private:
        UInteger32 seconds;
        Integer32 nanoseconds;
};

ostream& operator<<(ostream& s, const TimeRepresentation& it);

#endif
 /* end of TimeRepresentation.h*/

/*start GlobalTime.h*/         
#ifndef GLOBALTIME_H
#define GLOBALTIME_H
#include "TimeRepresentation.h"
#include "constants.h"
#include <iostream>

using namespace std;

class GlobalTime{

public:

        /*************Constructor************/
        GlobalTime();

        /****************accessors***********/
        TimeRepresentation get_localTime() const;
        Integer16 get_currentUtcOffset() const;
        Boolean get_leap59() const;
        Boolean get_leap61() const;
        UInteger16 get_epochNumber() const;

        /****************mutators***********/
        void set_localTime(TimeRepresentation lt);
        void set_currentUtcOffset(Integer16 cuo);
        void set_leap59(Boolean l59);
        void set_leap61(Boolean l61);
        void set_epochNumber(UInteger16 en);

        GlobalTime &operator=(const GlobalTime & other_t);

        friend ostream& operator<<(ostream& s, const GlobalTime& it);

private:
        TimeRepresentation localTime;
        Integer16 currentUtcOffset;
        Boolean leap59;
        Boolean leap61;
        UInteger16 epochNumber;
};

ostream& operator<<(ostream& s, const GlobalTime& it);
#endif
/*end GlobalTime.h*/
Last edited on
I'm not sure what your question is - what did you try that you expected to work but didn't work?

Also, there is already a standard C/C++ header with type aliases for integers of certain bit widths:
http://en.cppreference.com/w/cpp/header/cstdint
I wanted to see if it was possible to see if one could set the internals of a subclass within the class, without making extra mutators for the class. Say I have, an accessor to the subclass that just returns the subclass, can i use the subclasses mutator to edit. Something like class.get_subclass().set_variable_in_subclass(15), where class is a declaration of Class, get_subclass() is the classes mutator that returns the subclass, and set_variable_in_subclass(15) is the subclasses mutator to edit the contents of the subclass. I tried this, it compiled but didn't set anything so I was looking for suggestions in doing this without, creating new mutators in the class or using a temporary variable.

sorry for the confusion, but im not very good at explaining.
There are no subclasses (inner-classes) or subclasses (derived classes) in your code, so I have no idea what you are talking about.
I may have the wrong terminology but on line 79, TimeRepresentation is another class that I have defined, and have shown the header file at the top. TimeRepresenatation is a class that is used in another class called GlobalTime. I was writing a driver for GlobalTime and wanted to know how to set the internals of the TimeRepresentation class within the GlobalTime class. Below is my GlobalTime.cc file, I think I may need to pass by reference or value to do this correctly, but not quite sure how I would do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "GlobalTime.h"
#include <string.h>
#include <iostream>

using namespace std;

/*****************Constructor****************/
GlobalTime::GlobalTime()
{
}

/*****************accessors***************/
TimeRepresentation GlobalTime::get_localTime() const
{return localTime;}

Integer16 GlobalTime::get_currentUtcOffset() const
{return currentUtcOffset;}

Boolean GlobalTime::get_leap59() const
{return leap59;}

Boolean GlobalTime::get_leap61() const
{return leap61;}

UInteger16 GlobalTime::get_epochNumber() const
{return epochNumber;}

/****************mutators******************/
void GlobalTime::set_localTime(TimeRepresentation lt)
{localTime = lt;}

void GlobalTime::set_currentUtcOffset(Integer16 cuo)
{currentUtcOffset = cuo;}

void GlobalTime::set_leap59(Boolean l59)
{leap59 = l59;}

void GlobalTime::set_leap61(Boolean l61)
{leap61 = l61;}

void GlobalTime::set_epochNumber(UInteger16 en)
{epochNumber = en;}

GlobalTime &GlobalTime::operator=(const GlobalTime & other_t)
{
        localTime = other_t.localTime;
        currentUtcOffset = other_t.currentUtcOffset;
        leap59 = other_t.leap59;
        leap61 = other_t.leap61;
        epochNumber = other_t.epochNumber;

        return *this;
}
ostream& operator<<(ostream& s, const GlobalTime& it)
{
        cout << "local time : " << it.localTime << endl;
/*        cout << "current utc offset : " << it.currentUtcOffset << endl;
        cout << "leap59 : " << it.leap59 << endl;
        cout << "leap61 : " << it.leap61 << endl;
        cout << "epochNumber : " << it.epochNumber << endl;
*/
        return s;
}


this is my TimeRepresentation.cc file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "TimeRepresentation.h"
#include <iostream>

using namespace std;

/*****************Constructor****************/
TimeRepresentation::TimeRepresentation()
{
seconds = 0;
nanoseconds = 0;
}

/*****************accessors***************/
UInteger32 TimeRepresentation::get_seconds() const
{return seconds;}

Integer32 TimeRepresentation::get_nanoseconds() const
{return nanoseconds;}

/****************mutators******************/
void TimeRepresentation::set_seconds(UInteger32 s)
{seconds = s;}

void TimeRepresentation::set_nanoseconds(Integer32 ns)
{nanoseconds = ns;}

TimeRepresentation &TimeRepresentation::operator=(const TimeRepresentation & other_t)
{
        seconds = other_t.seconds;
        nanoseconds = other_t.nanoseconds;
        return *this;
}

ostream& operator<<(ostream& s, const TimeRepresentation& it)
{
        cout << "Seconds : " << it.seconds << endl;
        cout << "Nanoseconds : " << it.nanoseconds << endl;

        return s;
}


sorry for confusion
You should call the member functions you took the time to create. If you intended on ditching encapsulation and just setting values directly, why would you have written the functions?
Topic archived. No new replies allowed.