Whom are class private fields hidden from?

Given two class declaration examples.
The problem is to count field X.
How many are X-s in the first example?
How many are X-s in the second one?

I have tried the inheridance models but all of them are failing to come out with
correct answer. Seems to me an unfinished wreckage lurking around.

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
//I variant
class A{ 
  int X
}

class B:public A{
  int X
}

class C:public B{
  int X
}

//II variant

class A{ 
  int X
}

class B:public A{
  
}

class C:public B{
  
}
I'm not sure what the post is asking, so I'll answer the title question.

Private members are only visible to other members. Protected members are visible to other members and derived classes, as well as friend classes. Public members are visible everywhere.
I think this test should give you the answer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//I variant
class A1{ int X; };

class B1:public A1{ int X; };

class C1:public B1{ int X; };

//II variant
class A2{ int X; };

class B2:public A2{ };

class C2:public B2{ };

int main() {
    std::cout << "Number of ints in C1=" << sizeof(C1) / sizeof(int) << std::endl;
    std::cout << "Number of ints in C2=" << sizeof(C2) / sizeof(int) << std::endl;
}
Ok. Thanks. I think a prior to the Turbo Pascal is telling the truth with all its rigidity unlike
its descendands rapping around the field, including the C++.
Topic archived. No new replies allowed.