Access Modifier for class name in C++

Viewed 2529

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.

1 Answers

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.

Related