Which of the following is a correct syntax for declaring a constructor in C++ ?

2025

Which of the following is a correct syntax for declaring a constructor in C++ ?

  1. A.

    constructor ClassName();

  2. B.

    ClassName();

  3. C.

    void ClassName();

  4. D.

    ClassName() {}

Attempted by 64 students.

Show answer & explanation

Correct answer: B

A C++ constructor must match the class name exactly and have no return type.

Explore the full course: Tpsc Assistant Technical Officer