Confused about class access labels!

If we define classes like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class base {
private:
    int a;
protected:
    int b;
public:
    int c;
}

class publicderived: public base {

}

class protectederived: protected base {

}

class privatederived: private base {

}


then

a can be seen by base only, right?

b can be seen by all classes, right? and will change to be private of privatederived, and remain protected in publicderived and protected derived, right?

c can be seen by all classes, right? and will change to protected member of protectederived, and change to private member of privatederived, and remain public in publicderived, right?
Last edited on
a can be accessed by base only.

b can be accessed by base and any class derived from base.

c can be accessed by anything.



Any code with a publicderived object may access the inherited 'base' object.

Only code within protectedderived and classes derived from it may access the inherited 'base' object.

Only code within privatederived may access the inherited 'base' object.



By "access the inherited base object' I mean:

1) Get a pointer/reference to a base from a pointer/object of the derived class:
1
2
3
4
5
publicderived foo;
base* foobase = &foo;  // OK, foo has base public derived

privatederived bar;
base* barbase = &foo; // ERROR outside of privatederived class - base is private derived 


2) Access any of base's members.

1
2
3
4
5
6
publicderived foo;
foo.c = 5;  // OK, foo has base public derived

privatederived bar;
bar.c = 5;  // ERROR outside of privatederived class - even though c is public in base,
  // the base class itself is inaccessible outside of privatederived 
Last edited on
Topic archived. No new replies allowed.