As far as I understand, they both support inheritance, both may contain fields and functions, both may contain a constructor and a destructor, both support inheritance. The difference is that functions and variables without the private / protected / public keyword are one by default sent to the private section, others to the public section. Is it possible to inherit a struct from a parent class and vice versa? If so, how will the fields be converted without a modifier? For example, if they are sent from a class to a child struct, will they remain private or become public? If not, why not? Inheritance, I mean the full, public inheritance modifier.

  • In theory, earlier the struct was treated as a class in which all fields are public. If the concept has not changed, then you can inherit. - pepsicoca1
  • one
    @ pepsicoca1 Not all, but those that are declared without a modifier. - Harry

2 answers 2

Inheritance is possible.

The members of a struct behave as if the implicit access specifier public specified at the beginning of a struct definition, and the class members behave as if the implicit access specifier private specified at the beginning of the definition

 struct S { // public: ... }; class C { // private: ... }; 

In inheritance, if the heir is a struct , then inheritance is performed as public by default. If the heir is a class , then inheritance is performed as private by default.

 struct DS : Parent // эквивалентно `struct DS : public Parent` { ... }; class DC : Parent // эквивалентно `class DC : private Parent` { ... }; 

And then the same general rules work, the same for both class and struct : a more restrictive access level “wins” a more relaxed level of access.

    Since the struct is a class with public by default , all the same rules apply as if it were a class whose members are declared as public — clearly, those that are declared without a modifier . Members declared with a modifier behave exactly as if this type were declared as a class .

    I hope, it is not necessary to describe the access rules for inheritance here? :) And then you can not finish them in 2018 :)