Default class inheritance access

access specifiers in c++ inheritance
c++ private inheritance
are private members inherited in c++
type of inheritance in c++
protected c++
c++ multiple inheritance
access modifiers in c++
can derived class access private members of base class c++

Suppose I have a base and derived class:

class Base
{
    public:
    virtual void Do();
}

class Derived:Base
{
    public:
    virtual void Do();
}

int main()
{
    Derived sth;
    sth.Do(); // calls Derived::Do OK
    sth.Base::Do(); // ERROR; not calls Based::Do 
}

as seen I wish to access Base::Do through Derived. I get a compile error as "class Base in inaccessible" however when I declare Derive as

class Derived: public Base

it works ok.

I have read default inheritance access is public, then why I need to explicitly declare public inheritance here?

You might have read something incomplete or misleading. To quote Bjarne Stroustrup from "The C++ programming Language", fourth Ed., p. 602:

In a class, members are by default private; in a struct, members are by default public (§16.2.4).

This also holds for members inherited without access level specifier.

A widespread convention is, to use struct only for organization of pure data members. You correctly used a class to model and implement object behaviour.

Default class inheritance access, This means derived classes can not access private members of the base class directly! class Def: Base // Defaults to private inheritance. A class can always access its own (non-inherited) members. The public accesses the members of a class based on the access specifiers of the class it is accessing. A class accesses inherited members based on the access specifier inherited from the parent class. This varies depending on the access specifier and type of inheritance used.

From standard docs, 11.2.2

In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key struct and private is assumed when the class is defined with the class-key class.

So, for structs the default is public and for classes, the default is private...

Examples from the standard docs itself,

class D3 : B { / ... / }; // B private by default

struct D6 : B { / ... / }; // B public by default

11.5, In a member-specification of a class/struct or union, define the accessibility of 6​) Private inheritance: the public and protected members of the base class listed after the A union has public access for its members by default. Only the class and friend functions can access private members. By default all the members of a class would be private, for example in the following class width is a private member, which means until you label a member, it will be assumed a private member −

The default inheritance level (in absence of an access-specifier for a base class )for class in C++ is private. [For struct it is public]

class Derived:Base

Base is privately inherited so you cannot do sth.Base::Do(); inside main() because Base::Do() is private inside Derived

Derived classes, C++ Inheritance Access Control - When creating a derived class from a base class then, you can use different access specifiers to inherit etc. class T : private X // 'private' is default for classes. {. // a is private. // b is private. // c is not accessible  Inheritance & Access Modifiers - Tutorial to learn Inheritance & Access Modifiers in Java in simple, easy and step by step way with syntax, examples and notes. Covers topics like Inheritance, Access Modifiers, super and final keyword etc.

The default type of the inheritance is private. In your code,

class B:A

is nothing but

class B: private A

access specifiers, (no specifier) (default): the member (variable or method) can be accessed from within any method of a class inside the same package. public: the member (  Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. When creating a class, instead of writing completely new data members and member functions,

C++ Inheritance Access Control, If the access-specifier is not used, then it is private by default. Consider a base class Shape and its derived class Rectangle as follows −. Live Demo. #include <​  Struct members, including nested classes and structs, can be declared public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected, or private. Class and struct members, including nested classes and structs, have private access by default. Private nested types aren't accessible from outside the containing type.

Access specifiers and Inheritance, Default: When no access modifier is specified for a class , method or data The method display in class A is protected and class B is inherited from class A and  Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.

C++ Inheritance, In principle, private and protected members of a class cannot be accessed from constructors of a derived class calls the default constructor of its base classes  Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected or private. The access level for class members and struct members, including nested classes and structs, is private by default.

Comments
  • @Prasoon Taken out of context you are right, but the context established by the title and the code clearly is "class" and not "struct".
  • This should be the answer.