3

I was looking at inheritance concepts in http://www.geeksforgeeks.org/inheritance-in-c/. I was confused with few sentences written by the author. At one place author says

If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class .......

That means do we have something like public class in C++? Also below table from the article indicates that there is a concept of public/Protected class. enter image description here

I looked at few other SO posts (Use of "Public" in a derived class declaration?) and found no reference to Public, Private or protected class itself. The post https://stackoverflow.com/questions/4792614/making-classes-public-to-other-classes-in-c talks of public, but by means of header file.

Rajesh
  • 1,085
  • 1
  • 12
  • 25
  • The article you linked shows what it's talking about: `class Child : public Parent` – chris Sep 25 '17 at 18:05
  • Unrelated to the question, but it bothers me that the private column is in the middle and that the public rows is last. – François Andrieux Sep 25 '17 at 18:06
  • 1
    There's no such thing as a public or private class in C++. However, there is such a thing as a public or private *base class*. – cdhowie Sep 25 '17 at 18:08
  • What do you mean by public class? An inherited class can have an access modifier to define the kind of inheritance. You can also define classes within other classes, in which case access modifiers also come into play. But from the question, it seems like you are under the impression that classes in general have an inherit top-level access modifier, which is not the case. – François Andrieux Sep 25 '17 at 18:09
  • Note that private members from base classes and members of privately inherited base classes are inherited but inaccessible from the derived class. It's misleading to imply that they are not inherited at all. – François Andrieux Sep 25 '17 at 18:11
  • _"Also below table from the article indicates that there is a concept of public/Protected class. "_ There are no access modifiers at the `class` / `struct` declaration level in c++. If that article suggests such, it's wrong. – user0042 Sep 25 '17 at 18:12

1 Answers1

14

The Public, Protected and Private keywords are the visibility labels in C++. There is no public, protected and private class type in c++ (like Java). These three keywords are also used in a completely different context to specify the visibility inheritance model.

The table given below lists all of the possible combinations of the component declaration and inheritance model presenting the resulting access to the components when the subclass is completely defined.

enter image description here

It reads in the following way (take a look at the first row):

if a component is declared as public and its class is inherited as public the resulting access is public.

Have a look at an example below:

class Super {
    private:     int x;
    protected:   int y;
    public:      int z;
 };
class Sub : protected Super {};

The resulting access for variables y, z in class Sub is protected and for variable x is none.

NOTE:

The lack of a modifier yields private.

abhiarora
  • 9,743
  • 5
  • 32
  • 57