In the previous chapter, we learned:
Object = Identity + State + BehaviorWe also learned:
class User
{
};is merely a blueprint.
But an important question remains:
How does and object actually come into existence?
When you write:
User user;what exactly happens?
Where is the object stored?
How is memory allocated?
Who initializes its state?
When is it destroyed?
Who free its memory?
What happens if initialization fails?
What if the object owns resources?
What if the object creates other objects?
Historical Context
Early programming languages often forced developers to manage memory manually.
Example:
malloc()
free()Programmers were responsible for:
Allocate memory
Initialize memory
Track ownership
Release memoryThis caused countless bugs:
Memory Leaks
Dangling Pointers
Double Free
Uninitialized MemoryC++ introduced a powerful concept:
Constructors and Destructors
which enabled automatic object lifecycle management.
This eventually evolved into one of C++'s greatest strengths:
RAII (Resource Acquisition Is Initialization)
The Lifecycle of an Object
Every object follows a lifecycle.
Creation
↓
Initialization
↓
Usage
↓
DestructionEvery object in C++ follows this journey.
Real-World Analogy: Hotel Guest
Consider a hotel.
Step 1: Arrival
Guest enters hotel.
Equivalent:
Object CreationStep 2: Check-In
Room assigned.
Equivalent:
InitializationStep 3: Stay
Guest uses room.
Equivalent:
Object UsageStep 4: Checkout
Guest leaves.
Equivalent:
Object DestructionEvery object follows the same lifecycle.
What Is a Constructor?
A constructor is:
A special function responsible for initializing an object when it is created.
Syntax:
class User
{
public:
User()
{
}
};Notice:
Same name as class
No return typeWhy Constructors Exist
Without constructors:
class User
{
public:
std::string name;
int age;
};Creation:
User user;What should:
name = ?
age = ?be initialized to?
The object may start in an invalid state.
Constructors solve this problem.
Example:
class User
{
public:
std::string name;
int age;
User()
{
name = "Unknown";
age = 0;
}
};Now every User begins in a valid state.
Default Constructor
A constructor with no parameters.
class User
{
public:
User()
{
}
};Usage:
User user;Parameterized Constructor
Allows initialization using values.
class User
{
public:
std::string name;
int age;
User(std::string n, int a)
{
name = n;
age = a;
}
};Usage:
User user("TheJat", 07);Constructor Overloading
A class can have multiple constructors.
class User
{
public:
User()
{
}
User(std::string name)
{
}
User(std::string name, int age)
{
}
};C++ selects the matching constructor.
Initialization Lists
Professional C++ code uses initialization lists.
Instead of:
User(std::string n, int a)
{
name = n;
age = a;
}Prefer:
User(std::string n, int a)
: name(n),
age(a)
{
}Why?
Because:
Direct Initializationinstead of:
Default Construction
+
AssignmentMore efficient.
Object Creation in Memory
When an object is created:
User user("TheJat", 07);two things happen:
Memory Allocated
↓
Constructor ExecutesImportant:
Allocation != InitializationThese are separate operations.
Stack Memory
Most objects are created on the stack.
Example:
void function()
{
User user("TheJat", 07);
}When function exists:
Object automatically destroyedBenefits of Stack Allocation
Fast
Automatic Cleanup
No Memory LeaksPreferred whenever possible.
Heap Memory
Objects can also be created dynamically.
User* user = new User("TheJat", 07);Memory:
Stack
+---------+
| Pointer |
+---------+
Heap
+---------+
| User |
+---------+Important Difference
Stack:
Automatic CleanupHeap:
Manual Cleanup RequiredExample:
delete user;Failure to delete:
Memory LeakObject Lifetime
Lifetime means:
The period during which an object exists.
Stack Object Lifetime
void process()
{
User user;
}Timeline:
Enter Function
↓
Create User
↓
Use User
↓
Exit Function
↓
Destroy UserAutomatic.
Heap Object Lifetime
User* user = new User();Timeline:
Create Object
↓
Use Object
↓
Delete Object
↓
Destroy ObjectControlled by programmer.
Destructor
Just as constructor creates objects:
Destructors clean them up.
Syntax:
class User
{
public:
~User()
{
}
};Characteristics:
Starts with ~
No Parameters
No Return TypeExample:
class File
{
public:
File()
{
std::cout << "Open File\n";
}
~File()
{
std::cout << "Close File\n";
}
};Usage:
{
File file;
}Output:
Open File
Close FileAutomatic cleanup.
Constructor and Destructor Order
Example:
class Engine
{
public:
Engine()
{
std::cout << "Engine Created\n";
}
~Engine()
{
std::cout << "Engine Destroyed\n";
}
};class Car
{
private:
Engine engine;
};Creation:
Engine Created
Car CreatedDestruction:
Car Destroyed
Engine DestroyedReverse order.
// Example program
#include <iostream>
#include <string>
using namespace std;
class Engine {
public:
Engine()
{
cout << "Engine constructor \n";
}
~Engine()
{
cout << "Engine destructor \n";
}
};
class Car{
public:
Engine engine;
Car()
{
cout<< "Car Constructor \n";
}
~Car()
{
cout<< "Car Destructor \n";
}
};
int main()
{
Car car1;
}Engine constructor
Car Constructor
Car Destructor
Engine destructor Ownership
One of the most important concepts in modern C++.
Question:
Who is responsible for cleanup?Example:
User* user = new User();Who deletes it?
Ownership determines responsibility.
Ownership Analogy
Imagine:
Car OwnerOwner responsible for:
Maintenance
Insurance
RepairsSimilarly:
Object OwnerResponsible for:
Creation
Lifecycle
CleanupResource Management
Objects often own resources.
Example:
Files
Sockets
Database Connections
Threads
LocksNot just memory.
Example:
class DatabaseConnection
{
};Opening connection:
Resource AcquiredClosing connection:
Resource ReleasedMust always happen correctly.
The Problem of Manual Cleanup
Example:
DatabaseConnection* db =
new DatabaseConnection();
processData();
delete db;Looks fine.
But what if:
processData();throws exception?
Then:
delete db;never executes.
Resource leak.
Enter RAII
RAII means:
Resource Acquisition Is Initialization
Idea:
Acquire Resource in Constructor
Release Resource in DestructorExample:
class File
{
public:
File()
{
open();
}
~File()
{
close();
}
};Now:
void process()
{
File file;
}Resource cleanup becomes automatic.
Why RAII Is Powerful
Without RAII:
Acquire Resource
Remember Cleanup
Hope Nothing FailsWith RAII:
Acquire Resource
Cleanup AutomaticMuch safer.
Leave a comment
Your email address will not be published. Required fields are marked *


