ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » C+ solved programs » C++ constructor, destructor programs

C++ program to demonstrate example of constructor, destructor with default arguments

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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.