C++ Program to using parameterized constructor


In this program, You will learn how to implement parameterized constructor in C++.


Parameterized Constructor

Test(int, int){ }


Example: How to implement parameterized constructor in C++

Run it Now
#include<iostream>
using namespace std;

class Test {
public:
   int a;

   Test(int x) {
       a = x;
   }
   void display() {
       cout << "\nValue of a is :" << a;
   }
};

int main() {

   int x;
   cout << "Enter a Number :";
   cin>>x;

   Test obj(x);
   obj.display();

   return 0;
}

Output:

Enter a Number :12
Value of a is :12