Constructors in Programming Languages
Getting familiar with Constructors

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:
Same Name as Class: A constructor has the same name as the class.
No Return Type: Constructors do not have a return type, not even
void.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:
Keyword
constructor: The constructor method is calledconstructor.Called with
new: When an object is created using thenewkeyword, the constructor method is called.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:
Same Name as Class: A constructor has the same name as the class.
No Return Type: Constructors do not have a return type.
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:
Method Name
__init__: The constructor method is named__init__.First Parameter
self: The first parameter of__init__is alwaysself, which refers to the instance being created.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.
