ADVERTISEMENT
ADVERTISEMENT

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

C++ program to demonstrate example of Parameterized Constructor

In this program we will learn to create parameterized constructor program in c++ programming language.

Parameterized Constructor program in C++



/*C++ program to demonstrate example of Parameterized Constructor.*/
#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 block of member function to access data members.
    public:
        //Declaration of perameterized constructor to initailize data members.
            Demo (int a, int b); 
        //To take input from user.
        void    Input(); 
        //To display output onn screen.
        void    Display();
     
};//End of class
 
//Definition of constructor.
Demo:: Demo(int a, int b)
{
    X = a;
    Y = b;
}
 
//Definition of Input() member function.
void Demo:: Input()
{
    cout << "Enter Value of X: "; cin >> X;
    cout << "Enter Value of Y: "; cin >> Y;
}
 
 
//Definition of Display() member function.
void Demo:: Display()
{
    cout << endl << "X: " << X;
    cout << endl << "Y: " << Y << endl;
}
 
int main()
{
    Demo d(10,20) ; //Ctor autometically call when object is created.
 
    //Display value of data member.
    cout << endl <<"Method 1: " << endl;  
    cout << "Value after initialization: " ;
    d.Display();    
 
    d.Input();
    cout << "Value after User Input : ";
    d.Display();
 
 
    //We can also create object like this
    Demo d1 = Demo(10,20);
     
    //Display value of data member.
    cout << endl << "Method 2: " << endl;
    cout << "Value after initialization: ";
    d1.Display();   
         
    return 0;
}

Output

    Method 1: 
    Value after initialization: 
    X: 10
    Y: 20
    Enter Value of X: 25
    Enter Value of Y: 30
    Value after User Input : 
    X: 25
    Y: 30

    Method 2: 
    Value after initialization: 
    X: 10
    Y: 20

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


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.