Constructors

This lesson introduces the concept of constructors, how to declare, overload and call them

Introduction

A constructor is automatically called when an object of the class is declared.

  • A constructor is a member function that is usually public.

  • A constructor can be used to initialize member variables when an object is declared.

Note: A constructor’s name must be the same as the name of the class it is declared in

A constructor cannot return a value.

Note: No return type, not even void can be used while declaring a constructor

Constructor Declaration

A constructor​ for the DayofYear class can be declared as follows:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy