Implementing Constructors and Destructors in C++

Constructor Overloadinng and Default Construction

Constructor overloading allows a class to have multiple constructors with different parameter lists. The default constructor is called when an object is created without arguments.

#include<iostream>
using namespace std;

class LineSegment {
    int startX, startY;
    int endX, endY;

public:
    // Default constructor
    LineSegment() {
        startX = startY = endX = endY = 0;
        cout << "Default constructor executed" << endl;
    }
    
    // Constructor with four parameters
    LineSegment(int sx, int sy, int ex, int ey) {
        startX = sx; startY = sy;
        endX = ex; endY = ey;
        cout << "Created segment from (" << startX << "," << startY 
             << ") to (" << endX << "," << endY << ")" << endl;
    }
    
    // Constructor with two parameters
    LineSegment(int ex, int ey);
};

LineSegment::LineSegment(int ex, int ey) {
    startX = startY = 0;
    endX = ex; endY = ey;
    cout << "Created segment from (0,0) to (" << endX << "," << endY << ")" << endl;
}

int main() {
    LineSegment seg1;          // Calls default constructor
    LineSegment seg2(2, 3, 9, 5);  // Calls four-parameter constructor
    LineSegment seg3(5, 6);    // Calls two-parameter constructor
    return 0;
}

Output:

Default constructor executed
Created segment from (2,3) to (9,5)
Created segment from (0,0) to (5,6)

Destructor Implementation and Invocation

Destructors are automatically called when objects go out of scope, performing cleanup operations.

#include<iostream>
using namespace std;

class DataPoint {
    int x, y;

public:
    // Parameterized constructor
    DataPoint(int a, int b) {
        x = a; y = b;
        cout << "Parameterized constructor called" << endl;
    }
    
    // Default constructor
    DataPoint() {
        x = y = 0;
        cout << "Default constructor called" << endl;
    }
    
    // Destructor
    ~DataPoint() {
        cout << x << " - Destructor executed" << endl;
    }
    
    void display() {
        cout << x << '\t' << y << endl;
    }
};

int main() {
    DataPoint dp1;           // Default constructor
    DataPoint dp2(3, 30);    // Parameterized constructor
    dp1.display();
    dp2.display();
    cout << "Exiting main function" << endl;
    return 0;
}

Output:

Default constructor called
Parameterized constructor called
0       0
3       30
Exiting main function
3 - Destructor executed
0 - Destructor executed

Constructor and Destructor Invocation Sequence

Constructors execute in object creation order, while destructors execute in reverse order when objects are destroyed.

#include<iostream>
#include<cstring>
using namespace std;

class StudentRecord {
    int studentID;
    char studentName[10];
    char gender;

public:
    StudentRecord(int id, char name[10], char g) {
        studentID = id;
        strcpy(studentName, name);
        gender = g;
        cout << "Constructor invoked." << endl;
    }
    
    ~StudentRecord() {
        cout << "Destructor invoked for " << studentID << endl;
    }
    
    void showInfo() {
        cout << "ID: " << studentID << endl;
        cout << "Name: " << studentName << endl;
        cout << "Gender: " << gender << endl << endl;
    }
};

int main() {
    StudentRecord student1(10010, "Wang_li", 'f');
    student1.showInfo();
    StudentRecord student2(10011, "Zhang_fun", 'm');
    student2.showInfo();
    return 0;
}

Output:

Constructor invoked.
ID: 10010
Name: Wang_li
Gender: f

Constructor invoked.
ID: 10011
Name: Zhang_fun
Gender: m

Destructor invoked for 10011
Destructor invoked for 10010

Rectangle Class with Geometric Calculations

This class demonstrates encapsulation with constructors and memmber functions for geometric calculations.

#include<iostream>
using namespace std;

class Rectangle {
    int left, bottom;
    int right, top;

public:
    Rectangle(int l, int b, int r, int t) {
        left = (l < r) ? l : r;
        right = (l > r) ? l : r;
        bottom = (b < t) ? b : t;
        top = (b > t) ? b : t;
    }
    
    int getWidth() const { return right - left; }
    int getHeight() const { return top - bottom; }
    int perimeter() const { return 2 * (getWidth() + getHeight()); }
    int area() const { return getWidth() * getHeight(); }
    
    void printDetails() const {
        cout << "Coordinates: (" << left << "," << bottom << ") to (" 
             << right << "," << top << ")" << endl;
        cout << "Width: " << getWidth() << " Height: " << getHeight() << endl;
        cout << "Perimeter: " << perimeter() << endl;
        cout << "Area: " << area() << endl;
    }
};

int main() {
    Rectangle rect(0, 0, 3, 4);
    rect.printDetails();
    return 0;
}

Output:

Coordinates: (0,0) to (3,4)
Width: 3 Height: 4
Perimeter: 14
Area: 12

Tags: C++ Object-Oriented Programming Constructors destructors Class Design

Posted on Tue, 27 Oct 2026 16:13:12 +0000 by pkedpker