Search code examples
c++inheritanceinterfaceaccess-modifiersmodifier

default access modifier in C++


I have an interface in C++ that looks something like this:

// A.h
#pragma once

class A
{
public:
    //Some declarations.    
private:    
    //Some declarations.
protected:
    //Some declarations.
};

The specific form is not important. Since this is an interface, there will be a class B that inherits from A. In the header file for class B I have:

// B.h
#pragma once

class B : A
{
public:
    //Some declarations.    
private:    
    //Some declarations.
protected:
    //Some declarations.
};

My concern is that I tend to use class B : A instead of class B : public A, just my bad memory.

So far I have had no issues with this, since it's a small enough project. But will forgetting the public keyword affect my project in any sense?

Or more succinctly, I know how access modifiers work but, what does class B : A default to?


Solution

  • The ONLY difference between struct and class is that in a struct, everything is public until declared otherwise, and in a class, everything is private until declared otherwise. That includes inheritance. So class B : A will default to private inheritance, and struct B : A will default to public inheritance.