Understanding
Constructors and Destructors



What are Constructors and Destructors?

When working with object-oriented programming (OOP), objects are created from classes. But how do these objects get initialized with starting values or resources? That’s where constructors come in.

Similarly, when an object is no longer needed, and its resources should be released, destructors help perform cleanup operations automatically.

Constructor

A constructor is a special routine that gets called automatically when a new object is created. It is used to initialize the object with default or given values.

Think of it as a “setup function” for your object. Every time you create a new object, the constructor runs without you needing to explicitly call it.

Example: Constructor in Action

CLASS Car:
    FUNCTION Constructor():
        PRINT "A car object has been created!"
OBJECT myCar = NEW Car()

Output:

A car object has been created!

Intuition Check:

Question: Why don’t we call the constructor ourselves when creating an object?

Answer: Constructors are automatically invoked during object creation. This ensures that all objects are properly initialized before use.

Parameterized Constructor

Constructors can also accept arguments. This allows different objects to start with different values.

Example: Constructor with Parameters

CLASS User:
    FUNCTION Constructor(name, age):
        SET self.name = name
        SET self.age = age
        PRINT "User created: " + name + ", Age: " + age
OBJECT user1 = NEW User("Alice", 25)
OBJECT user2 = NEW User("Bob", 30)

Output:

User created: Alice, Age: 25
User created: Bob, Age: 30

Intuition Check:

Question: Can a class have more than one constructor?

Answer: Some languages support constructor overloading (multiple constructors with different parameters), but generally, you can mimic this behavior using conditional logic inside a single constructor.

Destructor

A destructor is the counterpart of a constructor. It is called automatically when the object is about to be destroyed or goes out of scope.

It is mainly used for cleanup activities like closing files, releasing memory, or disconnecting from services.

Example: Destructor in Action

CLASS FileHandler:
    FUNCTION Constructor():
        PRINT "File opened"

    FUNCTION Destructor():
        PRINT "File closed"
OBJECT file = NEW FileHandler()
// Do some file operations
DESTROY file // This will trigger the destructor

Output:

File opened
File closed

Intuition Check:

Question: What if we forget to destroy the object manually?

Answer: In most modern programming environments, destructors are triggered automatically by the system when the object is no longer in use (like garbage collection). However, in some low-level contexts, manual destruction is necessary.

Why Constructors and Destructors Matter

Summary

Constructors and destructors are essential in object-oriented programming to automate the creation and cleanup of objects. They ensure smooth lifecycle management and help maintain clean, efficient codebases.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

Mention your name, and programguru.org in the message. Your name shall be displayed in the sponsers list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M