Home »
C+ programs »
C++ constructor, destructor programs
C++ program to demonstrate example of constructor, destructor with default arguments
Learn about the constructor and destructor with default arguments in C++, how to implement constructor, destructor with default arguments in C++ program?
[Last updated : March 02, 2023]
In this program we will learn to create constructor, destructor with default arguments in c++ programming language.
Constructor, Destructor with default arguments program in C++
// C++ program to demonstrate example of
// constructor with default arguments.
#include <iostream>
using namespace std;
class Demo {
private: //Private Data member section
int X, Y;
public: //Public Member function section
//Default or no argument constructor.
Demo()
{
X = 0;
Y = 0;
cout << endl
<< "Constructor Called";
}
//Perameterized constructor with default argument.
Demo(int X, int Y = 20)
{
this->X = X;
this->Y = Y;
cout << endl
<< "Constructor Called";
}
//Destructor called when object is destroyed
~Demo()
{
cout << endl
<< "Destructor Called" << endl;
}
//To print output on console
void putValues()
{
cout << endl
<< "Value of X : " << X;
cout << endl
<< "Value of Y : " << Y << endl;
}
};
//main function : entry point of program
int main()
{
Demo d1 = Demo(10);
cout << endl
<< "D1 Value Are : ";
d1.putValues();
Demo d2 = Demo(30, 40);
cout << endl
<< "D2 Value Are : ";
d2.putValues();
return 0;
}
Output
Constructor Called
D1 Value Are :
Value of X : 10
Value of Y : 20
Constructor Called
D2 Value Are :
Value of X : 30
Value of Y : 40
Destructor Called
Destructor Called