Constructors (Introduction)

A constructor is a special member function of a class that is automatically called when an object is created.

Main idea:
Constructor’s job is to initialize the object.

That means:
• Give initial values to variables
• Prepare the object to be used

Why Constructor is Needed (Purpose)

Without constructor:

 
Student s;

Variables inside s may contain:
• Garbage values
• Undefined data

Constructor ensures:
• Object starts in a valid state
• Data is properly initialized

So the purpose of constructor is:
• Automatic initialization
• Safety
• Clean object creation

Special Properties of Constructor

A constructor has these rules:

• Same name as class
• No return type (not even void)
• Called automatically
• Runs only once per object

Example:

 
class Student {
public:
Student() {
cout << "Constructor called";
}
};

How Constructor Works (Memory Level)

When you write:

 
Student s1;

The system does this:

  1. Allocates memory for object s1

  2. Calls constructor automatically

  3. Initializes variables

  4. Object becomes ready

You never call constructor manually.

Example 1: Simple Constructor

 
#include <iostream>

using namespace std;
class Student {
public:
int roll;
Student() {
roll = 1;
cout << "Constructor executed" << endl;
}
};
int main() {
Student s;
cout << s.roll;
return 0;
}

Explanation:
• Constructor runs automatically
roll gets value 1
• Object is ready before use

Types of Constructors (Conceptual)

Even without naming types deeply, constructors can:

• Take no parameters
• Take parameters
• Set default values


Constructor with Parameters

Purpose:
 Initialize object using values provided by user.

Example:

 
class Student {
public:
int roll;
Student(int r) {
roll = r;
}
};

Usage:

 
Student s1(10);
Student s2(20);

Each object:
• Has its own value
• Uses same constructor logic

Example 2: Parameterized Constructor

 
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int speed;
Car(string b, int s) {
brand = b;
speed = s;
}
void show() {
cout << brand << " " << speed << endl;
}
};
int main() {
Car c1("Toyota", 120);
Car c2("Honda", 100);
c1.show();
c2.show();
return 0;
}

Explanation:
• Constructor sets data
• Objects are created fully initialized

Default Values Using Constructor

 
class Account {
public:
int balance;
Account() {
balance = 0;
}
};

This ensures:
• No garbage value
• Safe object

Destructors (Introduction)

A destructor is a special member function that is automatically called when an object is destroyed.

Main idea:
 Destructor’s job is to clean up resources.

Why Destructor is Needed (Purpose)

Destructor is used to:
• Free memory
• Release resources
• Close files
• Clean up before object dies

This prevents:
• Memory leaks
• Resource waste

Special Properties of Destructor

• Same name as class
• Starts with ~
• No return type
• No parameters
• Called automatically

Example:

 
class Student {
public:
~Student() {
cout << "Destructor called";
}
};

When Destructor is Called

Destructor is called when:

• Object goes out of scope
• Function ends
• Program ends
• Object is deleted

Memory-Level Understanding

When object is destroyed:

  1. Destructor executes

  2. Cleanup code runs

  3. Memory is freed

Order is guaranteed.

Example 3: Simple Destructor

 
#include <iostream>

using namespace std;
class Test {
public:
Test() {
cout << "Constructor called" << endl;
}
~Test() {
cout << "Destructor called" << endl;
}
};
int main() {
Test t;
cout << "Inside main" << endl;
return 0;
}

Output order:

 
Constructor called
Inside main
Destructor called

This proves:
• Constructor → object creation
• Destructor → object destruction

Constructor & Destructor Together (Very Important)

They always work as a pair.

Example:

 
class Demo {
public:
Demo() {
cout << "Object created" << endl;
}
~Demo() {
cout << "Object destroyed" << endl;
}
};

For each object:
• Constructor runs once
• Destructor runs once

Example 4: Destructor with Dynamic Memory

 
#include <iostream>
using namespace std;
class Sample {
int *p;
public:
Sample() {
p = new int(10);
cout << "Memory allocated" << endl;
}
~Sample() {
delete p;
cout << "Memory freed" << endl;
}
};
int main() {
Sample s;
return 0;
}

Explanation:
• Constructor allocates memory
• Destructor frees memory
• Prevents memory leak

Object Destruction Order (Extra Important)

Objects are destroyed:
• In reverse order of creation

Example:

 
Test a;
Test b;

Destruction order:
b destroyed first
a destroyed last