C++ Program to using parameterized constructor


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


Test(int, int) { 
     //statement
}

Example: How to implement parameterized constructor in C++.

#include<iostream>
using namespace std;

class Test {
public:
   int a;

   Test(int x) {
       a = x;
   }
   void display() {
       cout << "Value 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:10
Value of an is:10