C++'s default inheritance access specifier? [duplicate] - c++

This question already has answers here:
Default class inheritance access
(4 answers)
Closed 3 years ago.
I have some legacy code that I have to wrap, and I have come across this declaration:
class Foo : Bar
{
// ...
};
This seems to compile under GCC. I know it's bad, but I can't change it. My question is, if no inheritance access specifier is present, how does the C++ compiler handle it?

For classes, the default is private.
For structs, the default is public.

BTW, it is not called access modifier. It is called access specifier
$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."
In your context, 'Bar' is a private base class of 'Foo'

It's private.

Related

When would you use a friend class/function instead of inheritance? [duplicate]

This question already has answers here:
When should you use 'friend' in C++?
(31 answers)
What's the difference between friendship and inheritance?
(4 answers)
Closed 5 years ago.
I am not exactly sure what the use of a friend function or class is when you can easily just use inheritance (parent/child classes)? From my understanding, a friend function or class allows a non-member function to access the members of another class that it is declared a friend of. Similarly, a child class can access the members of a parent class. I am wondering, in which situation would you pick one over the other?
Because you may want to use some private functions/member of a class without being a child of it....
You should read more about inheritance. You can't consider inheritance as a solution to access "members of a class" as you mentioned.

What is the default mode (public, protected, or private) of inheritance in C++? [duplicate]

This question already has answers here:
Default inheritance access specifier
(9 answers)
Closed 7 years ago.
When I write
class Derived : Base { };
This compiles. (I sort of assumed that one needs to specify it as being one of public, protected, or private inheritance, which has consequences about the visibility of members.
Which one of the three is it if I do not specify?
The default is "private" for classes, and "public" for structs. This is also true for the default access mode of members in said classes and structs.
In a class, members are by default private; in a struct, members are by default public (§16.2.4).

Could we declare a pointer of the class's type in the class declaration? [duplicate]

This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 8 years ago.
I know that is possible for a struct:
struct A{
A* p;
};
But could we do that to a class:
class B{
B*p;
}
I tried to google it, but end not found it. Thank you very much!
Yes. There is no difference between a class declared with struct and one declared with class other than the default access for members and bases.
Yes, a class can contain pointers to a class of the same type, and the symbol representing the class can appear in signatures of functions or in the bodies of functions defined in that class. Furthermore, it should be noted that, in C++, struct and class are identical with only one difference -- the default access level for a struct is "public", while the default access level for a class is "private" (of course, one can trivially write "public:" or "private:" at the beginning of either in order to change the access level; it is only convention that results in "class" being used for types that include more functionality, encapsulation and "struct" for pure data objects).

Can we declare an interface class by making all the methods abstract using the keyword ‘abstract’ in a class in C++? [duplicate]

This question already has answers here:
How do you declare an interface in C++?
(17 answers)
Closed 9 years ago.
I want to know about the way of defining interface class in C++ by this approach. Is it possible? I don't want to know about interface creation but want to know about the abstract keyword use in C++.
In C++, an abstract class is any class that has at least 1 pure virtual function.
C++ does not have direct support for interfaces, but you can make one by making all of the functions public, virtual, and abstract, and have no data members in the class.
In C++, an interface is defined as follows:
class Interface {
public:
virtual ~Interface();
virtual void aMethod() = 0;
};
Note the virtual destructor.

what is difference between structure and class [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What are the differences between struct and class in C++
consider that i need to answer this question for interview..
in structure, the member access specifier are default to public while on class is private.
This is a standard interview question.
The standard (and correct) answer is "A struct's members are public by default while a class' members are private by default."
For bonus points, the full answer is "A struct's members are public by default and inheritance is public by default whereas a class's members and inheritance are private by default."