Vidyalelo
C++ Programming · Q23

Inheritance in C++

Programming · C++ Programming · question 23

Q23

What is the default access specifier for a base class when inheriting in C++?

A.
private
Answer
B.
public
C.
protected
D.
friend

Answer: Option A

Solution

Answer: Option A
Solution:
The correct answer is A: private.
Let's break down why:

In C++, when you inherit a class without explicitly specifying an access specifier (like `public`, `private`, or `protected`), the default access specifier depends on how you inherit.

If you use the `class` keyword to derive a class, the default access specifier is private.
For example, if you write `class Derived : Base`, it's the same as writing `class Derived : private Base`.

If you use the `struct` keyword to derive a class, the default access specifier is public.

This means that if you don't write `public`, `protected`, or `private` before the base class name in the inheritance, the members of the base class become `private` in the derived class (when inheriting with `class`).

Therefore, the default access specifier is private.