Accessing private members of C++ classes

Hello, this is my first time posting here. yay.

i have a small problem, i was told that i am able to access a class within a class
directly, example::

if a class is within another class, example: i have class named time, and i have another class named day, which has class time testworking as a private member, and class day can directly access the private members of time testworkin.

thus i wrote this code
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
#include <iostream>
using namespace std;

//First class Declaration
class Time
{
        private:
                double x;

        public:
              void  setx(double p);


};

// Functions of Time
void Time::setx(double p)
{
    x =p;
}

//Second class Declaration
class Day
{
        private:
                Time        testing;

        public:
                void setst(double n);
                void printout();



};
//Functions of Day
void Day::setst(double n)
{
    testing.setx(n);
}
void Day::printout()
{
    cout<<"your value in class employee was set to " << x <<endl;
}


//Main function
int main ()
{
    double h;

    cout<< "enter a number to be set"<<endl;
    cin >>h;
    Day test;
    test.setst(h);
    test.printout();

    return 0;

}
and i am getting a compile time error.
can you help me understand my error, i will truly appreciate it, and thank you very much in advance.
Create relationship between those classes first.
1
2
3
4
5
6
7
8
9
10
11
12
13
//Second class Declaration
class Day:public Time  //for example
{
        private:
                Time        testing;

        public:
                void setst(double n);
                void printout();



};
You cann't access private members of Base class see.
http://cplusplus.com/doc/tutorial/inheritance/

EDIT:
1
2
3
4
class Time
{
        private://By default it set to private but for your need you need to make it protected.
                double x;........

and class day can directly access the private members of time testworkin.


This is incorrect. Only functions that are part of Time can access private members of Time.

Day cannot access Time private members, even if it has a Time object as one of its members.


Your compiler error is from here:

1
2
3
4
void Day::printout()
{
    cout<<"your value in class employee was set to " << x <<endl;
}


Day has no member named 'x', so the compiler doesn't know what you're trying to print.

What you want is to print testing.x, but since x is private that won't work either. You'll either need to make x public, or you'll need to make some kind of getter function and call that to get x.
Hello, thank you very much for answering.

The problem is that i have not learned inheritance,
but i what i did was rewrite the code::

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
#include <iostream>
using namespace std;

class Time
{
        private:
                double x;

        public:
              void  setx(double p);
              void printo();


};

void Time::setx(double p)
{
    x =p;
}
void Time::printo()
{
cout <<"your value in class time was set to " <<x<<endl;
}


class Day
{
        private:
                Time        testing[10];

        public:
                void setst(double n);
                void printout();



};

void Day::setst(double n)
{
    testing[1].setx(n);
}
void Day::printout()
{
   testing[1].printo();
}

int main ()
{
    double h;

    cout<< "enter a number to be set"<<endl;
    cin >>h;
    Day test;
    test.setst(h);
    test.printout();
    cout<< "enter a number to be set in second class"<<endl;
    cin >>h;
    Day hello;
    hello.printout();
    hello.setst(h);
    hello.printout();
    test.printout();



    return 0;

}


thus this works fine, now but may i ask a question, i made an array of 10 testing.

thus when i create an object of type day (ex day hello) there was one object named hello was created of type day, and it had 10 objects of type time. is that correct?
when i create an object of type day (ex day hello) there was one object named hello was created of type day, and it had 10 objects of type time. is that correct?


Yes, this is correct.

On a somewhat unrelated note... remember that array indexes start at 0, not at 1

So testing[1] is actually the second Time in the array, not the first. The first would be testing[0]
I don't care what result you need but after that changes it run..And from DISCH idea it work.
testing.x
but x should declared as public it Time class//Logic?
Disch, thank you, i understand now.

Thank both of you for helping me out, i am new to C++, its been a month since i started.

your help is really appreciated.
Topic archived. No new replies allowed.