Examveda

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

A. private

B. public

C. protected

D. friend

Answer: Option A

Solution (By Examveda Team)

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.

Join The Discussion

Comments (1)

  1. KRISHNA MOHAN
    KRISHNA MOHAN:
    3 months ago

    Answer A will be correct not C

Related Questions on Inheritance in C plus plus