Constructors and Destructors in
C++
A Detailed Explanation with Examples
What is a Constructor?
• A constructor is a special member function
that is automatically called when an object is
created. It initializes the object.
Characteristics of Constructors
• - Has the same name as the class
• - Does not have a return type
• - Can be overloaded
• - Called automatically when an object is
created
Types of Constructors
• - Default Constructor
• - Parameterized Constructor
• - Copy Constructor
Default Constructor
• A constructor that takes no arguments and
initializes objects with default values.
Example: Default Constructor
• #include <iostream>
• using namespace std;
• class Demo {
• public:
• Demo() { cout << "Default Constructor Called!"; }
• };
• int main() {
• Demo obj; // Constructor is automatically called
• return 0;
• }
Parameterized Constructor
• A constructor that takes arguments to initialize
an object with specific values.
Example: Parameterized Constructor
• #include <iostream>
• using namespace std;
• class Demo {
• public:
• int num;
• Demo(int n) { num = n; }
• void show() { cout << "Number: " << num; }
• };
• int main() {
• Demo obj(100); // Object initialized with 100
• obj.show();
• return 0;
• }
Copy Constructor
• A constructor that initializes a new object
using an existing object.
Example: Copy Constructor
• #include <iostream>
• using namespace std;
• class Demo {
• public:
• int num;
• Demo(int n) { num = n; }
• Demo(const Demo &obj) { num = obj.num; }
• void show() { cout << "Number: " << num; }
• };
• int main() {
• Demo obj1(50);
• Demo obj2 = obj1; // Copy Constructor called
• obj2.show();
• return 0;
• }
What is a Destructor?
• A destructor is a special member function that
is automatically called when an object is
destroyed. It is used for cleanup.
Characteristics of Destructor
• - Has the same name as the class but with a ~
prefix
• - Does not take any arguments
• - No return type
• - Called automatically when an object goes
out of scope
Example: Destructor
• #include <iostream>
• using namespace std;
• class Demo {
• public:
• Demo() { cout << "Constructor Called!"; }
• ~Demo() { cout << "Destructor Called!"; }
• };
• int main() {
• Demo obj;
• return 0; // Destructor is called automatically
• }
Why Use Constructors?
• Explanation of Why Use Constructors?
Why Use Destructors?
• Explanation of Why Use Destructors?
Constructor Overloading
Why Use Constructor Overloading?
✔ Initialize objects in different ways
✔ Provide flexibility in object creation
✔ Avoid multiple setter functions
Example of Constructor Overloading
#include <iostream>
using namespace std;
int main() {
class Student {
string name;
Student s1; // Calls
int age; Default Constructor
public:
// Default Constructor
Student s2("Alice"); // Calls
Student() { Constructor with 1 parameter
name = "Unknown";
age = 0; Student s3("Bob", 21); //
} Calls Constructor with 2
// Parameterized Constructor (1 parameter)
Student(string n) { parameters
name = n;
age = 18; // Default age
s1.display();
} s2.display();
// Parameterized Constructor (2 parameters)
Student(string n, int a) {
s3.display();
name = n;
age = a;
} return 0;
void display() { }
cout << "Name: " << name << ", Age: " << age << endl;
}
};
Output:
Name: Unknown, Age: 0
Name: Alice, Age: 18
Name: Bob, Age: 21
Constructor vs Destructor
• Explanation of Constructor vs Destructor
Real-World Examples of Constructors
• Explanation of Real-World Examples of
Constructors