Skip to main content

Command Palette

Search for a command to run...

Constructors in Programming Languages

Getting familiar with Constructors

Published
•3 min read•View as Markdown
Constructors in Programming Languages

C++

Definition: In C++, a constructor is a special member function of a class that initializes objects of that class. It is called automatically when an object is created.

Key Points:

  1. Same Name as Class: A constructor has the same name as the class.

  2. No Return Type: Constructors do not have a return type, not even void.

  3. Types: There can be default constructors (no parameters), parameterized constructors (with parameters), and copy constructors (which take a reference to an object of the same class).

Example:

#include <iostream>
using namespace std;

class Car {
public:
    string brand;
    int year;

    // Default Constructor
    Car() {
        brand = "Toyota";
        year = 2020;
    }

    // Parameterized Constructor
    Car(string b, int y) {
        brand = b;
        year = y;
    }

    void display() {
        cout << "Brand: " << brand << ", Year: " << year << endl;
    }
};

int main() {
    Car car1; // Default constructor called
    Car car2("Honda", 2022); // Parameterized constructor called

    car1.display();
    car2.display();

    return 0;
}

JavaScript

Definition: In JavaScript, a constructor is a special method for creating and initializing an object created with a class.

Key Points:

  1. Keyword constructor: The constructor method is called constructor.

  2. Called with new: When an object is created using the new keyword, the constructor method is called.

  3. Object-Oriented: Even though JavaScript is not class-based, ES6 introduced classes to simplify object-oriented programming.

Example:

class Car {
    constructor(brand, year) {
        this.brand = brand;
        this.year = year;
    }

    display() {
        console.log(`Brand: ${this.brand}, Year: ${this.year}`);
    }
}

const car1 = new Car("Toyota", 2020); // Constructor called
const car2 = new Car("Honda", 2022); // Constructor called

car1.display();
car2.display();

Java

Definition: In Java, a constructor is a block of code used to initialize an object. It is called when an instance of a class is created.

Key Points:

  1. Same Name as Class: A constructor has the same name as the class.

  2. No Return Type: Constructors do not have a return type.

  3. Overloading: Constructors can be overloaded, meaning you can have multiple constructors in a class with different parameter lists.

Example:

public class Car {
    String brand;
    int year;

    // Default Constructor
    public Car() {
        brand = "Toyota";
        year = 2020;
    }

    // Parameterized Constructor
    public Car(String brand, int year) {
        this.brand = brand;
        this.year = year;
    }

    public void display() {
        System.out.println("Brand: " + brand + ", Year: " + year);
    }

    public static void main(String[] args) {
        Car car1 = new Car(); // Default constructor called
        Car car2 = new Car("Honda", 2022); // Parameterized constructor called

        car1.display();
        car2.display();
    }
}

Python

Definition: In Python, the constructor method is called __init__ and it is used to initialize the object's attributes.

Key Points:

  1. Method Name __init__: The constructor method is named __init__.

  2. First Parameter self: The first parameter of __init__ is always self, which refers to the instance being created.

  3. Automatic Call: __init__ is called automatically when a new instance of a class is created.

Example:

class Car:
    def __init__(self, brand, year):
        self.brand = brand
        self.year = year

    def display(self):
        print(f"Brand: {self.brand}, Year: {self.year}")

car1 = Car("Toyota", 2020) # Constructor called
car2 = Car("Honda", 2022) # Constructor called

car1.display()
car2.display()

Summary

Constructors are fundamental in object-oriented programming, ensuring that an object is initialized properly when it is created. Despite differences in syntax and some specific rules, the underlying concept of constructors remains consistent across languages.