New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

C++ Constructor and Destructor Example Program

Hello Everyone!

In this tutorial, we will learn how to demonstrate the concept of Constructor and Destructor in the C++ programming language.

To understand the concept of Constructor and Destructor in CPP, we will recommend you to visit here: C++ Constructor and Destructor, where we have explained it from scratch.

Code:

#include <iostream>

using namespace std;

//Rectangle class to demonstrate the working of Constructor and Destructor in CPP
class Rectangle {
    public:
        float length, breadth;

    //Declaration of the default Constructor of the Rectangle Class
    public:
        Rectangle() {
            cout << "\n\n****** Inside the Constructor ******* \n\n";
            length = 2;
            breadth = 4;
        }

    //Declaration of the Destructor of the Rectangle Class
    public:
        ~Rectangle() {
            cout << "\n\n****** Inside the Destructor ******* \n\n";
        }
};

//Defining the main method to access the members of the class
int main() {

    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the concept of Constructor and Destructor in CPP  ===== \n\n";

    cout << "\nCalling the default Constructor of the Rectangle class to initialize the object.\n\n";

    //Declaring the Class object to access the class members
    Rectangle rect;

    cout << "\nThe Length of the Rectangle set by the Constructor is = " << rect.length << "\n\n";

    cout << "\nThe Breadth of the Rectangle set by the Constructor is = " << rect.breadth << "\n\n";

    return 0;
}

Output:

C++ constructor and destructor

We hope that this post helped you develop better understanding of the concept of Contructor and Destructor in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )