ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » C++ programs » C++ Constructor and Destructor programs

C++ program to demonstrate example of Constructor Overloading

In this program we will learn to create Constructor Overloading program in c++ programming language.

Constructor Overloading program in C++.



/*C++ program to demonstrate example of Constructor Overloading.*/
#include <iostream>
using namespace std;
 
//Class declaration.
class Demo
{
    //Private block to declare data member( X,Y ) of integer type.
    private:
        int X;
        int Y;
 
    //Public blocks of member function to access data members.
    public:
        //Declaration of default constructor to initialize data members.
            Demo (); 
 
        //Declaration of parameterized constructor to initialize data members.
            Demo (int a, int b); 
        //To display output onn screen.
        void    Display();
     
};//End of class
 
//Definition of parameterized constructor.
Demo:: Demo()
{
    X = 10;
    Y = 20;
}
 
//Definition of parameterized constructor.
Demo:: Demo(int a, int b)
{
    X = a;
    Y = b;
}
 
 
//Definition of Display() member function.
void Demo:: Display()
{
    cout << endl << "X: " << X;
    cout << endl << "Y: " << Y << endl;
}
 
int main()
{
    Demo d1;
    cout << "Default Constructor: " << endl;
    cout << "Value after initialization : " ;
    d1.Display();   
 
    Demo d2(30,40) ; //Ctor automatically call when object is created.
    cout << "Parameterized Constructor: " << endl;
    cout << "Value after initialization : " ;
    d2.Display();   
 
    return 0;
}

Output

    Default Constructor: 
    Value after initialization: 
    X: 10
    Y: 20
    Parameterized Constructor: 
    Value after initialization: 
    X: 30
    Y: 40

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

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.