1

I have this question about c++ class access modifiers. If I have a basic class, let say it looks like this:

class A
{
public:
    int a1;
private:
    int a2;
}

If i create another class, called C that has public access from class A, then the variable a1 will be public for class C. If the access is private, then the a1 will be private for class C, but if class C has protected access from class A, then a2 will be private for class C. My question is if I create class C:

class C: private A
{
public:
    int c1;
private:
    int c2;
}

then I have private a2 in class C, but is the variable a1 from class A going to be public variable for class C?

  • best example to understand this http://stackoverflow.com/questions/2156913/does-protected-inheritance-allow-the-derived-class-access-the-private-members-of – Ashish Kasma Jul 07 '14 at 13:25

1 Answers1

2

The rules of accessibility of base classes are described in the following quote from the C++ Standard

11.2 Accessibility of base classes and base class members [class.access.base] 1 If a class is declared to be a base class (Clause 10) for another class using the public access specifier, the public members of the base class are accessible as public members of the derived class and protected members of the base class are accessible as protected members of the derived class. If a class is declared to be a base class for another class using the protected access specifier, the public and protected members of the base class are accessible as protected members of the derived class. If a class is declared to be a base class for another class using the private access specifier, the public and protected members of the base class are accessible as private members of the derived class

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335